【问题标题】:OpenAPI 3.0 Java Annotations of Nested ClassOpenAPI 3.0 Java Annotations of Nested Class
【发布时间】:2020-12-24 03:51:05
【问题描述】:

我试图让组件在使用 OpenAPI 3.0 Java 注释时嵌套在一起。但是,在另一个对象中引用的每个对象都被创建为 $ref 而不是构建为该字段节点。如果没有 $ref,我怎样才能让它嵌套在下面?

例如:

public class User{
  int id;
  String name;
  ContactInfo contactInfo;
}

public class ContactInfo{
  String email;
  String phone;
}

作为

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        contact_info:
          # The value of this property is an object
          type: object
          properties:
            email:
              type: string
              format: email
            phone:
              type: string

而不是

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        contactInfo: {
          $ref: "#/components/schemas/ContactInfo"
        }
    ContactInfo:
      type: object
      properties:
         email:
           type: string
           format: email
         phone:
           type: string

【问题讨论】:

    标签: java openapi openapi-generator springdoc


    【解决方案1】:

    所有复杂对象都是用 springdoc-openapi 生成的,使用 $ref 对象进行重用。

    此行为默认来自用于解析嵌套对象的 swagger-core 库。

    也就是说,您可以使用 OpenApiCustomiser 和 swagger 类的组合以编程方式定义您的属性,以获得您的预期结果:

    @Component
    public class MyOpenApiCustomiser extends SpecFilter implements OpenApiCustomiser {
        @Override
        public void customise(OpenAPI openApi) {
            ResolvedSchema resolvedUserSchema = ModelConverters.getInstance()
                    .resolveAsResolvedSchema(new AnnotatedType(User.class));
            resolvedUserSchema.schema
                    .addProperties("contactInfo", new ObjectSchema()
                            .addProperties("email", new StringSchema().format("email"))
                            .addProperties("phone", new StringSchema()));
            openApi.schema(resolvedUserSchema.schema.getName(), resolvedUserSchema.schema);
            this.removeBrokenReferenceDefinitions(openApi);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 2018-11-05
      • 2022-12-06
      相关资源
      最近更新 更多