【问题标题】:Representing ISO 8601 year-month dates documentation with Swagger使用 Swagger 表示 ISO 8601 年月日期文档
【发布时间】:2021-08-04 23:23:53
【问题描述】:

如何让 Swagger 核心生成带有以字符串表示的年月字段的 openapi.json 文档?

在代码中我已经尝试过@JsonFormat 和@JsonbDateFormat。

返回的实体以正确的格式正确表示为字符串,但文档始终生成为对象。

环境:

  • io.swagger.core.v3.swagger-jaxrs2 2.1.9
  • 野蝇 21.0.2
  • java 11

这里是一个sn-p:

@JsonbDateFormat(value = "uuuu-MM") // or "yyyy-MM", or @JsonFormat(shape = Shape.STRING, pattern = "uuuu-MM")
private YearMonth reference;

实体返回:

{
    "reference": "2020-11",
}

生成的文档:

{
  "openapi" : "3.0.1",
  ...
  "components" : {
    "schemas" : {
      "Entity" : {
        "type" : "object",
        "properties" : {
          "reference" : {
            "type" : "object",
            "properties" : {
              "year" : {
                "type" : "integer",
                "format" : "int32"
              },
              "month" : {
                "type" : "string",
                "enum" : [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ]
              },
              "monthValue" : {
                "type" : "integer",
                "format" : "int32"
              },
              "leapYear" : {
                "type" : "boolean"
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: java swagger wildfly openapi resteasy


    【解决方案1】:

    显然 Swagger 核心不使用 @JsonFormat 或 @JsonbDateFormat 作为格式化模板的源,它仅使用属性类型,但它没有默认的 YearMonth 类型。

    因此,可以使用 Swagger Annotation @Schema 定义数据表示。

    在这种特殊情况下:@Schema(type = "string", format = "yearmonth", example = "2020-07")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2013-05-14
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多