OpenAPI 3.0嵌套类的Java注解

huangapple go评论82阅读模式
英文:

OpenAPI 3.0 Java Annotations of Nested Class

问题

我试图在使用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:
          # 此属性的值是一个对象
          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
英文:

I am trying to get the components to appear nested together when using OpenAPI 3.0 Java annotations. However, every object that is referenced inside another object are being created as $ref instead of being built out as that field node. How can I get it to nest under without the $ref?

For Example:

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

public class ContactInfo{
  String email;
  String phone;
}

as

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

instead of

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

答案1

得分: 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);
    }
}
英文:

All complex objects are generated with springdoc-openapi, using $ref objects for reuse purposes.

This behavior comes by default from the swagger-core library for the resolution of the Nested objects.

This is said, you can define your properties programatically using a combination of OpenApiCustomiser and swagger classes to get your expected result:

@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);
	}
}

huangapple
  • 本文由 发表于 2020年9月5日 06:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63748521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定