Skip to content

fix: keep polymorphic subtype composition when re-resolved standalone (#5028)#5230

Open
seonwooj0810 wants to merge 1 commit into
swagger-api:masterfrom
seonwooj0810:fix/issue-5028-subtype-composition-overwrite
Open

fix: keep polymorphic subtype composition when re-resolved standalone (#5028)#5230
seonwooj0810 wants to merge 1 commit into
swagger-api:masterfrom
seonwooj0810:fix/issue-5028-subtype-composition-overwrite

Conversation

@seonwooj0810

Copy link
Copy Markdown

Fixes #5028

Root cause

Polymorphic subtypes lose their allOf reference to the parent (regression in 2.2.41): a subtype that resolved to

"DateAttributeTypeImpl": { "type": "object", "allOf": [ { "$ref": "#/components/schemas/AttributeType" } ] }

degrades to { "type": "object", "properties": {} }.

The parent's resolveSubtypes builds each subtype's composed schema (allOf → parent) and registers it via defineModel. However, when the same subtype is resolved standalone afterwards — here while resolving the parent's @DiscriminatorMapping, whose resolveDiscriminator calls context.resolve(new AnnotatedType().type(mapping.schema())) without the subtype(true) flag — the resolution-cache lookup misses (AnnotatedType equality was tightened in #4975 / #5005), so the subtype is re-resolved as a plain object and modelByName is overwritten, dropping the composition.

Fix

Guard model registration in ModelConverterContextImpl so a schema without composition keywords (allOf/anyOf/oneOf) never overwrites an already-registered composed schema of the same name. The forward direction (plain → composed) is unaffected, so the composed subtype definition survives the later plain re-resolution. Applied at both storage sites (defineModel and the resolve() name mapping).

Tests

Added Ticket5028Test reproducing the reporter's hierarchy (interface parent with subTypes + discriminatorMapping, subtype via @Schema(implementation=...)). Both assertions fail on master and pass with the fix, for OpenAPI 3.0 and 3.1.

Verification done

  • (1/2) No in-flight PR: searched open/all PRs for the symbol and my own open PRs — none cover this root cause; only cross-ref is a Copilot WIP bot issue.
  • (4) Confirmed the overwrite path on master (resolveDiscriminator L2746 → ModelConverterContextImpl.resolve/defineName).
  • (3) Regression source [Bug]: Regression: AnnotatedType equality change drops polymorphic subtype allOf entries #5003 is closed; this is the residual case.
  • Full swagger-core module suite (722 tests) + focused composition/subtype/discriminator suite (38 tests) pass with the change. git commit -s (DCO).

A subtype's allOf reference to its parent is built by the parent's
resolveSubtypes and stored via defineModel. When the same subtype is
later resolved standalone (e.g. while resolving a discriminatorMapping
target, which uses an AnnotatedType without the subtype flag and so
misses the resolution cache since swagger-api#4975/swagger-api#5005), the plain re-resolution
overwrote the stored composed schema, dropping its allOf.

Guard model registration so a schema without composition keywords never
overwrites an already-registered composed schema of the same name.

Fixes swagger-api#5028

Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
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.

[Bug]: Polymorphic types don't include composite schema reference

1 participant