我正在使用Java的Swagger Core 2.0.2生成OpenAPI文档。除其他外,我有以下类(class)SomeDTO:

@Schema(name = "SomeDTO", description = "some description")
public class SomeDTO {
  @Schema(description = "description of name")
  private String name;
  @Schema(required = true, description = "description of OtherDTO")
  private OtherDTO otherDTO;
}
OtherDTO描述如下:
public class OtherDTO {
  @Schema(required = true)
  private String someField;
  private String someOtherField;
}

我的问题是description字段上方的requiredotherDTO字段都不起作用。

生成的openapi.json看起来像这样:
    "components": {
      "schemas": {
        "SomeDTO" : {
          "type": "object",
          "properties": {
            "name": {
              "type" : "string"
            }
            "otherDTO" : {
              "$ref": "#/components/schemas/OtherDTO"
            }
          },
          "description": "some description"
        },
        "OtherDTO": {
          "required": ["someField"],
          "type": "object",
          "properties": {
            "somefield": {
              "type": "string"
            },
            "someOtherField": {
              "type": "string"
            }
          }
        }
      }
    }

我期望SomeDTO模式具有一个包含requiredOtherDTO数组,但没有。说明也丢失了。

我尝试了模式设置的多种组合,但无济于事。对于理解我做错的任何帮助,我将深表感谢。

提前致谢。

最佳答案

我找到了部分问题的解决方案。

该问题是由以下事实引起的:使用$ref元素时,需要将与被引用元素相关的sibling elements are ignored. So元素(descriptionname等)指定为被引用对象本身(上面示例中的@Schema)的OtherDTO。在父对象中指定这些元素(例如SomeDTO)将使它们被忽略。

但是,被引用元素中的架构元素似乎并未传播到父对象。因此,要使otherDTO成为SomeDTO中的必填字段,我需要将requiredProperties = { "OtherDTO" })添加到SomeDTO的架构中。

这是更新的代码:
SomeDTO

@Schema(name = "SomeDTO", description = "some description",
requiredProperties = { "OtherDTO" })
public class SomeDTO {
  @Schema(description = "description of name")
  private String name;
  private OtherDTO otherDTO;
}
OtherDTO
@Schema(name = "OtherDTO", description = "Description of OtherDTO")
public class OtherDTO {
  @Schema(required = true)
  private String someField;
  private String someOtherField;
}

但是,它不能完全解决我的问题,因为我仍然不知道如何在description中设置otherDTOSomeDTO。但这使我更近了一步。

关于java - Swagger会忽略引用架构的架构属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51671883/

10-10 13:16