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

Body empty and required ignored in POST of simple Boolean #566

Closed
PierreHa opened this issue Sep 10, 2014 · 7 comments
Closed

Body empty and required ignored in POST of simple Boolean #566

PierreHa opened this issue Sep 10, 2014 · 7 comments

Comments

@PierreHa
Copy link

When posting just a Boolean in the body, there is no body sent, it is empty! Also the check is not done before sending as it is required (by default), no need to select value in the correct dropdown.

We are running latest UI code and swagger-springmvc 0.8.8
Example code for the service:

import com.wordnik.swagger.annotations.*;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@Api(value = "Test",
description = "Set an boolean value",
position = 1)
@RestController
public class BooleanController {

@ApiOperation(value = "Set a boolean value", response = Boolean.class)
@ApiResponses(
        {
                @ApiResponse(code = 200, message = "Boolean updated")
        }
)
@RequestMapping(method = RequestMethod.POST, value = "/booleanPost", consumes = MediaType.APPLICATION_JSON_VALUE)
public Boolean setBooleanValue(
        @ApiParam(value = "The value of the boolean to set")
        @RequestBody(required = true)
        boolean boolValue,
        HttpServletResponse servletResponse) {
    return boolValue;
}

}

@PierreHa
Copy link
Author

You might instead want the answer from http://localhost:8080//api-docs/default/test to debug
{"apiVersion": "1.0", "swaggerVersion": "1.2", "basePath": "/", "resourcePath": "/booleanPost", "consumes": ["application/json"], "apis": [
{
"path": "/booleanPost",
"description": "setBooleanValue",
"operations": [
{
"method": "POST",
"summary": "Set a boolean value",
"notes": "setBooleanValue",
"type": "boolean",
"nickname": "setBooleanValue",
"consumes": ["application/json"],
"parameters": [
{
"name": "boolValue",
"description": "The value of the boolean to set",
"defaultValue": "",
"required": false,
"type": "boolean",
"paramType": "body",
"allowMultiple": false
}
],
"responseMessages": [
{
"code": 201,
"message": "Created"
},
{
"code": 200,
"message": "Boolean updated",
"responseModel": "boolean"
},
{
"code": 403,
"message": "Forbidden"
},
{
"code": 401,
"message": "Unauthorized"
},
{
"code": 404,
"message": "Not Found"
}
],
"deprecated": "false"
}
]
}
]}

@webron
Copy link
Contributor

webron commented Sep 10, 2014

While there may still be a bug in Swagger-UI, there's also a bug with swagger-springmvc. The name of the field has to be "body" if it is a body parameter. You should probably open an issue on that on swagger-springmvc.

Another problem is that you say the operation consumes application/json but using a primitive (boolean in this case) can't be represented properly in JSON so that just won't work.

@PierreHa
Copy link
Author

I will file one at swagger-springmvc then, What do you mean by it cant represent boolean?
From http://www.ietf.org/rfc/rfc4627.txt
JSON can represent four primitive types (strings, numbers, booleans,
and null) and two structured types (objects and arrays).

@webron
Copy link
Contributor

webron commented Sep 10, 2014

You can represent booleans, but they have to be within a structure.

true is not valid json.

{
  "myProperty" : true
}

That's a valid json.
You need to represent it as part of an object, not a standalone primitive.

@PierreHa
Copy link
Author

Ok Thanks, didn't think of that.
If I wrap it, the required is still ignored, but it will sen an object if I add one!

@webron
Copy link
Contributor

webron commented Sep 10, 2014

As for the required, if you look at the swagger specification you attached, you'll see it says the body parameter is "required": false. It looks like swagger-springmvc doesn't parse @RequestBody(required = true). Try modifying the @ApiParam to this:

        @ApiParam(value = "The value of the boolean to set", required = true)

and see if the output changes.

@PierreHa
Copy link
Author

Thanks again
I had mixed up the Swagger and Spring requiered ( @RequestBody(required = true))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants