Skip to content

Spring Controller is not able to detect empty string ("") as a string object in RequestBody [SPR-13861] #18434

Closed

Description

@spring-projects-issues

AVINABA GHOSH HAJRA opened SPR-13861 and commented

Problem Statement :
Spring Controller is not able to detect empty string ("") as a string object in RequestBody.Till Spring 3.2 Spring Controller is able to detect empty string ("") as a string. But now we upgrade our spring version from 3.2 to 4.2.And in Spring 4.2 we are facing this issue.Our API gives HTTP status 400 as response instead of processing the empty string in RequestBody
Example :
Controller Method definition:

   @MethodInfo(comments = DocumentationConstants.submitAttemptComments, date = "Mar 31 2014", summary = "Submits an Activity", sequence = 5 )
@RequestMapping(value ="/submit", method=RequestMethod.POST,produces="application/json")
@ResponseBody
public ResponseEntity<String> submitAttempt(@RequestBody String eaid)

And JUNIT ::

   @Test
public void testCase() throws Exception {
                 String eaid = "";
                                                                                 when(securityServiceMock.validateEAID(anyString())).thenReturn(false);	
     Map<String, String> jsonMap = new HashMap<String, String>();
     jsonMap.put("success", "true");
     when(assignmentServiceMock.getAjsonInfo(anyString(), anyBoolean())).thenReturn(new JSONObject(jsonMap));
     MvcResult result = this.mockMvcAssgnmntMock.perform(post("/caa/activity/submit")
               .content(eaid)
               .accept(MediaType.parseMediaType("application/json;charset=utf-8")))
               .andExpect(status().isNotAcceptable()).andReturn();
     _logger.info("Result received:" + result.getResponse().getContentAsString());
     assertEquals("{\"message\":\"EAID is acceptable\",\"status\":\"200\"}", result.getResponse().getContentAsString());
}

is giving error ::
java.lang.AssertionError: Status expected:<200> but was:<400>


Affects: 4.2 GA

Issue Links:

Activity

  1. spring-projects-issues commented on Jan 12, 2016

    @spring-projects-issues
    CollaboratorAuthor

    AVINABA GHOSH HAJRA commented

    We can use public ResponseEntity submitAttempt(@RequestBody(required = false) String eaid).
    But this leads to a code change due to spring version upgrade. And this is not recomended.
    But if we change the input from a empty string to a blank space (from "" to " ") then it works properly.
    Thanks!

  2. spring-projects-issues commented on Jan 18, 2016

    @spring-projects-issues
    CollaboratorAuthor

    AVINABA GHOSH HAJRA commented

    Is there any update regarding this issue?

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

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    As far as I can see, this is intentional in that we fixed the actual meaning of the 'required' flag there. If required=true lets empty bodies through, what's the meaning of required=false?

    So from my perspective, you'll have to add required=false, and there's nothing wrong with that. Technically, you always expected required=false behavior but since Spring did accidentally not enforce the meaning of the 'required' flag, your code nevertheless worked even with required=false omitted. In other words, semantically, you should have added required=false in your Spring 3.2 based code already.

    Juergen

  4. spring-projects-issues commented on Jan 21, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Brian Clozel commented

    Indeed, this is the expected behavior for this feature and we've fixed several bugs lately wrt JSON deserialization and @RequestBody. Let me know if you need further help with this.

    Thanks Juergen!

  5. spring-projects-issues commented on Jan 28, 2016

    @spring-projects-issues
    CollaboratorAuthor

    AVINABA GHOSH HAJRA commented

    Thanks guys. This issue can be closed

  6. added
    type: bug
    status: declined
    in: core
    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: corestatus: declined

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions