Repository navigation
Description
Summary
In ModelResolver.resolveSubtypes(...) the local count is never incremented, so return count != 0 is a constant false. The caller therefore always runs model.setDiscriminator(null), which is a no-op because the discriminator is unconditionally re-applied immediately afterwards by resolveDiscriminator(...).
Net effect: the setDiscriminator(null) call and the count/return-value plumbing are dead code. "Fixing" them (making the return meaningful) is not safe as-is: it starts clearing discriminators that are actually needed.
Where
modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java
// resolveSubtypes(...)
int count = 0;
for (NamedType subtype : types) {
// ... resolves subtypes, defines their allOf schemas ...
// NOTE: count is never incremented
}
return count != 0; // always false// caller (resolve(...))
if (isSealedWithSchemaOneOf) {
model.setDiscriminator(null);
} else if (!resolveSubtypes(model, beanDesc, context, annotatedType.getJsonViewAnnotation())) {
model.setDiscriminator(null); // always taken, because resolveSubtypes always returns false
}
Discriminator discriminator = resolveDiscriminator(type, context);
if (discriminator != null) {
model.setDiscriminator(discriminator); // re-applied right after -> the clear above is a no-op
}History
countwith++countwas introduced in 2015 (5d92f811b, "fix for 802") where it was live: the return value drovemodel.setDiscriminator(null).- A 2017 "clean up" refactor (
023c99e96, implementation of AnyOf, OneOf, AllOf, (Not) support (models + core) #2277) removed the++countincrements but keptint count = 0;andreturn count != 0;, silently turning the return value into a constantfalse. - A later 2017 refactor (
745af316a) extractedresolveDiscriminator(...)and moved discriminator computation to run afterresolveSubtypes, which made thesetDiscriminator(null)a no-op (the value is recomputed and set immediately after).
Why it is dead code today (verified)
- Removing both
setDiscriminator(null)calls: fullswagger-coresuite still passes. - Adding
count++back alone: full suite still passes, no observable schema change.
Why "just fix it" is not safe
Restoring the original intent (compute the discriminator first, set it, then clear it when resolveSubtypes returns false and count++ makes that meaningful) breaks 6 existing tests on unmodified 3.0.0, all by dropping a discriminator that is expected to be present:
Ticket4904Test.testComposedSchemaWithDiscriminator(getDiscriminator()becomesnull)JsonSubTypesAndSchemaOneOfTest.beanWithJsonSubTypesImplementsBeanWithSchemaOneOfHasOnlyOneOfInModelSchemaObjectComplexPolymorphicModelTest.testDiscriminatorMappingIntegrity,...testSchemaStructureCorrectnessArrayOfSubclassTest.extractSubclassArray_oas30,..._oas31
The current ordering is load-bearing: resolveDiscriminator(...) is not pure (for mapping it calls context.resolve(...) on the mapping targets, which can redefine the parent model in the context), and the discriminator is applied last, on the model instance read back from the context. Clearing it based on resolveSubtypes removes discriminators for cases with an explicit @Schema(oneOf), sealed classes, and mapping-only types.
Options
- Leave as-is (document the dead code with a comment). No behavior change, lowest risk.
- Delete the dead code: remove
int count/return count != 0, makeresolveSubtypes(...)returnvoid(or keep abooleanwith a real meaning), and remove bothsetDiscriminator(null)branches (the sealed-class one and theresolveSubtypes-false one). Removing both is behavior-neutral — the full suite still passes — but it should be confirmed by the maintainers, since it encodes that nothing is expected to clear the discriminator there. - Investigate the intended semantics of the discriminator clear and re-implement it correctly (likely more involved).
Option 1 or 2 looks appropriate; option 3 needs a design decision from the maintainers.
Environment
swagger-core 3.0.0 (1ed537e47); reproduced with JDK 25 and the module's default test suite.
Activity
fix: resolveSubtypes() return value is dead code and model.setDiscrim…
fix: resolveSubtypes() return value is dead code and model.setDiscrim…
fix: resolveSubtypes() return value is dead code and model.setDiscrim…
fix: resolveSubtypes() return value is dead code and model.setDiscrim…
ewaostrowska commentedon Nov 27, 2026
Thank you for the report @vpelikh,
The issue should be resolved with the following PRs: