当前位置: 首页 > 知识库问答 >
问题:

如何使用springdoc openapi/swagger生成映射到json对象的类型的可空字段?

荣沈义
2023-03-14

首先,以下存储库具有重现此问题的所有代码(以及描述):https://github.com/elgleidson/swagger-problem

我有以下JSON:

{
  "nonNullableField": "not null",
  "nullableField": null,
  "nonNullableObjectField": {
    "someField": "some value"
  },
  "nullableObjectField": null,
  "nonNullableList": [
    "not null"
  ],
  "nullableList": null,
  "nonNullableObjectList": [
    {
      "someField": "some value"
    }
  ],
  "nullableObjectList": null
}

映射到以下Java类:

@Value
public class MyResponse {

  @Schema(nullable = false, description = "DO NOT map to json object and DO NOT allow null")
  private final String nonNullableField = "not null";

  @Schema(nullable = true, description = "DO NOT map to json object and allows null")
  private final String nullableField = null;

  @Schema(nullable = false, description = "map to json object and DOES NOT allow null")
  private final MyClass nonNullableObjectField = new MyClass(nonNullableField);

  @Schema(nullable = true, description = "map to json object and allows null")
  private final MyClass nullableObjectField = null;

  @ArraySchema(arraySchema = @Schema(nullable = false, description = "list that DOES NOT map to json object and DOES NOT allow null"))
  private final List<String> nonNullableList = List.of(nonNullableField);

  @ArraySchema(arraySchema = @Schema(nullable = true, description = "list that DOES NOT map to json object and allow null"))
  private final List<String> nullableList = null;

  @ArraySchema(arraySchema = @Schema(nullable = false, description = "list that map to json object and DOES NOT allow null"))
  private final List<MyClass> nonNullableObjectList = List.of(nonNullableObjectField);

  @ArraySchema(arraySchema = @Schema(nullable = true, description = "list that map to json object and allow null"))
  private final List<MyClass> nullableObjectList = null;

}

@Value
@Schema(description = "my class description")
public class MyClass {

  @Schema(description = "my class field description")
  private final String someField;

}

当我转到/v3/api-docs(或/swagger-ui.html)时,会生成以下留档:

{
  "MainResponse": {
    "type": "object",
    "properties": {
      "nonNullableField": {
        "type": "string",
        "description": "DO NOT map to json object and DO NOT allow null"
      },
      "nullableField": {
        "type": "string",
        "description": "DO NOT map to json object and allows null",
        "nullable": true
      },
      "nonNullableObjectField": {
        "$ref": "#/components/schemas/MyClass"
      },
      "nullableObjectField": {
        "$ref": "#/components/schemas/MyClass"
      },
      "nonNullableList": {
        "type": "array",
        "description": "list that DOES NOT map to json object and DOES NOT allow null",
        "items": {
          "type": "string"
        } 
      },
      "nullableList": {
        "type": "array",
        "description": "list that DOES NOT map to json object and allow null",
        "nullable": true,
        "items": {
          "type": "string"
        }
      },
      "nonNullableObjectList": {
        "type": "array",
        "description": "list that map to json object and DOES NOT allow null",
        "items": {
          "$ref": "#/components/schemas/MyClass"
        }
      },
      "nullableObjectList": {
        "type": "array",
        "description": "list that map to json object and allow null",
        "nullable": true,
        "items": {
          "$ref": "#/components/schemas/MyClass"
        }
      }
    }
  },
  "MyClass": {
    "type": "object",
    "properties": {
      "someField": {
        "type": "string",
        "description": "my class field description"
      }
    },
    "description": "my class description",
    "nullable": true
  }
}

如您所见,对于类型未映射到object的字段,会按预期生成留档。nullableObjectField不会发生同样的情况:nullable属性放在MyClass定义中而不是字段中。

我想生成以下文档:

{
  "MainResponse": {
    "type": "object",
    "properties": {
      "nonNullableField": {
        "type": "string",
        "description": "DO NOT map to json object and DO NOT allow null"
      },
      "nullableField": {
        "type": "string",
        "description": "DO NOT map to json object and allows null",
        "nullable": true
      },
      "nonNullableObjectField": {
        "$ref": "#/components/schemas/MyClass",
        "description": "map to json object and DOES NOT allow null"
      },
      "nullableObjectField": {
        "$ref": "#/components/schemas/MyClass",
        "description": "map to json object and allows null",
        "nullable": true
      },
      "nonNullableList": {
        "type": "array",
        "description": "list that DOES NOT map to json object and DOES NOT allow null",
        "items": {
          "type": "string"
        } 
      },
      "nullableList": {
        "type": "array",
        "description": "list that DOES NOT map to json object and allow null",
        "nullable": true,
        "items": {
          "type": "string"
        }
      },
      "nonNullableObjectList": {
        "type": "array",
        "description": "list that map to json object and DOES NOT allow null",
        "items": {
          "$ref": "#/components/schemas/MyClass"
        }
      },
      "nullableObjectList": {
        "type": "array",
        "description": "list that map to json object and allow null",
        "nullable": true,
        "items": {
          "$ref": "#/components/schemas/MyClass"
        }
      }      
    }
  },
  "MyClass": {
    "type": "object",
    "properties": {
      "someField": {
        "type": "string",
        "description": "my class field description"
      }
    },
    "description": "my class description"
  }
}

我该怎么做?可能吗?

共有1个答案

邴越彬
2023-03-14

它看起来像是一个招摇过市的错误:

  • https://github.com/swagger-api/swagger-core/issues/3518
 类似资料:
  • 问题内容: 我想创建一个地图,可以将其转换为json对象,例如 但是golang指定使用类型声明地图,因此我可以使用map [string] string或map [string] int。我如何创建上述的json对象? 注意:直到运行时或需要创建json对象时,我才知道需要哪些数据和/或类型。因此,我不能只创建像 问题答案: 您可以随时使用存储任何类型。如包装袋中的文件所述: 若要将JSON解组

  • 我试图使用http://modelmapper.org/表示DAO和模型类的库- 模型类- 道类- 公共类主题{私有字符串名称; 映射逻辑 ModelMapper似乎不起作用,它给我提供了主题类项目,而不是主题模型类项目

  • 问题内容: 我正在尝试将JSON文件映射到类对象,然后根据新接收的JSON更新卡。 我的JSON结构是这样的 我的班级看起来像这样: 如何将JSON文件中的值映射到CardInfo类创建的对象的字段中? 更新资料 以下试用版在 ci.description上 打印为null ,是否表示从未创建该对象? 更新2 打印cardInfo给出以下内容: {$ class:FirstCard,id:1,说明

  • 问题内容: 我有一个JSON响应,我需要将对应的JSON字符串映射到特定的Response类,是否有任何工具或框架可以做到这一点。 响应类为: Json响应字符串为{“ 0”:{“ 0”:“ Rockey”,“ 1”:“ John”}} 我将Apache CXF Framework与Jettison一起使用,因为JSON Provider还使用JAXB将数据连接到低带宽客户端。 请注意,我要将数字

  • 问题内容: 我们有一张有很多列的大桌子。移至MySQL Cluster后,由于以下原因无法创建表: 错误1118(42000):行大小太大。不包括BLOB在内的已使用表类型的最大行大小为14000。这包括存储开销,请查阅手册。您必须将某些列更改为TEXT或BLOB 举个例子: 这是用于存储配置参数的表。我在想,我们可以将一些列合并为一个列,并将其存储为JSON对象,然后将其转换为Java对象。 例

  • 我们有一张有很多柱子的大桌子。在我们移动到MySQL集群后,表无法创建,因为: 错误1118 (42000):行大小过大。使用的表类型(不包括BLOB)的最大行大小为14000。这包括存储开销,检查手册。您必须将一些列更改为TEXT或BLOB 例如: 它是一个用于存储配置参数的表。我在想,我们可以将一些列组合成一个列,并将其存储为JSON对象,并将其转换为Java对象。 例如: 我们定义了: 通过