kotlin - 为什么 Swagger 不检测可选的 JSON 属性?

标签 kotlin jackson openapi javalin

我有以下类(class):

import com.fasterxml.jackson.annotation.JsonProperty
import org.joda.time.DateTime
import org.joda.time.DateTimeZone

data class Entity(
        val email: String,
        val name: String,
        val birthDate: DateTime,
        @JsonProperty(required = false) val gender: Gender? = null,
        @JsonProperty(required = false) val country: String? = null,
        val locale: String,
        val disabled: Boolean = false,
        @JsonProperty(required = false) val createdAt: DateTime = DateTime(DateTimeZone.UTC),
        val role: Role,
        val entityTypeId: Long,
        val entityTypeAttributes: MutableMap<String, Any> = HashMap(),
        val medicalSpecialityId: Long? = null,
        val id: Long? = null
)

有些属性不是必需的,因为它们要么可以为 null(性别、国家/地区),要么具有默认值 (createdAt)。

但是生成的swagger文档如下:

 "components": {
    "schemas": {
      "Entity": {
        "required": [
          "birthDate",
          "createdAt", <------------ Notice here!
          "disabled",
          "email",
          "entityTypeAttributes",
          "entityTypeId",
          "locale",
          "name",
          "role"
        ],
        "type": "object",
        "properties": {
          "email": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "birthDate": {
            "type": "string",
            "format": "date-time"
          },
          "gender": {
            "type": "string",
            "enum": [
              "MALE",
              "FEMALE",
              "OTHER"
            ]
          },
          "country": {
            "type": "string"
          },
          "locale": {
            "type": "string"
          },
          "disabled": {
            "type": "boolean"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "role": {
            "type": "string",
            "enum": [
              "ADMIN",
              "DOCTOR",
              "PATIENT"
            ]
          },
          "entityTypeId": {
            "type": "integer",
            "format": "int64"
          },
          "entityTypeAttributes": {
            "type": "object",
            "additionalProperties": {
              "type": "object"
            }
          },
          "medicalSpecialityId": {
            "type": "integer",
            "format": "int64"
          },
          "id": {
            "type": "integer",
            "format": "int64"
          }
        }
      },
   (...)

因此,就文档而言,它表明 createdAt 是强制性的(这不是真的)...

Generated Swagger docs

我正在使用 Kotlin、Javalin 和 OpenAPI (io.javalin.plugin.openapi) Javalin 集成。

我不知道我还需要做什么才能让 OpenAPI 明白 createdAt 是可选的...

最佳答案

我的猜测是,kotlin 实现使用可空性作为发现必需属性并忽略必需属性的方法。例如,您实际上不需要性别和国家/地区的注释。

显然这并不理想,但是如果将 creadtedAt 更改为 DateTime 呢?它不会按要求显示。

这可能是 javalin 引入的 kotlin openapi 文档工具的错误。

关于kotlin - 为什么 Swagger 不检测可选的 JSON 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61696873/

相关文章:

android - 使用 Dagger2 的多模块 android 应用程序,在功能模块中不使用 `kapt`

java - JSON - 无法使用 Jackson 序列化对象内的 JSONObject

java - jackson :避免反序列化某些字段但不要忽略它们

java - 开放API 3.0.1只读注解

azure - 在 Azure API 管理中添加对特定终结点的授权

android - 每当列表发生变化时,LazyList 都会重新组合项目

android - RxJava 将 Single 与 Completable 结合起来

kotlin - 即使 encoded=true,Retrofit2 也会对查询值进行编码

java - DateTimeFormatter 可以格式化日期,但无法读取其自身的格式

java - springdoc-openapi 应用默认的全局 SecurityScheme 可能吗?