Skip to content

Support single-Map @JsonAnySetter methods#6075

Open
goutamadwant wants to merge 3 commits into
FasterXML:3.xfrom
goutamadwant:fix-anysetter-map-method
Open

Support single-Map @JsonAnySetter methods#6075
goutamadwant wants to merge 3 commits into
FasterXML:3.xfrom
goutamadwant:fix-anysetter-map-method

Conversation

@goutamadwant

@goutamadwant goutamadwant commented Jul 7, 2026

Copy link
Copy Markdown

Fixes #4889

Root cause

@JsonAnySetter discovery handled fields, creator parameters, and two-argument setter methods, but explicitly annotated one-argument methods were still treated as regular setters. That meant a method like setOther(Map<String, Object> other) was not recognized as an any-setter target.

Change

This adds support for explicitly annotated single-argument Map any-setter methods. Unknown properties are collected into a map using the existing any-setter key/value deserialization flow, then the annotated method is invoked once with the collected values.

Verification

  • ./mvnw -q -Dtest=tools.jackson.databind.deser.AnySetterTest#testJsonAnySetterOnMapMethod test
  • ./mvnw -q -Dtest=tools.jackson.databind.deser.AnySetterTest test
  • ./mvnw -q -Dtest=tools.jackson.databind.deser.AnySetterTest,tools.jackson.databind.deser.builder.BuilderSimpleTest,tools.jackson.databind.deser.creators.AnySetterForCreator562Test,tools.jackson.databind.introspect.POJOPropertiesCollectorTest test
  • ./mvnw -q test
  • ./mvnw -q -DskipTests animal-sniffer:check
  • git diff --check

@JooHyukKim

Copy link
Copy Markdown
Member

Can you specify issue number on test methods? @goutamadwant See neighboring test cases as example

@goutamadwant goutamadwant force-pushed the fix-anysetter-map-method branch from 75278f1 to 41e77d2 Compare July 8, 2026 06:40
@goutamadwant

Copy link
Copy Markdown
Author

Can you specify issue number on test methods? @goutamadwant See neighboring test cases as example

Thanks @JooHyukKim, updated the new test method names to include 4889, matching the neighboring test cases. Let me know if any suggestions. thanks!

@github-actions

Copy link
Copy Markdown

🧪 Code Coverage Report

Metric Coverage Change
Instructions coverage 81.70% 📉 -0.010%
Branches branches 75.19% 📉 -0.040%

Coverage data generated from JaCoCo test results

@cowtowncoder cowtowncoder changed the title Support single-Map @JsonAnySetter methods Support single-Map @JsonAnySetter methods Jul 10, 2026

@JooHyukKim JooHyukKim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We need JavaDocs as well.
Thanks!

PropertyMetadata.STD_OPTIONAL);
if (am.getParameterCount() == 1) {
JavaType paramType = am.getParameterType(0);
if (!paramType.isMapLikeType()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think single argument doesn't always mean any-setter is intended?

if (anyMethod.getParameterCount() == 1) {
Class<?> type = anyMethod.getRawParameterType(0);
if (!Map.class.isAssignableFrom(type)) {
throw new IllegalArgumentException(String.format(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure if we can simply throw error, considering we are doing minor version fix...? 🤔

throw new UnsupportedOperationException("Cannot call createParameterObject() on " + getClass().getName());
}

public void finish(DeserializationContext ctxt, Object instance) throws JacksonException { }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Method name is generic too much.
Can we do both below?

  1. Change name
  2. Add JavaDoc

I'm talking about all finish methods

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.

Support @JsonAnySetter on setter method (single Map parameter)

3 participants