43
43
import org .apache .commons .lang3 .ArrayUtils ;
44
44
import org .apache .commons .lang3 .StringUtils ;
45
45
46
+ import org .springframework .core .MethodParameter ;
46
47
import org .springframework .core .annotation .AnnotatedElementUtils ;
47
48
import org .springframework .http .HttpStatus ;
48
49
import org .springframework .util .CollectionUtils ;
@@ -82,7 +83,7 @@ public ApiResponses build(Components components, HandlerMethod handlerMethod, Op
82
83
apiResponsesFromDoc .forEach (apiResponses ::addApiResponse );
83
84
// for each one build ApiResponse and add it to existing responses
84
85
// Fill api Responses
85
- computeResponse (components , handlerMethod .getMethod (), handlerMethod . getReturnType (). getParameterType (), apiResponses , methodAttributes , false );
86
+ computeResponse (components , handlerMethod .getReturnType (), apiResponses , methodAttributes , false );
86
87
return apiResponses ;
87
88
}
88
89
@@ -97,7 +98,7 @@ public void buildGenericResponse(Components components, Map<String, Object> find
97
98
if (reqMappringMethod != null ) {
98
99
methodProduces = reqMappringMethod .produces ();
99
100
}
100
- Map <String , ApiResponse > apiResponses = computeResponse (components , method ,null , new ApiResponses (),
101
+ Map <String , ApiResponse > apiResponses = computeResponse (components , new MethodParameter ( method ,- 1 ) , new ApiResponses (),
101
102
new MethodAttributes (methodProduces , springDocConfigProperties .getDefaultConsumesMediaType (), springDocConfigProperties .getDefaultProducesMediaType ()), true );
102
103
apiResponses .forEach (genericMapResponse ::put );
103
104
}
@@ -118,10 +119,10 @@ private List<Method> getMethods(Map<String, Object> findControllerAdvice) {
118
119
return methods ;
119
120
}
120
121
121
- private Map <String , ApiResponse > computeResponse (Components components , Method method , Class <?> clazz , ApiResponses apiResponsesOp ,
122
+ private Map <String , ApiResponse > computeResponse (Components components , MethodParameter methodParameter , ApiResponses apiResponsesOp ,
122
123
MethodAttributes methodAttributes , boolean isGeneric ) {
123
124
// Parsing documentation, if present
124
- Set <io .swagger .v3 .oas .annotations .responses .ApiResponse > responsesArray = getApiResponses (method );
125
+ Set <io .swagger .v3 .oas .annotations .responses .ApiResponse > responsesArray = getApiResponses (methodParameter . getMethod () );
125
126
if (!responsesArray .isEmpty ()) {
126
127
methodAttributes .setWithApiResponseDoc (true );
127
128
if (!springDocConfigProperties .isOverrideWithGenericResponse ())
@@ -146,7 +147,7 @@ private Map<String, ApiResponse> computeResponse(Components components, Method m
146
147
apiResponsesOp .addApiResponse (apiResponseAnnotations .responseCode (), apiResponse );
147
148
}
148
149
}
149
- buildApiResponses (components , method , clazz , apiResponsesOp , methodAttributes , isGeneric );
150
+ buildApiResponses (components , methodParameter , apiResponsesOp , methodAttributes , isGeneric );
150
151
return apiResponsesOp ;
151
152
}
152
153
@@ -174,25 +175,25 @@ private void buildContentFromDoc(Components components, ApiResponses apiResponse
174
175
}
175
176
}
176
177
177
- private void buildApiResponses (Components components , Method method , Class <?> clazz , ApiResponses apiResponsesOp ,
178
+ private void buildApiResponses (Components components , MethodParameter methodParameter , ApiResponses apiResponsesOp ,
178
179
MethodAttributes methodAttributes , boolean isGeneric ) {
179
180
if (!CollectionUtils .isEmpty (apiResponsesOp ) && (apiResponsesOp .size () != genericMapResponse .size () || isGeneric )) {
180
181
// API Responses at operation and @ApiResponse annotation
181
182
for (Map .Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
182
183
String httpCode = entry .getKey ();
183
184
ApiResponse apiResponse = entry .getValue ();
184
- buildApiResponses (components , method , clazz , apiResponsesOp , methodAttributes , httpCode , apiResponse ,
185
+ buildApiResponses (components , methodParameter , apiResponsesOp , methodAttributes , httpCode , apiResponse ,
185
186
isGeneric );
186
187
}
187
188
}
188
189
else {
189
190
// Use response parameters with no description filled - No documentation
190
191
// available
191
- String httpCode = evaluateResponseStatus (method , method .getClass (), isGeneric );
192
+ String httpCode = evaluateResponseStatus (methodParameter . getMethod (), methodParameter . getMethod () .getClass (), isGeneric );
192
193
ApiResponse apiResponse = genericMapResponse .containsKey (httpCode ) ? genericMapResponse .get (httpCode )
193
194
: new ApiResponse ();
194
195
if (httpCode != null )
195
- buildApiResponses (components , method , clazz , apiResponsesOp , methodAttributes , httpCode , apiResponse ,
196
+ buildApiResponses (components , methodParameter , apiResponsesOp , methodAttributes , httpCode , apiResponse ,
196
197
isGeneric );
197
198
}
198
199
}
@@ -222,9 +223,9 @@ private Set<io.swagger.v3.oas.annotations.responses.ApiResponse> getApiResponses
222
223
return responses ;
223
224
}
224
225
225
- private Content buildContent (Components components , Method method , Class <?> clazz , String [] methodProduces , JsonView jsonView ) {
226
+ private Content buildContent (Components components , MethodParameter methodParameter , String [] methodProduces , JsonView jsonView ) {
226
227
Content content = new Content ();
227
- Type returnType = getReturnType (method , clazz );
228
+ Type returnType = getReturnType (methodParameter );
228
229
if (isVoid (returnType )) {
229
230
// if void, no content
230
231
content = null ;
@@ -241,11 +242,11 @@ else if (ArrayUtils.isNotEmpty(methodProduces)) {
241
242
return content ;
242
243
}
243
244
244
- private Type getReturnType (Method method , Class <?> clazz ) {
245
+ private Type getReturnType (MethodParameter methodParameter ) {
245
246
Type returnType = Object .class ;
246
247
for (ReturnTypeParser returnTypeParser : returnTypeParsers ) {
247
248
if (returnType .getTypeName ().equals (Object .class .getTypeName ())) {
248
- returnType = returnTypeParser .getReturnType (method , clazz );
249
+ returnType = returnTypeParser .getReturnType (methodParameter );
249
250
}
250
251
else {
251
252
break ;
@@ -272,12 +273,12 @@ private void setContent(String[] methodProduces, Content content,
272
273
Arrays .stream (methodProduces ).forEach (mediaTypeStr -> content .addMediaType (mediaTypeStr , mediaType ));
273
274
}
274
275
275
- private void buildApiResponses (Components components , Method method , Class <?> clazz , ApiResponses apiResponsesOp ,
276
+ private void buildApiResponses (Components components , MethodParameter methodParameter , ApiResponses apiResponsesOp ,
276
277
MethodAttributes methodAttributes , String httpCode , ApiResponse apiResponse , boolean isGeneric ) {
277
278
// No documentation
278
279
if (StringUtils .isBlank (apiResponse .get$ref ())) {
279
280
if (apiResponse .getContent () == null ) {
280
- Content content = buildContent (components , method , clazz , methodAttributes .getMethodProduces (),
281
+ Content content = buildContent (components , methodParameter , methodAttributes .getMethodProduces (),
281
282
methodAttributes .getJsonViewAnnotation ());
282
283
apiResponse .setContent (content );
283
284
}
@@ -292,7 +293,7 @@ else if (CollectionUtils.isEmpty(apiResponse.getContent())) {
292
293
&& ((isGeneric || methodAttributes .isMethodOverloaded ()) && methodAttributes .isNoApiResponseDoc ())) {
293
294
// Merge with existing schema
294
295
Content existingContent = apiResponse .getContent ();
295
- Schema <?> schemaN = calculateSchema (components , method . getGenericReturnType (),
296
+ Schema <?> schemaN = calculateSchema (components , methodParameter . getGenericParameterType (),
296
297
methodAttributes .getJsonViewAnnotation ());
297
298
if (schemaN != null && ArrayUtils .isNotEmpty (methodAttributes .getMethodProduces ())) {
298
299
Arrays .stream (methodAttributes .getMethodProduces ()).forEach (mediaTypeStr -> mergeSchema (existingContent , schemaN , mediaTypeStr ));
0 commit comments