diff --git a/src/main/java/org/springframework/data/repository/util/VavrCollections.java b/src/main/java/org/springframework/data/repository/util/VavrCollections.java index 71a9fe120..6ab56c782 100644 --- a/src/main/java/org/springframework/data/repository/util/VavrCollections.java +++ b/src/main/java/org/springframework/data/repository/util/VavrCollections.java @@ -116,18 +116,35 @@ class VavrCollections { */ @Nullable @Override - public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + public Object convert(@Nullable Object source, TypeDescriptor sourceDescriptor, TypeDescriptor targetDescriptor) { + + Class targetType = targetDescriptor.getType(); + + if (io.vavr.collection.Seq.class.isAssignableFrom(targetType)) { + return io.vavr.collection.List.ofAll((Iterable) source); + } + + if (io.vavr.collection.Set.class.isAssignableFrom(targetType)) { + return LinkedHashSet.ofAll((Iterable) source); + } + + if (io.vavr.collection.Map.class.isAssignableFrom(targetType)) { + return LinkedHashMap.ofAll((Map) source); + } + + // No dedicated type asked for, probably Traversable. + // Try to stay as close to the source value. if (source instanceof List) { return io.vavr.collection.List.ofAll((Iterable) source); } - if (source instanceof java.util.Set) { + if (source instanceof Set) { return LinkedHashSet.ofAll((Iterable) source); } - if (source instanceof java.util.Map) { - return LinkedHashMap.ofAll((java.util.Map) source); + if (source instanceof Map) { + return LinkedHashMap.ofAll((Map) source); } return source; diff --git a/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java b/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java index 33ccd387a..a96c41513 100755 --- a/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java +++ b/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java @@ -338,4 +338,13 @@ public class QueryExecutionConvertersUnitTests { Set> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes(); assertThat(allowedPageableTypes).contains(io.vavr.collection.Seq.class); } + + @Test // DATAJPA-1258 + public void convertsJavaListsToVavrSet() { + + List source = Collections.singletonList("foo"); + + assertThat(conversionService.convert(source, io.vavr.collection.Set.class)) // + .isInstanceOf(io.vavr.collection.Set.class); + } }