GeezerVison is showing github.com — fix: keep polymorphic subtype composition when re-resolved standalone (#5028) by seonwooj0810 · Pull Request #5230 · swagger-api/swagger-core
Conversation
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).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
jochenberger reacted with thumbs up emoji
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 <[email protected]>
|
Bumping this fix whenever there's time — happy to add more regression tests around the polymorphic re-resolution case if helpful.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This looks related to #5424, which reports the same effect with a different trigger: a subtype that is also used via @JsonUnwrapped loses its allOf reference to the parent when the polymorphic usage is resolved first. There jsonUnwrappedHandler resolves the type with a modified AnnotatedType (ctxAnnotations(null), jsonUnwrappedHandler(null), resolveAsRef(false)), and the resulting plain schema is defined over the composed one.
As far as I can tell, the guard in ModelConverterContextImpl would also prevent that overwrite. Would you consider adding that scenario to the tests here? If it passes, #5424 is covered by this PR; if it fails, jsonUnwrappedHandler needs its own change. Something like this (I haven't compiled or run it):
@Test
public void testUnwrappedUsageKeepsSubtypeCompositionOpenApi30() {
assertUnwrappedUsageKeepsComposition(false, Step.class, Transform.class);
assertUnwrappedUsageKeepsComposition(false, Transform.class, Step.class);
}
@Test
public void testUnwrappedUsageKeepsSubtypeCompositionOpenApi31() {
assertUnwrappedUsageKeepsComposition(true, Step.class, Transform.class);
assertUnwrappedUsageKeepsComposition(true, Transform.class, Step.class);
}
private void assertUnwrappedUsageKeepsComposition(boolean openapi31, Class<?>... resolveInOrder) {
final ModelResolver modelResolver = new ModelResolver(mapper());
modelResolver.setOpenapi31(openapi31);
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
for (Class<?> type : resolveInOrder) {
context.resolve(new AnnotatedType(type));
}
final io.swagger.v3.oas.models.media.Schema customAttr = context.getDefinedModels().get("CustomAttr");
assertNotNull(customAttr, "CustomAttr schema should be defined");
assertNotNull(customAttr.getAllOf(),
"CustomAttr must keep its allOf reference to Location, but was: " + customAttr);
assertTrue(customAttr.getAllOf().stream()
.anyMatch(s -> "#/components/schemas/Location"
.equals(((io.swagger.v3.oas.models.media.Schema) s).get$ref())),
"allOf must reference Location, but was: " + customAttr.getAllOf());
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({@JsonSubTypes.Type(value = CustomAttr.class, name = "customAttr")})
@Schema(discriminatorProperty = "type",
discriminatorMapping = @DiscriminatorMapping(value = "customAttr", schema = CustomAttr.class))
interface Location {
}
// also used unwrapped, hence no type id of its own
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
static class CustomAttr implements Location {
public String customAspect;
public String attribute;
}
static class Transform {
public String expression;
@JsonUnwrapped
public CustomAttr target;
}
static class Step {
public List<Location> oldDefinitions;
}
The Transform, Step order is a control: according to #5424 it already works today, because resolveSubtypes() replaces the plain model. Only Step, Transform should fail without the guard. The issue was reported for OpenAPI 3.1 only, so I don't know whether the 3.0 variant fails without the guard either; it is still useful as a regression test.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Resolving a polymorphic usage before a @JsonUnwrapped usage of the same
subtype (swagger-api#5424) also re-resolves the subtype plain and used to drop its
allOf reference to the parent. Add OAS 3.0/3.1 regression tests for both
resolution orders.
Signed-off-by: seonwoo_jung <[email protected]>
|
Added both tests in bcb421d. They pass with the guard, and with the ModelConverterContextImpl change reverted the Step, Transform order fails on both OpenAPI 3.0 and 3.1, so #5424 looks covered by this PR without a separate jsonUnwrappedHandler change. The full swagger-core module suite still passes (724 tests).
|
jochenberger reacted with hooray emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This branch has not been deployed
No deployments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|