swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

How to display default value 0 on swagger-ui

Open zon999 opened this issue 11 years ago • 9 comments

Hi all, My result on swagger is: { "name": "string", "wareHouseID": "Integer", "company": "string", "fullname": "string", "address": "string", "parentID": "Integer" }

but I need "wareHouseID": 0, "parentID": 0. My question: How to set default values 0. My code: private int wareHouseID; private int parentID; private String name; private String fullname; private String company; private String address;

public Warehouse() {
}

@ApiModelProperty(position = 1,required = true)
public int getWareHouseID() {
    return wareHouseID;
}

public void setWareHouseID(int wareHouseID) {
    this.wareHouseID = wareHouseID;
}

@ApiModelProperty(position = 2,required = true)
public int getParentID() {
    return parentID;
}

Thanks all.

zon999 avatar Oct 08 '15 06:10 zon999

May I know which generator are you using? Java? springmvc?

If I understand correctly, you want to set default value (e.g. 0) to model property?

wing328 avatar Oct 08 '15 08:10 wing328

Hi @wing328 i using java and jaxrs

zon999 avatar Oct 08 '15 09:10 zon999

Do you mean you generate the swagger.json from annotation in your code (Java, JAX-RS)?

If you already have a swagger.json to describe your REST API, do the properties have the default attribute? (ref: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#fixed-fields-7)

wing328 avatar Oct 08 '15 09:10 wing328

Can you give me an example :)

zon999 avatar Oct 08 '15 10:10 zon999

Here you go:

    "Tag": {
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64",
          "default": 0
        },
        "name": {
          "type": "string",
          "default": "unknown"
        }
      }
    }

wing328 avatar Oct 08 '15 11:10 wing328

Oh, your mean is i have to define swagger.json in my project?

zon999 avatar Oct 08 '15 12:10 zon999

How's your swagger.json generated? using annotation in the code?

wing328 avatar Oct 08 '15 13:10 wing328

My code: @POST @Path(Constants.NEW) @ApiOperation(value = "Create a new warehouse", notes = "Creates a new warehouse", response = Warehouse.class) @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid Id supplied", response = Warehouse.class), @ApiResponse(code = 404, message = "Warehouse not found", response = Warehouse.class) }) @ApiImplicitParams({ @ApiImplicitParam(name = "warehouse", required = true, dataType = "Warehouse", paramType = "body", allowableValues = MediaType.APPLICATION_JSON) }) public @ResponseBody Response createNewWarehouse( @ApiParam(name = "warehouse", required = true) @RequestParam(required = true) Warehouse wh) throws NotFoundException { if(warehouseService.createWarehouse(wh).equals(Constants.SUCCESS)){ String[] errorMessage = new String[]{}; String[] message = new String[]{Constants.SUCCESS}; Object[] rows = new Object[]{wh}; return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.SUCCESS, errorMessage, rows, message)).build(); }else if(warehouseService.createWarehouse(wh).equals(Constants.DUPPLICATE)){ String[] errorMessage = new String[]{Constants.DUPPLICATE}; String[] message = new String[]{}; Object[] rows = new Object[]{}; return Response.status(ApiResponseMessage.DUPPLICATE).entity(new ApiResponseMessage(ApiResponseMessage.DUPPLICATE,errorMessage,rows,message)).build(); }else{ String[] errorMessage = new String[]{Constants.FAIL}; String[] message = new String[]{}; Object[] rows = new Object[]{}; return Response.status(ApiResponseMessage.NO_CONTENT).entity(new ApiResponseMessage(ApiResponseMessage.NO_CONTENT,errorMessage,rows,message)).build(); } }

I don't have swagger.json :(

zon999 avatar Oct 08 '15 14:10 zon999

i don't see swagger.json here: https://github.com/swagger-api/swagger-codegen/tree/master/samples/server/petstore/jaxrs

zon999 avatar Oct 08 '15 14:10 zon999