Skip to content

Conversation

PatrickFeiring
Copy link

@PatrickFeiring PatrickFeiring commented Oct 4, 2025

Description

This PR adds inference of discriminator mappings when an explicit mapping is not present.

It 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 let Jackson compute the ids used in the mapping. This means that both explicitly listed names in @JsonSubTypes.Type annotations works, as well as ids that Jackson compute using the mechanism listed in JsonTypeInfo.Id.

Mappings are computed not only for sub types listed explicitly in JsonSubTypes annotations, but should also for modules that customize the AnnotationInstrospectors findSubtypes method, such as https://github.com/sigpwned/jackson-modules-java-17-sealed-classes.

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

} else {
SerializationConfig config = _mapper.getSerializationConfig();
AnnotationIntrospector introspector = config.getAnnotationIntrospector();
List<NamedType> subTypes = introspector.findSubtypes(
Copy link
Author

@PatrickFeiring PatrickFeiring Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As outlined in the issue comment, there are likely ways to find subtypes in a more exhaustive manner, but using the introspector seemed a good conservative starting point that covers the most common use-cases, as I don't know Jackson internals all that well
#3411 (comment)

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.

Consider the @JsonSubTypes for generating the discriminator mapping
1 participant