Skip to content
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

MediaType parameters in the "consumes" condition of @RequestMapping are not considered for matching #28024

Closed
thake opened this issue Feb 10, 2022 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@thake
Copy link

thake commented Feb 10, 2022

This issue was opened based on the discussion in #27999.

Given the following controller:

@RestController
@RequestMapping("/hal-documents")
class MyController {
    @PostMapping(
        consumes = ["""application/hal+json;profile="my-resource-v1""""],
        produces = ["""application/hal+json;profile="my-resource-v1""""]
    )
    fun postVersion1(@RequestBody request : String) = "version-1"

    @PostMapping(
        consumes = ["""application/hal+json;profile="my-resource-v2""""],
        produces = ["""application/hal+json;profile="my-resource-v2""""]
    )
    fun postVersion2(@RequestBody request : String) = "version-2";
}

A request that provides a request body with the content type application/hal+json;profile="my-resource-v2" is being routed to postVersion1 but should be routed to postVersion2.

Even worse, if the consumes media type only differs in media type parameters and the handler methods can't be ranked by produces an Ambiguous handler methods mapped for ... error will be thrown leading to a 500er.

Example

Controller:

@RestController
@RequestMapping("/hal-documents")
class MyController {
    @PostMapping(
        consumes = ["""application/hal+json;profile="my-resource-v1""""]
    )
    fun postVersion1(@RequestBody request : String) = "version-1"

    @PostMapping(
        consumes = ["""application/hal+json;profile="my-resource-v2""""]
    )
    fun postVersion2(@RequestBody request : String) = "version-2";
}

Request:

POST http://localhost:8080/hal-documents
Content-Type: application/hal+json;profile="my-resource-v2"

{
  "my content" : "blub"
}

Response:

HTTP/1.1 500 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 10 Feb 2022 06:51:58 GMT
Connection: close

{
  "timestamp": "2022-02-10T06:51:58.921+00:00",
  "status": 500,
  "error": "Internal Server Error",
  "trace": "java.lang.IllegalStateException: Ambiguous handler methods mapped for '/hal-documents': {...}",
  "path": "/hal-documents"
}

Looking into the code, it seems like consumes and produces are treated differently in ProducesRequestCondition and ConsumesRequestCondition when it comes to media type parameters.

Affects: 5.3.15

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 10, 2022
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Feb 10, 2022
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Feb 14, 2022

We can align consumes with the produces condition, along the lines of 8dc535c, such that if a media type parameter is explicitly declared in the mapping and the same parameter is also present in the Content-Type header, then the two must match.

@rstoyanchev rstoyanchev added this to the 6.0.0-M3 milestone Feb 14, 2022
@rstoyanchev rstoyanchev self-assigned this Feb 14, 2022
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 14, 2022
@rstoyanchev rstoyanchev modified the milestones: 6.0.0-M3, 6.0.0-M4 Mar 16, 2022
@rstoyanchev rstoyanchev changed the title Content Negotiation with media type parameters: Parameters of consumes are not considered for ranking handler methods MediaType parameters in the "consumes" condition of @RequestMapping are not considered for matching May 11, 2022
@rstoyanchev
Copy link
Contributor

Fixed in f0e23b6 but commit message references a different issue by accident.

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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants