4040import java .util .List ;
4141import java .util .Locale ;
4242import java .util .Map ;
43+ import java .util .Map .Entry ;
4344import java .util .Objects ;
4445import java .util .Optional ;
4546import java .util .Set ;
7273import org .springdoc .core .utils .PropertyResolverUtils ;
7374import org .springdoc .core .utils .SpringDocAnnotationsUtils ;
7475
76+ import org .springframework .aop .support .AopUtils ;
7577import org .springframework .beans .BeansException ;
7678import org .springframework .context .ApplicationContext ;
7779import org .springframework .context .ApplicationContextAware ;
8082import org .springframework .core .ResolvableType ;
8183import org .springframework .core .annotation .AnnotatedElementUtils ;
8284import org .springframework .http .HttpStatus ;
85+ import org .springframework .http .MediaType ;
86+ import org .springframework .http .ProblemDetail ;
8387import org .springframework .util .CollectionUtils ;
8488import org .springframework .util .ReflectionUtils ;
8589import org .springframework .web .bind .annotation .ControllerAdvice ;
@@ -277,7 +281,7 @@ public ApiResponses build(Components components, HandlerMethod handlerMethod, Op
277281 private Map <String , ApiResponse > filterAndEnrichGenericMapResponseByDeclarations (HandlerMethod handlerMethod , Map <String , ApiResponse > genericMapResponse ) {
278282 if (operationService .getJavadocProvider () != null ) {
279283 JavadocProvider javadocProvider = operationService .getJavadocProvider ();
280- for (Map . Entry <String , ApiResponse > genericResponse : genericMapResponse .entrySet ()) {
284+ for (Entry <String , ApiResponse > genericResponse : genericMapResponse .entrySet ()) {
281285 Map <String , Object > extensions = genericResponse .getValue ().getExtensions ();
282286 Collection <String > genericExceptions = (Collection <String >) extensions .get (EXTENSION_EXCEPTION_CLASSES );
283287 for (Class <?> declaredException : handlerMethod .getMethod ().getExceptionTypes ()) {
@@ -305,13 +309,13 @@ private Map<String, ApiResponse> filterAndEnrichGenericMapResponseByDeclarations
305309 */
306310 public void buildGenericResponse (Components components , Map <String , Object > findControllerAdvice , Locale locale ) {
307311 // ControllerAdvice
308- for (Map . Entry <String , Object > entry : findControllerAdvice .entrySet ()) {
312+ for (Entry <String , Object > entry : findControllerAdvice .entrySet ()) {
309313 List <Method > methods = new ArrayList <>();
310314 Object controllerAdvice = entry .getValue ();
311315 // get all methods with annotation @ExceptionHandler
312316 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 );
315319 ControllerAdviceInfo controllerAdviceInfo = new ControllerAdviceInfo (controllerAdvice );
316320 Arrays .stream (ReflectionUtils .getAllDeclaredMethods (objClz ))
317321 .filter (m -> m .isAnnotationPresent (ExceptionHandler .class )
@@ -422,11 +426,12 @@ private Map<String, ApiResponse> computeResponseFromDoc(Components components, M
422426 */
423427 private void buildGenericApiResponses (Components components , MethodParameter methodParameter , ApiResponses apiResponsesOp ,
424428 MethodAttributes methodAttributes ) {
429+ ApiResponse apiResponse = null ;
425430 if (!CollectionUtils .isEmpty (apiResponsesOp )) {
426431 // API Responses at operation and @ApiResponse annotation
427- for (Map . Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
432+ for (Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
428433 String httpCode = entry .getKey ();
429- ApiResponse apiResponse = entry .getValue ();
434+ apiResponse = entry .getValue ();
430435 buildApiResponses (components , methodParameter , apiResponsesOp , methodAttributes , httpCode , apiResponse , true );
431436 }
432437 }
@@ -435,11 +440,21 @@ private void buildGenericApiResponses(Components components, MethodParameter met
435440 // available
436441 String httpCode = evaluateResponseStatus (methodParameter .getMethod (), Objects .requireNonNull (methodParameter .getMethod ()).getClass (), true );
437442 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 )
439444 : new ApiResponse ();
440445 buildApiResponses (components , methodParameter , apiResponsesOp , methodAttributes , httpCode , apiResponse , true );
441446 }
442447 }
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+ }
443458 }
444459
445460 /**
@@ -455,7 +470,7 @@ private void buildApiResponses(Components components, MethodParameter methodPara
455470 Map <String , ApiResponse > genericMapResponse = methodAttributes .getGenericMapResponse ();
456471 if (!CollectionUtils .isEmpty (apiResponsesOp ) && apiResponsesOp .size () > genericMapResponse .size ()) {
457472 // API Responses at operation and @ApiResponse annotation
458- for (Map . Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
473+ for (Entry <String , ApiResponse > entry : apiResponsesOp .entrySet ()) {
459474 String httpCode = entry .getKey ();
460475 boolean methodAttributesCondition = !methodAttributes .isMethodOverloaded () || (methodAttributes .isMethodOverloaded () && isValidHttpCode (httpCode , methodParameter ));
461476 if (!genericMapResponse .containsKey (httpCode ) && methodAttributesCondition ) {
@@ -693,8 +708,8 @@ private Map<String, ApiResponse> getGenericMapResponse(HandlerMethod handlerMeth
693708 List <ControllerAdviceInfo > controllerAdviceInfosInThisBean = localExceptionHandlers .stream ()
694709 .filter (controllerInfo -> {
695710 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 ());
698713 return beanType .equals (objClz );
699714 })
700715 .toList ();
@@ -772,7 +787,7 @@ private boolean isValidHttpCode(String httpCode, MethodParameter methodParameter
772787 final io .swagger .v3 .oas .annotations .Operation apiOperation = AnnotatedElementUtils .findMergedAnnotation (method ,
773788 io .swagger .v3 .oas .annotations .Operation .class );
774789 if (apiOperation != null ) {
775- responseSet = new HashSet <>(Arrays . asList (apiOperation .responses ()));
790+ responseSet = new HashSet <>(asList (apiOperation .responses ()));
776791 if (isHttpCodePresent (httpCode , responseSet ))
777792 result = true ;
778793 }
0 commit comments