springfox: DefaultValue missing for @RequestBody in swagger ui 2.4

After moving from com.mangofactory swagger v1.0.2 to springfox swagger v2.4, defaultValue of @ApiParam for a @RequestBody of a POST or PUT request is missing in swagger ui.

For the following method in my controller

@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "Create a new Project", notes = "Create a new Project.")
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    @ApiResponses(value = { @ApiResponse(code = 400, message = "Bad request") })
    public Project create(@RequestBody @ApiParam(value = "Project request object", defaultValue = "{"
            + "\n\"name\":\"Test-11\" , " + "\n\"description\":\"test Project11\" ,"
            + "\n\"from\":{\n\"source\":\"XYZ\" , " + "\n\"sourceId\":\"123\" ," + "\n\"stack\":\"STACK\" , "
            + "} , " + "\n\"to\": {\n\"source\":\"ABC\" , " + "\n\"sourceIdId\":\"123\" ,"
            + "\n\"stack\":\"STACK\" , " + "} , " + "\n\"usecase\":\"one\" , "
            + "\n\"entities\":[\"Test\"] , " + "\n\"offeringId\" :0 , "
            + "\n\"configurationProperties\" :[{\"name\" :\"testProperty11\" , "
            + "\n\"value\" : \"test\" , " + "\n\"id\" : 1 }," + "\n\"name\" : \"newProperty-11\" , "
            + "\n\"value\" :\"xyz11\" , " + "\n\"id\" : null}]," + "\n \"idType\" : \"CUSTOMER\" , "
            + "\n \"transformer\" : null , " + "\n \"status\" : null }") Project project)
            throws BaseHttpException {

Below swagger.json is generated which does not have a defaultValue

     {
            "in": "body",
            "name": "project",
            "description": "Project request object",
            "required": false,
            "schema": {
              "$ref": "#/definitions/Project"
      }

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 25 (11 by maintainers)

Most upvoted comments

Hi Everyone, Springfox provides @ApiModelProperty which can be used to set the default values for request body parameters. - @ApiModelProperty(required=true,value=“deviceToken”,example=“123”)

Instead of using a JSON return object, if you return a POJO DTO and annotate the POJO fields with @ApiModelProperty ,set the example attribute, swagger ui and yaml will contain the default values

For example: ` public org.json.simple.JSONObject simplePost2(@RequestBody POJO pojo,@ApiParam(name =“securityToken”,value = “securityToken”,defaultValue = “7d0fcbca-d6f0-4f29-b19f-2f8fe4e0f730” )@RequestHeader String securityToken, @ApiParam(name =“userId”,value = “userId”,defaultValue = “1” )@RequestHeader String userId) {

	//JSONObject response = new JSONObject();
	return json;

`

POJO Class: ` @ApiModel(value = “pojo”, description = “Model description”) public class POJO{

@ApiModelProperty(required=true,value="id",example="123456")
    private String id;

@ApiModelProperty(required=true,value="deviceToken",example="123")
private String deviceToken;

` Swagger.yaml : will contain the default values under definitions

definitions:
pojo:
     type: object
    required:
      - id
      - deviceToken
    properties:
      id:
        type: string
        example: '123456'
        description: id
      deviceToken:
        type: string
        example: '123'
        description: deviceToken
    description: Model description


**Pom.xml**

`               <dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.4.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.4.0</version>
		</dependency>`

Why is this issue closed? I don’t think there has been a resolution to 'nsaxena’s issue. We migrated from Swagger 2.4.0 to 2.5.0 and @RequestBody annotated elements, in POST and PUT endpoints are not detected nor shown in Swagger as input parameters. Why is that? Any configuration should be added or what? @dilipkrish Please assist

Ok, right, its the same with @ApiParam. I actually looked into the swagger spec and when the parameter in question is a body parameter (“in”: “body”) the default should appear in the schema object, under the “definitions” property of the spec, instead of the “parameters” property.

So the default should be returned like this:

"parameters": [
  {
    "in": "body",
    "name": "request",
    "description": "Request object",
    "required": true,
    "schema": {
      "$ref": "#/definitions/Request"
    }
  }
]

And then on the “definitions” property:

"definitions": {
  "Request": {
    "default": "the default value",
    "example": "an example",
    "properties": { ... }
  },
}

See also: