英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论