【发布时间】:2018-04-13 13:49:54
【问题描述】:
在 1.5.16 版本中使用 swagger-core/swagger-annotations
在为我的数据模型控制 swagger 文件中的默认属性时遇到问题。有一个 POJO,它定义了 HTTP POST 的输入 JSON 对象:
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;
}
我的问题是生成的 swagger 文件将包含参数“flag”的默认属性并将其设置为“false”,但似乎无法包含参数“name”的默认属性。
对于 swagger-core/annotations 版本是否有任何支持
- 实现包含非布尔值的默认属性 或
- 抑制以使布尔值不会获得默认值。
我想实现那个 swagger 文件全部或全部获取默认值。
欢迎任何提示
我的结果大摇大摆是这样的:
"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"
}
【问题讨论】:
-
required参数和属性不应具有default值 - 请参阅 Using default with required parameters for OpenAPI。 -
我也测试了其他非必需参数(字符串/整数),但似乎没有注释可用于控制在没有此参数时应用的默认值。