spring-framework: Prefer problem detail media type independent of ordering in Accept header

While replacing a custom implementation of Problem Details by the new one that came with Spring 6 I was puzzled by how content negotiation is implemented. I’m currently using Spring Boot 3.0.0 with Spring 6.0.2.

I’m getting a Content-Type application/problem+json as expected when sending no accept header or Accept: */* or Accept: application/problem+json, application/json.

I also expected to receive a Problem Details JSON with Content-Type application/problem+json when sending Accept: application/json. But that might be correct - it doesn’t seem to be clearly defined by RFC 7807.

Clearly a bug, seems that I’m still getting Content-Type application/json when sending Accept: application/json, application/problem+json. See also:

Message converters and encoders indicate a preference for application/problem+json when ProblemType is serialized. This ensures application/problem+json is preferred when the client is flexible or has no preference.

from: https://github.com/spring-projects/spring-framework/issues/28189#issuecomment-1121378986

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for your feedback and fix @rstoyanchev! I gave 6.0.5-SNAPSHOT a try and now almost all my tests are green.

Now waiting for related https://github.com/spring-projects/spring-boot/issues/33716 to be fixed to solve the others. 😉

This is getting quite complex. I think we should separate issues.

  1. This issue was/is about:
  • Response body has to match the Content-Type. If the response body contains a Problem Details document the Content-Type has to be application/problem+json for JSON documents or application/problem+xml for XML documents – exclusively. This is what the RFC says and why I called the current Spring implementation “broken”.
  • As @ueberfuhr mentioned the opposite is also true: A non-Problem Details response MUST NOT neither use a Content-Type header value of application/problem+json nor application/problem+xml.
  • We should not expect the client to specify an Accept header of application/problem+json or application/problem+xml in order to send a Problem Details report. In practice this almost never happens. We already explicitly enabled Problem Details use via spring.mvc.problemdetails.enabled.
  • In order do decide between JSON/XML Problem Details we should default on JSON and only send application/problem+xml if the client requested a XML document (Accept header value of */*xml*).
  1. The Problem Details XML format is invalid per RFC I created https://github.com/spring-projects/spring-framework/issues/29927 for that.

  2. @ueberfuhr raised several concerns/ideas/questions regarding Content Negotiation for implicit/explicit error responses. I’d suggest you also create a separate issue for that and link it to this one.