Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control of swagger file default property

Tags:

java

swagger

Using swagger-core/swagger-annotations in version 1.5.16

Have problem to control the default property in swagger file for my data model. Have a POJO that defines the input JSON object for HTTP POST:

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(description = "Parameters to use when creating my object")
public class MyPrototype {

    @JsonProperty(value = "name")
    @ApiModelProperty(value = "Name of this entity", required = true, example = "MyAccountGroup1")
    protected String name;
    @JsonProperty(value = "flag")
    @ApiModelProperty(value = "This flag is used for...")
    protected Boolean statusReportRequestSuppressed;

}

My problem is that the generated swagger file will contain a default property for parameter "flag" and set this to 'false' but there seems to be no way to include a default property for parameter "name".

Is there any support given the swagger-core/annotations version to either

  • achieve to include default property for non-boolean values or
  • suppress so that boolean values don't get the default.

I would like to achieve that swagger file get default values on all or nothing.

Any tips welcome

My outcome swagger looks like this:

"MyPrototype" : {
  "type" : "object",
  "required" : [ "name" ],
  "properties" : {
    "name" : {
      "type" : "string",
      "example" : "MyAccountGroup1",
      "description" : "Name of this entity"
    },
    "flag" : {
      "type" : "boolean",
      "description" : "This flag is used for...",
      "default" : false
    }
  },
  "description" : "Parameters to use when creating my object"
}
like image 444
Marcus Lind Avatar asked Sep 14 '25 14:09

Marcus Lind


1 Answers

swagger-core 1.5 does not support default values for model properties - but it should be supported in 2.x using the @Schema annotation:

import io.swagger.v3.oas.annotations.media;

@Schema(value = "Some optional property", defaultValue = "foo")
protected String optionalProperty;

More info:

  • @Schema annotation
  • Javadocs

Note that swagger-core 2.x produces OpenAPI 3.0 definitions, not OpenAPI 2.0 as in your current example.

like image 142
Helen Avatar answered Sep 17 '25 03:09

Helen