23
23
import org .springframework .web .bind .annotation .ResponseStatus ;
24
24
import org .springframework .web .method .HandlerMethod ;
25
25
26
- import java .lang .reflect .Method ;
27
- import java .lang .reflect .ParameterizedType ;
28
- import java .lang .reflect .Type ;
26
+ import java .lang .reflect .*;
29
27
import java .util .*;
30
28
import java .util .stream .Collectors ;
31
29
import java .util .stream .Stream ;
@@ -39,9 +37,12 @@ public abstract class AbstractResponseBuilder {
39
37
40
38
private final OperationBuilder operationBuilder ;
41
39
42
- protected AbstractResponseBuilder (OperationBuilder operationBuilder ) {
40
+ private final List <ReturnTypeParser > returnTypeParsers ;
41
+
42
+ protected AbstractResponseBuilder (OperationBuilder operationBuilder , List <ReturnTypeParser > returnTypeParsers ) {
43
43
super ();
44
44
this .operationBuilder = operationBuilder ;
45
+ this .returnTypeParsers = returnTypeParsers ;
45
46
}
46
47
47
48
public ApiResponses build (Components components , HandlerMethod handlerMethod , Operation operation ,
@@ -203,7 +204,7 @@ private Set<io.swagger.v3.oas.annotations.responses.ApiResponse> getApiResponses
203
204
204
205
private Content buildContent (Components components , Method method , String [] methodProduces , JsonView jsonView ) {
205
206
Content content = new Content ();
206
- Type returnType = method . getGenericReturnType ( );
207
+ Type returnType = getReturnType ( method );
207
208
if (isVoid (returnType )) {
208
209
// if void, no content
209
210
content = null ;
@@ -219,6 +220,19 @@ private Content buildContent(Components components, Method method, String[] meth
219
220
return content ;
220
221
}
221
222
223
+ private Type getReturnType (Method method ) {
224
+ Type returnType = Object .class ;
225
+ for (ReturnTypeParser returnTypeParser : returnTypeParsers ) {
226
+ if (returnType .getTypeName ().equals (Object .class .getTypeName ())) {
227
+ returnType = returnTypeParser .getReturnType (method );
228
+ } else {
229
+ break ;
230
+ }
231
+ }
232
+
233
+ return returnType ;
234
+ }
235
+
222
236
private Schema <?> calculateSchema (Components components , Type returnType , JsonView jsonView ) {
223
237
Schema <?> schemaN = null ;
224
238
if (isVoid (returnType )) {
@@ -328,4 +342,15 @@ private boolean isVoid(Type returnType) {
328
342
return Void .TYPE .equals (returnType ) || (returnType instanceof ParameterizedType
329
343
&& Void .class .equals (((ParameterizedType ) returnType ).getActualTypeArguments ()[0 ]));
330
344
}
345
+
346
+ interface ReturnTypeParser {
347
+ Type getReturnType (Method method );
348
+ }
349
+
350
+ public static class GenericReturnTypeParser implements ReturnTypeParser {
351
+ @ Override
352
+ public Type getReturnType (Method method ) {
353
+ return method .getGenericReturnType ();
354
+ }
355
+ }
331
356
}
0 commit comments