Skip to content

[Bug]: resolveSubtypes() return value is dead code and model.setDiscriminator(null) is a no-op #5333

Done

Description

@vpelikh

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

  • count with ++count was introduced in 2015 (5d92f811b, "fix for 802") where it was live: the return value drove model.setDiscriminator(null).
  • A 2017 "clean up" refactor (023c99e96, implementation of AnyOf, OneOf, AllOf, (Not) support (models + core) #2277) removed the ++count increments but kept int count = 0; and return count != 0;, silently turning the return value into a constant false.
  • A later 2017 refactor (745af316a) extracted resolveDiscriminator(...) and moved discriminator computation to run after resolveSubtypes, which made the setDiscriminator(null) a no-op (the value is recomputed and set immediately after).

Why it is dead code today (verified)

  • Removing both setDiscriminator(null) calls: full swagger-core suite 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() becomes null)
  • JsonSubTypesAndSchemaOneOfTest.beanWithJsonSubTypesImplementsBeanWithSchemaOneOfHasOnlyOneOfInModelSchemaObject
  • ComplexPolymorphicModelTest.testDiscriminatorMappingIntegrity, ...testSchemaStructureCorrectness
  • ArrayOfSubclassTest.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

  1. Leave as-is (document the dead code with a comment). No behavior change, lowest risk.
  2. Delete the dead code: remove int count/return count != 0, make resolveSubtypes(...) return void (or keep a boolean with a real meaning), and remove both setDiscriminator(null) branches (the sealed-class one and the resolveSubtypes-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.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions