Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import java.util.function.*;
import java.util.stream.*;

import static com.redis.om.spring.util.ObjectUtils.isPrimitiveOfType;
import static org.springframework.util.ClassUtils.isPrimitiveWrapper;

public class ReturnFieldsSearchStreamImpl<E, T> implements SearchStream<T> {

@SuppressWarnings("unused")
Expand Down Expand Up @@ -320,6 +323,8 @@ private List<T> toResultTuple(SearchResult searchResult, String[] returnFields)
mappedResults.add(new Point(Double.parseDouble(lon), Double.parseDouble(lat)));
} else if (targetClass == String.class) {
mappedResults.add(value.toString());
} else if (targetClass == Boolean.class || isPrimitiveOfType(targetClass, Boolean.class)) {
mappedResults.add(value.toString().equals("1"));
} else {
mappedResults.add(gson.fromJson(value.toString(), targetClass));
}
Expand Down Expand Up @@ -348,6 +353,12 @@ private List<T> toResultTuple(List<E> entities, String[] returnFields) {
returning.forEach(foi -> {
String getterName = "get" + ObjectUtils.ucfirst(foi.getSearchAlias());
Method getter = ReflectionUtils.findMethod(entitySearchStream.getEntityClass(), getterName);
// No "getXXX", maybe there is a "isXXX"
if (getter == null) {
getterName = "is" + ObjectUtils.ucfirst(foi.getSearchAlias());
getter = ReflectionUtils.findMethod(entitySearchStream.getEntityClass(), getterName);
}
//TODO: if getter is still null then field access?
mappedResults.add(ReflectionUtils.invokeMethod(getter, entity));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.function.Function;

import static java.util.Objects.requireNonNull;
import static org.springframework.util.ClassUtils.isPrimitiveWrapper;
import static org.springframework.util.ClassUtils.resolvePrimitiveIfNecessary;

public class ObjectUtils {
public static String getDistanceAsRedisString(Distance distance) {
Expand Down Expand Up @@ -417,6 +419,10 @@ public static Collection instantiateCollection(Type type) {
}
}

public static boolean isPrimitiveOfType(Class<?> clazz, Class<?> wrapper) {
return clazz.isPrimitive() && resolvePrimitiveIfNecessary(clazz) == wrapper;
}


private ObjectUtils() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
import lombok.*;
import org.springframework.data.annotation.Id;

@Data
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(force = true)
@Document
public class DocWithBoolean {
@Id
private String id;

@NonNull
@Indexed
public Boolean indexedBoolean;

@NonNull
public Boolean nonIndexedBoolean;

@Indexed
public boolean indexedPrimitiveBoolean;

public boolean nonIndexedPrimitiveBoolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.repository.RedisDocumentRepository;

@SuppressWarnings("unused") public interface DocWithBooleanRepository extends RedisDocumentRepository<DocWithBoolean, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@Autowired DeepListRepository deepListRepository;
@Autowired DocWithLongRepository docWithLongRepository;
@Autowired PersonRepository personRepository;
@Autowired DocWithBooleanRepository docWithBooleanRepository;

@Autowired EntityStream entityStream;

Expand Down Expand Up @@ -319,4 +320,60 @@ void testMapEntityStreamsReturnNestedField() {
.collect(Collectors.toList());
assertThat(results).containsOnly("nl-2-2");
}

// issue gh-265 - indexed boolean
@Test
void testMapEntityStreamsReturnIndexedBooleanValue() {
DocWithBoolean docWithBoolean = new DocWithBoolean();
docWithBoolean.setIndexedBoolean(true);
docWithBooleanRepository.save(docWithBoolean);

var result = entityStream.of(DocWithBoolean.class)
.filter(DocWithBoolean$.ID.eq(docWithBoolean.getId()))
.map(DocWithBoolean$.INDEXED_BOOLEAN) // should handle returned indexed boolean value, but fails
.collect(Collectors.toList());
assertThat(result).containsOnly(true);
}

// issue gh-265 - indexed boolean primitive
@Test
void testMapEntityStreamsReturnIndexedBooleanPrimitiveValue() {
DocWithBoolean docWithBoolean = new DocWithBoolean();
docWithBoolean.setIndexedPrimitiveBoolean(true);
docWithBooleanRepository.save(docWithBoolean);

var result = entityStream.of(DocWithBoolean.class)
.filter(DocWithBoolean$.ID.eq(docWithBoolean.getId()))
.map(DocWithBoolean$.INDEXED_PRIMITIVE_BOOLEAN) // should handle returned indexed boolean primitive value, but fails
.collect(Collectors.toList());
assertThat(result).containsOnly(true);
}

// issue gh-265 - non indexed boolean
@Test
void testMapEntityStreamsReturnNonIndexedBooleanValue() {
DocWithBoolean docWithBoolean = new DocWithBoolean();
docWithBoolean.setNonIndexedBoolean(true);
docWithBooleanRepository.save(docWithBoolean);

var result = entityStream.of(DocWithBoolean.class)
.filter(DocWithBoolean$.ID.eq(docWithBoolean.getId()))
.map(DocWithBoolean$.NON_INDEXED_BOOLEAN) // should handle returned non indexed boolean value, succeeds
.collect(Collectors.toList());
assertThat(result).containsOnly(true);
}

// issue gh-265 - non indexed boolean primitive
@Test
void testMapEntityStreamsReturnNonIndexedBooleanPrimitiveValue() {
DocWithBoolean docWithBoolean = new DocWithBoolean();
docWithBoolean.setNonIndexedPrimitiveBoolean(true);
docWithBooleanRepository.save(docWithBoolean);

var result = entityStream.of(DocWithBoolean.class)
.filter(DocWithBoolean$.ID.eq(docWithBoolean.getId()))
.map(DocWithBoolean$.NON_INDEXED_PRIMITIVE_BOOLEAN) // should handle returned non indexed primitive boolean primitive value, but fails
.collect(Collectors.toList());
assertThat(result).containsOnly(true);
}
}