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

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

Closed
spring-projects-issues opened this issue Jan 12, 2016 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) 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 Jan 12, 2016

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:

@spring-projects-issues
Copy link
Collaborator Author

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!

@spring-projects-issues
Copy link
Collaborator Author

AVINABA GHOSH HAJRA commented

Is there any update regarding this issue?

@spring-projects-issues
Copy link
Collaborator Author

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

@spring-projects-issues
Copy link
Collaborator Author

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!

@spring-projects-issues
Copy link
Collaborator Author

AVINABA GHOSH HAJRA commented

Thanks guys. This issue can be closed

@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: core Issues in core modules (aop, beans, core, context, expression) 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: core Issues in core modules (aop, beans, core, context, expression) 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