40
40
import java .util .List ;
41
41
import java .util .Locale ;
42
42
import java .util .Map ;
43
+ import java .util .Map .Entry ;
43
44
import java .util .Objects ;
44
45
import java .util .Optional ;
45
46
import java .util .Set ;
72
73
import org .springdoc .core .utils .PropertyResolverUtils ;
73
74
import org .springdoc .core .utils .SpringDocAnnotationsUtils ;
74
75
76
+ import org .springframework .aop .support .AopUtils ;
75
77
import org .springframework .beans .BeansException ;
76
78
import org .springframework .context .ApplicationContext ;
77
79
import org .springframework .context .ApplicationContextAware ;
80
82
import org .springframework .core .ResolvableType ;
81
83
import org .springframework .core .annotation .AnnotatedElementUtils ;
82
84
import org .springframework .http .HttpStatus ;
85
+ import org .springframework .http .MediaType ;
86
+ import org .springframework .http .ProblemDetail ;
83
87
import org .springframework .util .CollectionUtils ;
84
88
import org .springframework .util .ReflectionUtils ;
85
89
import org .springframework .web .bind .annotation .ControllerAdvice ;
@@ -277,7 +281,7 @@ public ApiResponses build(Components components, HandlerMethod handlerMethod, Op
277
281
private Map <String , ApiResponse > filterAndEnrichGenericMapResponseByDeclarations (HandlerMethod handlerMethod , Map <String , ApiResponse > genericMapResponse ) {
278
282
if (operationService .getJavadocProvider () != null ) {
279
283
JavadocProvider javadocProvider = operationService .getJavadocProvider ();
280
- for (Map . Entry <String , ApiResponse > genericResponse : genericMapResponse .entrySet ()) {
284
+ for (Entry <String , ApiResponse > genericResponse : genericMapResponse .entrySet ()) {
281
285
Map <String , Object > extensions = genericResponse .getValue ().getExtensions ();
282
286
Collection <String > genericExceptions = (Collection <String >) extensions .get (EXTENSION_EXCEPTION_CLASSES );
283
287
for (Class <?> declaredException : handlerMethod .getMethod ().getExceptionTypes ()) {
@@ -305,13 +309,13 @@ private Map<String, ApiResponse> filterAndEnrichGenericMapResponseByDeclarations
305
309
*/
306
310
public void buildGenericResponse (Components components , Map <String , Object > findControllerAdvice , Locale locale ) {
307
311
// ControllerAdvice
308
- for (Map . Entry <String , Object > entry : findControllerAdvice .entrySet ()) {
312
+ for (Entry <String , Object > entry : findControllerAdvice .entrySet ()) {
309
313
List <Method > methods = new ArrayList <>();
310
314
Object controllerAdvice = entry .getValue ();
311
315
// get all methods with annotation @ExceptionHandler
312
316
Class <?> objClz = controllerAdvice .getClass ();
313
- if (org . springframework . aop . support . AopUtils .isAopProxy (controllerAdvice ))
314
- objClz = org . springframework . aop . support . AopUtils .getTargetClass (controllerAdvice );
317
+ if (AopUtils .isAopProxy (controllerAdvice ))
318
+ objClz = AopUtils .getTargetClass (controllerAdvice );
315
319
ControllerAdviceInfo controllerAdviceInfo = new ControllerAdviceInfo (controllerAdvice );
316
320
Arrays .stream (ReflectionUtils .getAllDeclaredMethods (objClz ))
317
321
.filter (m -> m .isAnnotationPresent (ExceptionHandler .class )
@@ -422,11 +426,12 @@ private Map<String, ApiResponse> computeResponseFromDoc(Components components, M
422
426
*/
423
427
private void buildGenericApiResponses (Components components , MethodParameter methodParameter , ApiResponses apiResponsesOp ,
424
428
MethodAttributes methodAttributes ) {
429
+ ApiResponse apiResponse = null ;
425
430
if (!CollectionUtils .isEmpty (apiResponsesOp )) {
426
431
// API Responses at operation and @ApiResponse annotation
427
- for (Map . Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
432
+ for (Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
428
433
String httpCode = entry .getKey ();
429
- ApiResponse apiResponse = entry .getValue ();
434
+ apiResponse = entry .getValue ();
430
435
buildApiResponses (components , methodParameter , apiResponsesOp , methodAttributes , httpCode , apiResponse , true );
431
436
}
432
437
}
@@ -435,11 +440,21 @@ private void buildGenericApiResponses(Components components, MethodParameter met
435
440
// available
436
441
String httpCode = evaluateResponseStatus (methodParameter .getMethod (), Objects .requireNonNull (methodParameter .getMethod ()).getClass (), true );
437
442
if (Objects .nonNull (httpCode )) {
438
- ApiResponse apiResponse = methodAttributes .getGenericMapResponse ().containsKey (httpCode ) ? methodAttributes .getGenericMapResponse ().get (httpCode )
443
+ apiResponse = methodAttributes .getGenericMapResponse ().containsKey (httpCode ) ? methodAttributes .getGenericMapResponse ().get (httpCode )
439
444
: new ApiResponse ();
440
445
buildApiResponses (components , methodParameter , apiResponsesOp , methodAttributes , httpCode , apiResponse , true );
441
446
}
442
447
}
448
+ if (apiResponse != null ) {
449
+ Content content = apiResponse .getContent ();
450
+ if (content != null ) {
451
+ io .swagger .v3 .oas .models .media .MediaType mediaType = content .get (MediaType .ALL_VALUE );
452
+ if (mediaType != null && ProblemDetail .class .isAssignableFrom (methodParameter .getParameterType ())) {
453
+ content .addMediaType (MediaType .APPLICATION_PROBLEM_JSON_VALUE , mediaType );
454
+ content .remove (MediaType .ALL_VALUE );
455
+ }
456
+ }
457
+ }
443
458
}
444
459
445
460
/**
@@ -455,7 +470,7 @@ private void buildApiResponses(Components components, MethodParameter methodPara
455
470
Map <String , ApiResponse > genericMapResponse = methodAttributes .getGenericMapResponse ();
456
471
if (!CollectionUtils .isEmpty (apiResponsesOp ) && apiResponsesOp .size () > genericMapResponse .size ()) {
457
472
// API Responses at operation and @ApiResponse annotation
458
- for (Map . Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
473
+ for (Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
459
474
String httpCode = entry .getKey ();
460
475
boolean methodAttributesCondition = !methodAttributes .isMethodOverloaded () || (methodAttributes .isMethodOverloaded () && isValidHttpCode (httpCode , methodParameter ));
461
476
if (!genericMapResponse .containsKey (httpCode ) && methodAttributesCondition ) {
@@ -693,8 +708,8 @@ private Map<String, ApiResponse> getGenericMapResponse(HandlerMethod handlerMeth
693
708
List <ControllerAdviceInfo > controllerAdviceInfosInThisBean = localExceptionHandlers .stream ()
694
709
.filter (controllerInfo -> {
695
710
Class <?> objClz = controllerInfo .getControllerAdvice ().getClass ();
696
- if (org . springframework . aop . support . AopUtils .isAopProxy (controllerInfo .getControllerAdvice ()))
697
- objClz = org . springframework . aop . support . AopUtils .getTargetClass (controllerInfo .getControllerAdvice ());
711
+ if (AopUtils .isAopProxy (controllerInfo .getControllerAdvice ()))
712
+ objClz = AopUtils .getTargetClass (controllerInfo .getControllerAdvice ());
698
713
return beanType .equals (objClz );
699
714
})
700
715
.toList ();
@@ -772,7 +787,7 @@ private boolean isValidHttpCode(String httpCode, MethodParameter methodParameter
772
787
final io .swagger .v3 .oas .annotations .Operation apiOperation = AnnotatedElementUtils .findMergedAnnotation (method ,
773
788
io .swagger .v3 .oas .annotations .Operation .class );
774
789
if (apiOperation != null ) {
775
- responseSet = new HashSet <>(Arrays . asList (apiOperation .responses ()));
790
+ responseSet = new HashSet <>(asList (apiOperation .responses ()));
776
791
if (isHttpCodePresent (httpCode , responseSet ))
777
792
result = true ;
778
793
}
0 commit comments