Skip to content

Honor @JsonView on @JsonAnySetter when deserializing - #6248

Open
pjfanning wants to merge 2 commits into
FasterXML:3.1from
pjfanning:any-setter-json-view
Open

pjfanning wants to merge 2 commits into
FasterXML:3.1from
pjfanning:any-setter-json-view

Conversation

@pjfanning

Copy link
Copy Markdown
Member

@JsonView on a @JsonAnySetter was ignored when deserializing: values for unmatched properties were passed to the any-setter no matter which view was active. Regular properties already honor views, and so does @JsonAnyGetter when serializing.

static class Bean {
    @JsonView(ViewA.class) public String a;
    @JsonAnySetter @JsonView(ViewB.class)
    public Map<String, Object> other = new LinkedHashMap<>();
}

mapper.readerWithView(ViewA.class).forType(Bean.class)
    .readValue("{\"a\":\"1\",\"x\":\"3\"}");
// before: other == {x=3}; after: other == {}

Changes

  • SettableAnyProperty now stores views (setViews(), visibleInView(), hasViews()), and withValueDeserializer() carries them over.
  • BeanDeserializerFactory reads an explicit @JsonView from the any-setter accessor. This works for methods, fields and creator parameters.
  • BeanDeserializerBuilder._anyViews() enables view processing when the any-setter has views.
  • New BeanDeserializerBase._skipIfAnySetterNotInView(), called at every point where a value goes to the any-setter: the vanilla, creator, record-update, unwrapped and external-type-id paths, BuilderBasedDeserializer, and ThrowableDeserializer. A hidden value is skipped. If FAIL_ON_UNEXPECTED_VIEW_PROPERTIES is enabled, it is reported instead, the same way as for regular properties.

Tradeoff

Only an explicit @JsonView on the any-setter counts. Class-level default views and DEFAULT_VIEW_INCLUSION are not applied to it. That differs from regular properties and from @JsonAnyGetter. Applying them would mean that with 3.x defaults (DEFAULT_VIEW_INCLUSION disabled), an un-annotated any-setter stops receiving any values once a view is active. That seemed too big a behavior change for a patch release. If full symmetry is wanted, it could be done in 3.3.

Tests

Added AnySetterViewDeserializationTest, which covers method, field, creator-bean, record creator-parameter and builder any-setters, an un-annotated any-setter (behavior unchanged), and FAIL_ON_UNEXPECTED_VIEW_PROPERTIES. Without the fix, 6 of its 7 tests fail. ./mvnw verify passes.

🤖 Generated with Claude Code

pjfanning and others added 2 commits September 29, 2026 10:36
Views declared on an any-setter were ignored on deserialization, so values
were passed to it regardless of the active view -- unlike regular properties,
and unlike `@JsonAnyGetter` on serialization. Now, if the any-setter is not
visible in the active view, the value is skipped (or, with
`DeserializationFeature.FAIL_ON_UNEXPECTED_VIEW_PROPERTIES`, reported),
same as for regular properties.

Only an explicit `@JsonView` on the any-setter itself is considered; class-level
default views are not applied, so un-annotated any-setters behave as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@gitar-bot

gitar-bot Bot commented Sep 29, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

🟡 Medium risk · Changes view-based filtering for unmatched properties across multiple deserialization paths

Adds support for honoring @JsonView on @JsonAnySetter during deserialization, bringing parity with how regular properties and @JsonAnyGetter already handle views. Properties outside the active view are now skipped by the any-setter, with optional error reporting when FAIL_ON_UNEXPECTED_VIEW_PROPERTIES is enabled. No issues found.

Review coverage

📋 Rules No rules evaluated

🧪 Functional validation Not enabled · Set up

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Counting what did not apply, without listing it.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@github-actions

Copy link
Copy Markdown

🧪 Code Coverage Report

Metric Coverage
Instructions coverage 81.48%
Branches branches 74.83%

Coverage data generated from JaCoCo test results

@github-actions

Copy link
Copy Markdown

🧪 Code Coverage Report

Metric Coverage
Instructions coverage 81.48%
Branches branches 74.82%

Coverage data generated from JaCoCo test results

This branch has not been deployed

No deployments
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.

1 participant