Skip to content

Regression: @RequestBody(required = false) not respected [SPR-13417] #17996

Closed

Description

@spring-projects-issues

Jean-Baptiste Nizet opened SPR-13417 and commented

I'm migrating from 4.1.2 to 4.2.1, and am observing a regression.
A RestController method annotated with @RequestMapping(method = POST) and having an argument annotated with @RequestBody(required = false) causes a 415 response to be sent back by Spring MVC if the request doesn't have a body.

This used to work. And the behavior contradicts the javadoc of RequestBody.
BTW, a similar bug used to exist, and had been fixed: #14575.


Affects: 4.2.1

Issue Links:

Referenced from: commits spring-attic/spring-framework-issues@6bdf345, spring-attic/spring-framework-issues@fb75c0d

Activity

  1. spring-projects-issues commented on Sep 2, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Brian Clozel commented

    Hi Jean-Baptiste Nizet

    I couldn't reproduce this issue - this may not be strictly related to the @RequestBody mapping (otherwise we'd probably see an HTTP 400 error), but rather a serialization/deserialization or a content negotiation issue.

    Could you give me some clues about your setup?
    What is the expected media type? Which serialization technology are you using (Jackson), do you have a specific setup?

    I've been trying to reproduce this with a repro project. Let me know how I can amend this project to reproduce this.

  2. spring-projects-issues commented on Sep 2, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Jean-Baptiste Nizet commented

    I'll have a look to the repro project and try to reproduce and submit a PR, but I can tell you more about my use-case:

    • The issue disappears if there is a content-type header in the request (set to application/json). But the fact is that sending an HTTP request using angular's $http service, without body, doesn't set a content-type. And that seems logical, since there is no content
    • The rest controller looks like the following:
    @RestController
    @RequestMapping("/api/quizzes/publications/{quizzRevisionId}")
    @Transactional
    public class QuizzPublicationController {
    
        // ...
    
        @RequestMapping(method = POST)
        @Authenticated
        @ResponseStatus(HttpStatus.CREATED)
        public QuizzWithStatisticsDTO publish(@PathVariable("quizzRevisionId") Long quizzRevisionId,
                                              @RequestBody(required = false) @Validated QuizzPublicationCommandDTO command) {
            // ...
        }
    }
  3. spring-projects-issues commented on Sep 2, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Jean-Baptiste Nizet commented

    Pull request sent, with a unit test reproducing the problem: spring-attic/spring-framework-issues#105

  4. spring-projects-issues commented on Sep 2, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Brian Clozel commented

    Thanks Jean-Baptiste Nizet, I'll look into it.

  5. spring-projects-issues commented on Sep 3, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Brian Clozel commented

    This is now fixed.
    Because we now support ResponseBodyAdvice for empty bodies, we need to clearly deal with "no body, no content-type" cases separately. I've verified this with our repro project and a local SNAPSHOT.

    This should show up in our SNAPSHOTs in as soon as this build is done.

    FYI, Spring Boot 1.3 RC1 should ship with Spring Framework 4.2.2 (this fix included).

    Thanks for the report!

  6. spring-projects-issues commented on Sep 3, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Jean-Baptiste Nizet commented

    Thanks for the fix, Brian.

  7. spring-projects-issues commented on Jan 3, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Brian Clozel commented

    Hey Sam,

    I've received a notification for this comment as well - this may not be a regression per se, since a unit test was added along the fix for this particular issue.
    I've asked for more details on the issue itself.

    Thanks!

  8. added
    type: bug
    in: web
    on Jan 11, 2019
  9. added this to the 4.2.2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions