Quarkus 3中OpenAPI子类如何继承父类@Schema必填属性?
子类OpenAPI规范丢失父类必填属性的解决方法
问题场景
使用Quarkus 3(基于Smallrye OpenAPI扩展,兼容MicroProfile OpenAPI规范)生成OpenAPI v3规范时,父类标注的必填属性未被子类继承。
代码示例
父类与子类的Java代码:
import org.eclipse.microprofile.openapi.annotations.media.Schema; @Schema(requiredProperties = {"id"}) public class ArrangementDto { private String id; } public class CreditCardDto extends ArrangementDto { private String number; }
生成的异常规范
生成的OpenAPI规范中,CreditCardDto丢失了id的必填标记:
ArrangementDto: required: - id type: object properties: id: type: string CreditCardDto: type: object properties: id: type: string number: type: string
解决方法
1. 显式启用子类的Schema继承
在子类上添加@Schema(inherit = true)注解,该属性会让子类完整继承父类的Schema配置(包括必填属性列表)。如果子类自身还有需要标记为必填的属性,可以在requiredProperties中补充:
import org.eclipse.microprofile.openapi.annotations.media.Schema; @Schema(requiredProperties = {"id"}) public class ArrangementDto { private String id; } @Schema(inherit = true, requiredProperties = {"number"}) public class CreditCardDto extends ArrangementDto { private String number; }
调整后生成的CreditCardDto规范会同时包含父类的id和子类的number作为必填项:
CreditCardDto: required: - id - number type: object properties: id: type: string number: type: string
2. 确认Smallrye OpenAPI配置
检查Quarkus配置文件(如application.properties),确保未禁用Schema继承:
# 默认值为true,无需额外配置;若之前修改过,需确保该值为true smallrye.openapi.schema.inheritance=true
内容的提问来源于stack exchange,提问作者Uros K
相关产品推荐
相关产品推荐

