Subtype loses allOf reference to its polymorphic parent since 3.2.0 when a controller repeats the interface's @Valid on a @RequestBody
维护者通常 9 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 30/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- java, openapi, spring, spring-boot
- 领域
- api, documentation
调研方向
Start in GenericParameterService.resolveTypeAndTypeAnnotationsForParameter, where annotations of the parameter's AnnotatedType were added in ada02a095, and compare with DelegatingMethodParameter.getTypeUseAnnotations. The proposed filter is to drop type-use annotations already present among the parameter annotations, so the context-annotation cache key for createCat matches updateCat again. Reproduce first with the attached test.org.springdoc.api.v31.app283 fixture in springdoc-openapi-starter-webmvc-api, asserting Cat is emitted as allOf with a $ref to Animal. Note an open pull request already exists, so coordinate before starting.
由索引模型根据 Issue 内容生成。
描述
Describe the bug
After upgrading from 3.1.1 to 3.2.0, subtypes of a @JsonSubTypes parent are documented as flat object schemas instead of allOf: [{$ref: Parent}, {…}]. If all subtypes are affected, the parent schema is then unreferenced and removed by removeBrokenReferenceDefinitions (with springdoc.remove-broken-reference-definitions=false it is still there, but nothing references it).
The trigger is a controller method that implements an interface method and repeats the interface's @Valid on the @RequestBody parameter:
interface CatApi {
@PostMapping("/cats")
void createCat(@Valid @RequestBody Cat cat);
}
@RestController
public class HelloController implements CatApi {
@Override
public void createCat(@Valid Cat cat) { } // removing @Valid here restores the 3.1.1 output
}
To Reproduce
- Spring Boot: 4.1.1
- springdoc-openapi:
springdoc-openapi-starter-webmvc-ui3.2.0 (works with 3.1.1) - swagger-core: 2.2.55 (same in both versions)
springdoc.api-docs.version=OPENAPI_3_1
A test case for springdoc-openapi-starter-webmvc-api is attached (test.org.springdoc.api.v31.app283, two files). HelloController contains:
Animal: abstract,@JsonSubTypes(Cat), and its subtypeCatCatApi:PUT /catsandPOST /cats, both with@Valid @RequestBody Cat;POST /catsalso declares@ApiResponse(content = @Content(schema = @Schema(implementation = Cat.class)))updateCatwithout parameter annotations,createCatrepeating@ValidPOST /petswith@RequestBody AnimalandGET /cats/countreturning aString
Expected behavior
"Cat": {
"allOf": [
{ "$ref": "#/components/schemas/Animal" },
{ "type": "object", "properties": { "indoor": { "type": "boolean" } } }
]
}
Actual behavior
Cat is a flat object schema containing all properties (name, indoor) and no allOf.
Analysis
The behaviour changed with ada02a095 (#3377, "honour JSpecify @Nullable on controller parameters"). GenericParameterService.resolveTypeAndTypeAnnotationsForParameter now also forwards the annotations of the parameter's AnnotatedType itself. @Valid (like @NotNull) also targets TYPE_USE, so for createCat the request body is resolved with the context annotations [RequestBody, Valid, Valid] instead of [RequestBody, Valid]. The type-use annotations are read from the implementation method, while the parameter annotations are merged with the interface's, so updateCat keeps the old key and createCat gets a new one.
Because the ModelConverterContext is shared for the whole document build (SpringDocAnnotationsUtils.MODEL_CONVERTER_CONTEXT_MAP) and AnnotatedType.equals compares the context annotations, this new key leads to the following sequence (operations are processed in reverse RequestMappingInfo order):
PUT /cats:Catis resolved flat.POST /pets: resolvingAnimalmakesModelResolver.resolveSubtypesredefineCatasallOfschema in the shared context.POST /cats, request body: 3.1.1 hits the cache. 3.2.0 has a new key, soModelResolverresolvesCatagain anddefineModelreplaces theallOfschema with a flat one in the shared context.POST /cats, response:AnnotationsUtils.getSchemaresolvesCatin a fresh context andcomponents.addSchemasoverwrites the component with a flat schema.GET /cats/count:SpringDocAnnotationsUtils.extractSchemamerges all models of the shared context into the components and prefers schemas withallOf. In 3.1.1 this restores theallOfversion ofCat; in 3.2.0 the shared context only holds the flat one.
Possible fix
Skip type annotations that are already present among the parameter annotations before merging them, as DelegatingMethodParameter.getTypeUseAnnotations already does with !declaredAnnotations.contains(annotation). That keeps the cache keys of 3.1.1 for declaration-and-type-use annotations like @Valid/@NotNull, while type-use-only annotations such as JSpecify's @Nullable are still forwarded.
The underlying fragility is older, though: any resolution of a subtype under a new cache key after its parent has been resolved replaces the allOf form with a flat schema in the shared context, and whether the final document is correct then depends on operation order.
- 主要语言
- Java
- 星标
- 3.7k
- 派生
- 605
- 平均合并
- 9 天 11 小时
- 30 天内合并 PR
- 14
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
springdoc/springdoc-openapi 的其他 Issue
-
enhancement upstream
难度 4/5 3-5 天 新手友好度 48/100
springdoc/springdoc-openapi#3324 · 2 条评论 ·
维护者通常 9 天内回复
-
enhancement upstream
难度 3/5 1-2 天 新手友好度 55/100
springdoc/springdoc-openapi#3309 · 4 条评论 · 1 个 reaction ·
维护者通常 9 天内回复
-
难度 3/5 1-2 天 新手友好度 45/100
springdoc/springdoc-openapi#3404 · 1 条评论 ·
维护者通常 9 天内回复
查看 springdoc/springdoc-openapi 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
WalletConnect/walletconnect-monorepo#7389 · 1 条评论 ·
-
help wanted testing
难度 2/5 1-3 小时 新手友好度 74/100
elhajahmadj/offline-emergency-library#108 ·
维护者通常 1 天内回复
-
Add documentation for Transaction-bound OTP generation可能已有人在做 @radimtrojek-rgb 今天认领。 未关闭documentation
难度 2/5 1-3 小时 新手友好度 60/100
wultra/powerauth-nextstep#86 ·
-
API `/api/dataverses/{identifier}/dataset/linkingDataverses?alreadyLinking=true` returns at most 1 collection可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭Type: Bug
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
OpenLineage/OpenLineage#5138 ·
维护者通常 2 天内回复