Skip to content

Accept header handling is inconsistent [SPR-16750] #21291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Apr 20, 2018 · 3 comments
Closed

Accept header handling is inconsistent [SPR-16750] #21291

spring-projects-issues opened this issue Apr 20, 2018 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 20, 2018

Petar Cvitanusic opened SPR-16750 and commented

The fix for #20798 is breaking a bunch of our tests. I do agree with the fix in #20798 that Spring returns a 200 when the base is the same. For example Accepting "application/json" but returning "application/msword" is totally valid.

We have a controller method that returns attachments that can be different content types. In some cases spring throws a 406, in others it produces a 200.

The controller method does not specify what it produces, rather we specify the Content-Type header for the ResponseEntity:

@RequestMapping(method = RequestMethod.GET, value = "...")
public ResponseEntity<...> getAttachment(HttpServletRequest request) {
    
}

 

Scenario:

  1. Accept header is "application/json"
  2. Content type returned is "image/png"
  3. Spring throws a 406. This is correct.

 

  1. Accept header is "application/json"
  2. Content type returned is "image/jpeg"
  3. Spring returns a 200. This seems wrong to me.

Affects: 5.0.3, 5.0.4, 5.0.5

Reference URL: #20798

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Apr 23, 2018

Brian Clozel commented

Hi Petar Cvitanusic

The fix in #20798 can be summarized like this: adding consumes/produces attribute to the mapping annotation means you wish Spring MVC to assist you wit content negotiation. If nothing is provided there, it means the developers decide to take full control and Spring MVC should not be in the business of overriding the choice made there. If a Content-Type is provided in the response entity, we should use that information to select the right message converter, but we should not override the decision made by the controller.

Given the use case you're describing, you should probably ask Spring MVC for the media types in the Accept header and decide yourself how your endpoint should behave. The MediaType class offers a lot of facilities for that, and you can throw HttpMediaTypeNotAcceptableException manually when it is required by your application.

If anything, it seems you're disagreeing with #20720 (and not #20798 which is about a side-effect of that first fix). If this is the case, I'm afraid this change has been done on purpose and this behavior is now by design. Let me know if I've missed anything in my analysis.

@spring-projects-issues
Copy link
Collaborator Author

Petar Cvitanusic commented

Thanks for the explanation Brian,

It does look like we missed a MediaType in one of our custom message converters handling attachments which explains the discrepancy I mentioned above between jpeg vs png.

The explanation makes sense to me, I think we can close this bug.

@spring-projects-issues
Copy link
Collaborator Author

Brian Clozel commented

Alright Petar Cvitanusic, thanks for your feedback!

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants