【问题标题】:Control of swagger file default propertyswagger 文件默认属性的控制
【发布时间】: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
  • 我也测试了其他非必需参数(字符串/整数),但似乎没有注释可用于控制在没有此参数时应用的默认值。

标签: java swagger


【解决方案1】:

swagger-core 1.5 不支持模型属性的默认值 - 但它应该在 2.x 中使用@Schema 注解得到支持:

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

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

更多信息:

请注意,swagger-core 2.x 生成 OpenAPI 3.0 定义,而不是当前示例中的 OpenAPI 2.0。

【讨论】:

    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 2013-12-21
    • 2015-01-03
    • 2011-01-28
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多