spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.64k stars 38.14k forks source link

WebFlux returns 406 instead of RFC 7807 response for subtype of ProblemDetail #30533

Closed vijjayy81 closed 1 year ago

vijjayy81 commented 1 year ago

The following line causing 406 Error while using any subtype of ProblemDetail class.

        // For ProblemDetail, fall back on RFC 7807 format
        if (bestMediaType == null && elementType.toClass().equals(ProblemDetail.class)) {
            bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType), this.problemMediaTypes);
        }

(https://github.com/spring-projects/spring-framework/blob/main/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java#L178)

The issue does not exists in Servlet as it checks the error body type is assignable of ProblemDetail class.

            // For ProblemDetail, fall back on RFC 7807 format
            if (compatibleMediaTypes.isEmpty() && ProblemDetail.class.isAssignableFrom(valueType)) {
                determineCompatibleMediaTypes(this.problemMediaTypes, producibleTypes, compatibleMediaTypes);
            }

https://github.com/spring-projects/spring-framework/blob/main/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java#L247

rstoyanchev commented 1 year ago

Thanks for the report and analysis.