Skip to content

feat: Infer discriminator mapping#4983

Open
PatrickFeiring wants to merge 1 commit into
swagger-api:masterfrom
PatrickFeiring:feat/infer-discriminator-mapping
Open

feat: Infer discriminator mapping#4983
PatrickFeiring wants to merge 1 commit into
swagger-api:masterfrom
PatrickFeiring:feat/infer-discriminator-mapping

Conversation

@PatrickFeiring

@PatrickFeiring PatrickFeiring commented Oct 4, 2025

Copy link
Copy Markdown

Description

This PR adds inference of discriminator mappings when an explicit mapping is not present, which should greatly reduce the need for explicit @DiscriminatorMapping annotations and make it easier to keep the mapping in sync.

The basic theme of the approach is to leave as much resolving to Jackson as possible. This is done by instantiating a type serializer for the class, and letting Jackson compute the ids used in the mapping. This means that type ids from all the following sources are supported

  • name field in @JsonSubTypes.Type annotations
  • @JsonTypeName annotations
  • Mechanisms given by JsonTypeInfo.Id in @JsonTypeInfo annotations

It also means we support things such as default property names when no property value is set on the @JsonTypeInfo annotation (@type) and the fallback to JsonTypeInfo.As.PROPERTY when JsonTypeInfo.As.EXTERNAL_PROPERTY is used on a class.

It tries to be conservative and add only a discriminator mapping when it is applicable, e.g. not for JsonTypeInfo.As.WRAPPER_ARRAY. It keeps the old behaviour of using an explicit discriminator mapping if present.

The main resolveDiscriminator do have a new signature, which is a breaking change for those subclassing and overriding. The new signature should be sensible as it matches that of resolveSubtypes, and resolving subtypes is similar to determining the discriminator mappings. It is tricky to keep this backwards compatible while still making it handle the more general cases, but migration path is super easy and hopefully most overriding is not needed any more.

I've also tested that it works on the Jackson 2.8 version. Despite the readme mentioning support from 2.4 and newer, the model resolver uses functionality in Jackson only available from 2.8, so I assumed that sufficed.

Closes: #3411

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

@PatrickFeiring PatrickFeiring force-pushed the feat/infer-discriminator-mapping branch 2 times, most recently from ccfd386 to f744259 Compare October 6, 2025 18:16
@PatrickFeiring PatrickFeiring force-pushed the feat/infer-discriminator-mapping branch 3 times, most recently from 4458fd5 to 0e36b96 Compare June 20, 2026 14:40
@PatrickFeiring

PatrickFeiring commented Jun 20, 2026

Copy link
Copy Markdown
Author

I've rebased the PR on master, made it a bit more conservative about when discriminator mappings are added, added support for default property names, and made the finding of subtype match the resolveSubtypes method. I've tested it on older Jackson versions as well to make it is compatible. Let me know if there is anything missing for it to be reviewed

@PatrickFeiring PatrickFeiring force-pushed the feat/infer-discriminator-mapping branch from 0e36b96 to c13f6fc Compare June 21, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider the @JsonSubTypes for generating the discriminator mapping

3 participants