【问题标题】:All Of is not working properly in Open API Swagger 3.0All Of 在 Open API Swagger 3.0 中无法正常工作
【发布时间】:2021-09-13 05:14:27
【问题描述】:

在我的项目中,我使用 Open API 3.0 来生成 mmodel 类。

我的要求是我必须将父类属性重用到子类。

例如:

public class Student {
  private String id;
  private String name;
}

public class Address {
  private String id;
  private String name;
  private String city;
  private String state;
}

但问题是 allOf 生成如下所示的 bean 类而不是上面的类。

public class Address {
  public Student student;
  private String city;
  private String state;
}

下面是我的 schema.yaml

Student:
    properties :
    id:
      type: integer
      format: int64
      description: The ID of the new account
    name:
      type: string
      description: The human-readable description of this account
    

Address:
    properties : 
    allOf:
        $ref : '#/Student'
    city:
       type: string
      description: City
    state:
      type: string
      description: State

  

如何确保属性是复制而不是创建对象。

任何帮助将不胜感激!!!

【问题讨论】:

  • 看来allOf 并没有按照您的预期去做。根据Swagger Documentation allOf 用于组合(这意味着在您的示例中Address 由对Student 的引用和一些附加字段组成)
  • 谢谢托马斯!!!我理解 allOf 的目的。还有其他方法可以实现我的需要吗?

标签: java swagger swagger-ui openapi


【解决方案1】:

您可以在allOf 元素下定义单独的部分。第一部分是对父对象的引用。第二部分是具有附加属性的对象。

https://editor.swagger.io 中正确解析了以下内容:

openapi: 3.0.1
info:
  title: "example"
  version: "1.0"
paths:
  /example:
    post:
      responses:
        "200":
          description: example
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Address'
components:
  schemas:
    Student:
        properties:
          id:
            type: integer
            format: int64
            description: The ID of the new account
          name:
            type: string
            description: The human-readable description of this account
        
    Address:
      allOf:
      - $ref : '#/components/schemas/Student'
      - type: object
        properties:
          city:
             type: string
             description: City
          state:
            type: string
            description: State

【讨论】:

  • 谢谢杰克。但是如果我这样定义,我会收到拒绝访问错误。 java.io.FileNotFoundException: C:\test\src\main \resources\schema\v0_1(访问被拒绝)
  • 您可能正在使用 Spring Security,那么您必须从您的安全配置文件中为资源文件夹提供权限:- .antMatchers("/resources/**").permitAll()
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多