嵌套类的openapi 3.0 java注解

ccrfmcuu  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(845)

在使用openapi3.0java注解时,我试图使组件看起来嵌套在一起。但是,在另一个对象中引用的每个对象都被创建为$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
v2g6jxz6

v2g6jxz61#

所有复杂对象都是使用springdoc openapi生成的,使用$ref对象进行重用。
默认情况下,此行为来自swagger核心库,用于解析嵌套对象。
也就是说,您可以使用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);
    }
}

相关问题