Skip to content

fix: skip resolveSubtypes for sealed classes with @Schema(oneOf) - #5322

Merged
daniel-kmiecik merged 4 commits into
swagger-api:3.0.0from
vpelikh:pr-21-skip-resolve-subtypes-sealed-oneof
Sep 24, 2026
Merged

daniel-kmiecik merged 4 commits into
swagger-api:3.0.0from
vpelikh:pr-21-skip-resolve-subtypes-sealed-oneof

Conversation

@vpelikh

@vpelikh vpelikh commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Why this PR

A Java sealed class is polymorphic by the language: with @Schema(oneOf = ...) the user already declares the target subtypes explicitly. On Jackson 3, sealed classes also get their subtypes auto-detected, so swagger-core ended up generating both the user's intended oneOf and a redundant/wrong allOf, producing schemas that fail validation and confuse consumers.

Problem

  • Jackson 3 auto-detects subtypes of Java sealed classes (the compiler records PermittedSubclasses).
  • When a sealed class also declared @Schema(oneOf = ...), resolveSubtypes() added unwanted allOf references on top of the explicit oneOf.
  • The result was an invalid schema (composition keywords misused) and spurious references.

Change

In ModelResolver.resolve(), skip resolveSubtypes() when the resolved type is a sealed class that declares @Schema(oneOf = ...). The @Schema(oneOf) handling already builds the correct polymorphic schema with a discriminator, so the extra allOf is redundant and removed.

Result: sealed classes with @Schema(oneOf) now generate clean polymorphic schemas without duplicate allOf references.

Related: #4991

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • ♻️ Refactor (non-breaking change)
  • 🧪 Tests
  • 📝 Documentation
  • 🧹 Chore (build or tooling)

Checklist

  • I have added/updated tests as needed
  • I have added/updated documentation where applicable
  • The PR title is descriptive
  • The code builds and passes tests locally
  • I have linked related issues (if any)

Screenshots / Additional Context

vpelikh and others added 2 commits September 18, 2026 23:21
Jackson 3 auto-detects subtypes of Java sealed classes. When a sealed class also
declared @Schema(oneOf = ...), resolveSubtypes() added unwanted allOf references to
the subtypes on top of the explicit oneOf.

Skip resolveSubtypes() for sealed classes that declare @Schema(oneOf); the oneOf
handling already builds the correct polymorphic schema with a discriminator.

Adds SealedClassOneOfTest which fails without the fix (unwanted allOf on subtypes)
and passes with it.
@vpelikh vpelikh changed the title Skip resolveSubtypes for sealed classes with @Schema(oneOf) fix: skip resolveSubtypes for sealed classes with @Schema(oneOf) Sep 24, 2026
@daniel-kmiecik
daniel-kmiecik merged commit e66aae6 into swagger-api:3.0.0 Sep 24, 2026
6 checks passed
@vpelikh
vpelikh deleted the pr-21-skip-resolve-subtypes-sealed-oneof branch October 19, 2026 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants