From 3d81f3915e0712ef2ac599d3cdc0bebaeb5e9165 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 11 Nov 2025 10:53:02 +0100 Subject: [PATCH] Use conversion service --- .../repository/aot/MongoCodeBlocks.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/MongoCodeBlocks.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/MongoCodeBlocks.java index 97e79a42c..432113833 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/MongoCodeBlocks.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/MongoCodeBlocks.java @@ -20,6 +20,8 @@ import java.util.regex.Pattern; import org.bson.Document; import org.jspecify.annotations.Nullable; import org.springframework.core.annotation.MergedAnnotation; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mongodb.repository.ReadPreference; import org.springframework.data.mongodb.repository.aot.AggregationBlocks.AggregationCodeBlockBuilder; @@ -241,20 +243,16 @@ class MongoCodeBlocks { public static CodeBlock potentiallyWrapStreamable(MethodReturn methodReturn, CodeBlock returningIterable) { Class returnType = methodReturn.toClass(); + if (returnType.equals(Streamable.class)) { return CodeBlock.of("$T.of($L)", Streamable.class, returningIterable); } + if (ClassUtils.isAssignable(Streamable.class, returnType)) { - CodeBlock streamable = CodeBlock.of("$T.of($L)", Streamable.class, returningIterable); - if (ClassUtils.hasConstructor(returnType, Streamable.class)) { - return CodeBlock.of("new $T($L)", returnType, streamable); - } - if (ClassUtils.hasAtLeastOneMethodWithName(returnType, "of")) { - return CodeBlock.of("$T.of($L)", returnType, streamable); - } - if (ClassUtils.hasAtLeastOneMethodWithName(returnType, "valueOf")) { - return CodeBlock.of("$T.valueOf($L)", returnType, streamable); - } + + return CodeBlock.of( + "($1T) $2T.getSharedInstance().convert($3T.of($4L), $5T.valueOf($3T.class), $5T.valueOf($1T.class))", + returnType, DefaultConversionService.class, Streamable.class, returningIterable, TypeDescriptor.class); } return returningIterable;