@@ -70,26 +70,15 @@ public static FunctionDeclaration buildFunctionDeclaration(
7070
7171 Type returnType = func .getGenericReturnType ();
7272 if (returnType != Void .TYPE ) {
73- Type realReturnType = returnType ;
74- if (returnType instanceof ParameterizedType ) {
75- ParameterizedType parameterizedReturnType = (ParameterizedType ) returnType ;
76- String returnTypeName = ((Class <?>) parameterizedReturnType .getRawType ()).getName ();
77- if (returnTypeName .equals ("io.reactivex.rxjava3.core.Maybe" )
78- || returnTypeName .equals ("io.reactivex.rxjava3.core.Single" )) {
79- returnType = parameterizedReturnType .getActualTypeArguments ()[0 ];
80- if (returnType instanceof ParameterizedType ) {
81- ParameterizedType maybeParameterizedType = (ParameterizedType ) returnType ;
82- returnTypeName = ((Class <?>) maybeParameterizedType .getRawType ()).getName ();
83- }
84- }
85- if (returnTypeName .equals ("java.util.Map" )
86- || returnTypeName .equals ("com.google.common.collect.ImmutableMap" )) {
87- return builder .response (buildSchemaFromType (returnType )).build ();
73+ Type actualReturnType = returnType ;
74+ if (returnType instanceof ParameterizedType parameterizedReturnType ) {
75+ String rawTypeName = ((Class <?>) parameterizedReturnType .getRawType ()).getName ();
76+ if (rawTypeName .equals ("io.reactivex.rxjava3.core.Maybe" )
77+ || rawTypeName .equals ("io.reactivex.rxjava3.core.Single" )) {
78+ actualReturnType = parameterizedReturnType .getActualTypeArguments ()[0 ];
8879 }
8980 }
90- throw new IllegalArgumentException (
91- "Return type should be Map or Maybe<Map> or Single<Map>, but it was "
92- + realReturnType .getTypeName ());
81+ builder .response (buildSchemaFromType (actualReturnType ));
9382 }
9483 return builder .build ();
9584 }
@@ -107,50 +96,31 @@ static FunctionDeclaration buildFunctionDeclaration(JsonBaseModel func, String d
10796 }
10897
10998 private static Schema buildSchemaFromParameter (Parameter param ) {
110- Schema . Builder builder = Schema . builder ( );
99+ Schema schema = buildSchemaFromType ( param . getParameterizedType () );
111100 if (param .isAnnotationPresent (Annotations .Schema .class )
112101 && !param .getAnnotation (Annotations .Schema .class ).description ().isEmpty ()) {
113- builder .description (param .getAnnotation (Annotations .Schema .class ).description ());
114- }
115- switch (param .getType ().getName ()) {
116- case "java.lang.String" -> builder .type ("STRING" );
117- case "boolean" , "java.lang.Boolean" -> builder .type ("BOOLEAN" );
118- case "int" , "java.lang.Integer" -> builder .type ("INTEGER" );
119- case "double" , "java.lang.Double" , "float" , "java.lang.Float" , "long" , "java.lang.Long" ->
120- builder .type ("NUMBER" );
121- case "java.util.List" ->
122- builder
123- .type ("ARRAY" )
124- .items (
125- buildSchemaFromType (
126- ((ParameterizedType ) param .getParameterizedType ())
127- .getActualTypeArguments ()[0 ]));
128- case "java.util.Map" -> builder .type ("OBJECT" );
129- default -> {
130- BeanDescription beanDescription =
131- OBJECT_MAPPER
132- .getSerializationConfig ()
133- .introspect (OBJECT_MAPPER .constructType (param .getType ()));
134- Map <String , Schema > properties = new LinkedHashMap <>();
135- for (BeanPropertyDefinition property : beanDescription .findProperties ()) {
136- properties .put (property .getName (), buildSchemaFromType (property .getRawPrimaryType ()));
137- }
138- builder .type ("OBJECT" ).properties (properties );
139- }
102+ return schema .toBuilder ()
103+ .description (param .getAnnotation (Annotations .Schema .class ).description ())
104+ .build ();
140105 }
141- return builder . build () ;
106+ return schema ;
142107 }
143108
144109 public static Schema buildSchemaFromType (Type type ) {
145110 Schema .Builder builder = Schema .builder ();
146111 if (type instanceof ParameterizedType parameterizedType ) {
147- switch (((Class <?>) parameterizedType .getRawType ()).getName ()) {
148- case "java.util.List" ->
149- builder
150- .type ("ARRAY" )
151- .items (buildSchemaFromType (parameterizedType .getActualTypeArguments ()[0 ]));
152- case "java.util.Map" , "com.google.common.collect.ImmutableMap" -> builder .type ("OBJECT" );
153- default -> throw new IllegalArgumentException ("Unsupported generic type: " + type );
112+ String rawTypeName = ((Class <?>) parameterizedType .getRawType ()).getName ();
113+ switch (rawTypeName ) {
114+ case "java.util.List" , "com.google.common.collect.ImmutableList" :
115+ Schema itemSchema = buildSchemaFromType (parameterizedType .getActualTypeArguments ()[0 ]);
116+ builder .type ("ARRAY" ).items (itemSchema );
117+ break ;
118+ case "java.util.Map" :
119+ case "com.google.common.collect.ImmutableMap" :
120+ builder .type ("OBJECT" );
121+ break ;
122+ default :
123+ throw new IllegalArgumentException ("Unsupported generic type: " + type );
154124 }
155125 } else if (type instanceof Class <?> clazz ) {
156126 switch (clazz .getName ()) {
0 commit comments