Property-level @Schema(oneOf) makes every other property of the same type lose its subtypes (static TYPES_TO_SKIP)

已关闭
#3,398 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 9 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
57/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java, openapi, spring-boot

调研方向

Start by reading PolymorphicModelConverter and tracing how TYPES_TO_SKIP affects composePolymorphicSchema when properties are resolved. Reproduce the reported case with the supplied Spring Boot example and inspect GET /v3/api-docs. Done means only the property declaring @Schema(oneOf) is narrowed, while the plain properties still include both Circle and Square.

由索引模型根据 Issue 内容生成。

描述

An AI coding agent (Claude Code) found this bug while working in our codebase, traced it to PolymorphicModelConverter, reproduced it in a bare Spring Boot app and wrote this issue. The results below come from that app, not from reading the code alone.

Describe the bug

When one property declares @Schema(oneOf = ...) (or allOf), PolymorphicModelConverter adds the simple name of the property's type to the static set TYPES_TO_SKIP. composePolymorphicSchema then gives no subtypes to every later property of that type, in every class, for the rest of the JVM. Such a property is written as {} (abstract parent) or as a oneOf with only the parent (concrete parent).

Issue #2851 asked for the declared types to win on the field that declares them. The skip now also applies to other fields that declare nothing. The result depends on the order in which the schemas are resolved: in our application, removing an unrelated request body from one endpoint emptied a List<FileDTO> property elsewhere in the spec.

To Reproduce

  • springdoc-openapi-starter-webmvc-api 3.2.0 (also 3.1.1), Spring Boot 4.1.2, Java 25, default settings
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
@JsonSubTypes({ @JsonSubTypes.Type(value = Circle.class, name = "CIRCLE"),
                @JsonSubTypes.Type(value = Square.class, name = "SQUARE") })
public abstract static class Shape {}
public static class Circle extends Shape { public double radius; }
public static class Square extends Shape { public double side; }

public static class NarrowedHolder {
    @Schema(oneOf = Circle.class)
    public Shape shape;
}
public static class PlainHolder {
    public Shape shape;
}
public static class Outer {
    public NarrowedHolder first;
    public PlainHolder second;
}

@RestController
public static class OuterController {
    @PostMapping("/outer")
    public void outer(@RequestBody Outer body) {}
}

GET /v3/api-docs, then components.schemas:

  • NarrowedHolder.shape: {"oneOf":[{"$ref":"#/components/schemas/Circle"}]} (correct)
  • PlainHolder.shape: {}

With only @PostMapping("/plain") void plain(@RequestBody PlainHolder body), PlainHolder.shape is {"oneOf":[Circle, Square]}.

The same happens inside one class:

public static class BothHolder {
    @Schema(oneOf = Circle.class)
    public Shape narrowed;
    public Shape plain;     // written as {}
}

Expected behavior

PlainHolder.shape and BothHolder.plain are oneOf Circle and Square. Only the property that declares oneOf gets the declared subtypes.

Cause

TYPES_TO_SKIP is static, keyed on the simple class name, and never cleared. The decision should belong to the property that is resolved. Note that in OpenAPI 3.1 mode swagger-core removes @Schema from the ctxAnnotations of a property (SiblingAnnotationFilter), so the converter cannot read it there. We work around it in our project: while the converter resolves a type, it keeps the names of that type's properties that declare oneOf/allOf, and it checks AnnotatedType#getPropertyName() against them when it resolves a property of that type.

主要语言
Java
星标
3.7k
派生
605
平均合并
9 天 11 小时
30 天内合并 PR
14

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

springdoc/springdoc-openapi 的其他 Issue

查看 springdoc/springdoc-openapi 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。