Skip to content

GH-1297: Add Jackson PolymorphicTypeValidator to JsonInstanceSerializer - #1310

Draft
pjfanning wants to merge 1 commit into
apache:masterfrom
pjfanning:GH-1297-polymorphic-type-validator
Draft

pjfanning wants to merge 1 commit into
apache:masterfrom
pjfanning:GH-1297-polymorphic-type-validator

Conversation

@pjfanning

@pjfanning pjfanning commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Fixes #1297

ServiceInstance#getPayload() (and OldServiceInstance) is annotated with @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS), so JsonInstanceSerializer would load any class named in the JSON, relying only on Jackson's built-in gadget deny-list.

Changes

  • JsonInstanceSerializer now sets a PolymorphicTypeValidator on its ObjectMapper. By default:
    • polymorphic values with an Object base type (i.e. the erased payload T) must be the configured payloadClass or a subtype; anything else fails with InvalidTypeIdException
    • polymorphic values with any other declared base type (e.g. a nested @JsonTypeInfo field inside the user's payload) are allowed as before, so existing payloads with their own typed fields keep working
  • New public constructor JsonInstanceSerializer(Class<T>, boolean, PolymorphicTypeValidator) for users who need a different policy.
  • Users who pass Object.class as the payload class keep the previous (permissive) behaviour; they can use the new constructor to lock it down.

Related

Tests

  • disallowed payload class is rejected
  • custom validator allows/denies as configured
  • payload with a nested Id.CLASS field still round-trips

🤖 Generated with Claude Code

…rializer

ServiceInstance#getPayload uses @JsonTypeInfo(use = Id.CLASS), so Jackson
would instantiate any class named in the JSON. JsonInstanceSerializer now
sets a PolymorphicTypeValidator that only permits the configured payload
class (and its subtypes) for the payload. Nested polymorphic types with a
non-Object base type are unaffected. A new constructor allows supplying a
custom validator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pjfanning

Copy link
Copy Markdown
Member Author

@eolivelli this new polymorphic type validator may be a breaking change for some users and they would need to use the new constructor to provide their own polymorphic type validator.

@pjfanning
pjfanning marked this pull request as draft October 14, 2026 14:42
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.

Add Jackson PolymorphicTypeValidator

1 participant