java - Swagger 忽略引用模式的模式属性

标签 java swagger swagger-2.0 openapi

我正在使用 Swagger Core 2.0.2 for Java 生成 OpenAPI 文档。其中,我有以下类 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;
}

我的问题是 otherDTO 字段上方的 descriptionrequired 字段都没有任何效果。

生成的 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 模式有一个包含 OtherDTOrequired 数组,但事实并非如此。描述也丢失了。

我尝试了多种模式设置组合,但都无济于事。如果能帮助我理解我做错了什么,我将不胜感激。

提前致谢。

最佳答案

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

问题是由于使用 $ref 元素时,sibling elements are ignored. 引起的。因此,与被引用元素相关的元素(descriptionname 等)需要在被引用对象本身(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;
}

但是,它并没有完全解决我的问题,因为我仍然无法弄清楚如何在 SomeDTO 中设置 otherDTOdescription >。但这让我更近了一步。

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

相关文章:

java - 与 MongoDB 的 Jelastic 连接在远程 tomcat 上不起作用,但当本地 tomcat 连接到远程 Mongo 时正常

java - 关于具有多个条件的 If 语句的快速问题

swift - Swagger 自动生成不包含错误类

.net-core - 如何使用 ASP.NET Core Web Api 在 SwaggerUI 中设置默认版本

odata - 如何记录 OData 端点(swagger、swashbuckle、其他)?

java - 按集合中的元素查找

java - 如何为 Spring Kafka Listener 创建集成测试

swagger - NestJS 映射类型 - 嵌套对象

swagger - 在 Swagger 中创建我自己的类型

node.js - Swagger 生成器无法识别 Controller