控制swagger文件默认属性 [英] Control of swagger file default property

查看:77
本文介绍了控制swagger文件默认属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 1.5.16 版本中使用 swagger-core/swagger-annotations

Using swagger-core/swagger-annotations in version 1.5.16

在为我的数据模型控制 swagger 文件中的默认属性时遇到问题.有一个为 HTTP POST 定义输入 JSON 对象的 POJO:

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;

}

我的问题是生成的 swagger 文件将包含参数flag"的默认属性并将其设置为false",但似乎无法包含参数name"的默认属性.

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".

鉴于 swagger-core/annotations 版本是否有任何支持

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

  • 实现包含非布尔值的默认属性或
  • 抑制以便布尔值不会获得默认值.

我想实现该 swagger 文件在全部或全部上获得默认值.

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

欢迎任何提示

我的结果招摇如下:

"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"
}

推荐答案

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

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;

更多信息:

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

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

这篇关于控制swagger文件默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆