Skip to content

Honor @JsonValue accessor format with explicit serializers - #6269

Open
jin-sungdae wants to merge 4 commits into
FasterXML:3.xfrom
jin-sungdae:codex/jsonvalue-explicit-serializer-context
Open

jin-sungdae wants to merge 4 commits into
FasterXML:3.xfrom
jin-sungdae:codex/jsonvalue-explicit-serializer-context

Conversation

@jin-sungdae

Copy link
Copy Markdown

When a @jsonvalue accessor has both an explicit serializer and @jsonformat, the accessor's format is ignored. For example:

static class Value {
    @JsonValue
    @JsonSerialize(using = JavaUtilDateSerializer.class)
    @JsonFormat(pattern = "yyyy-MM", timezone = "UTC")
    public Date value = new Date(0L);
}

On current 3.x, serializing this value produces "1970-01-01T00:00:00.000Z" instead of "1970-01".

#6076 added an accessor-backed BeanProperty for dynamically or statically resolved @jsonvalue serializers, but the already-supplied serializer branch still contextualizes against only the enclosing property (or null at the root). Use the same _accessorProperty helper in that branch, preserving the existing enclosing-property precedence.

This is a follow-up to #4762 / #6076 and targets 3.x, where that helper is available. It does not introduce another annotation-merging implementation or change public APIs.

Regression coverage:

  • Explicit serializer on a @jsonvalue field.
  • Explicit serializer on a @jsonvalue method.
  • Nested wrapper without an enclosing format override.
  • Enclosing format override retaining precedence.

Validation on Microsoft OpenJDK 17.0.15 / Maven 3.9.9:

  • Before the fix: the new test class runs 4 tests, with 3 failures; the enclosing-override control already passes.
  • Focused run: 28 tests, 0 failures/errors/skips.
  • mvn -B -ntp clean verify: BUILD SUCCESS; 7,475 tests, 0 failures, 0 errors, 1 skipped.
  • git diff --check passes.

Prepared with AI assistance. The failure reproduction and verification commands above were executed locally.

@cowtowncoder cowtowncoder added the cla-needed PR needed, see https://github.com/FasterXML/jackson/blob/main/CONTRIBUTING.md#paperwork label Oct 6, 2026
@cowtowncoder

Copy link
Copy Markdown
Member

Thank you for submitting this PR; I'll review it as soon as I have time.

One thing needed before merging is CLA (if not yet sent); see https://github.com/FasterXML/jackson/blob/main/CONTRIBUTING.md#paperwork for details.

Looking forward to merging the pr !

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

🧪 Code Coverage Report

Metric Coverage Change
Instructions coverage 82.48% 📉 -0.010%
Branches branches 76.32% 📉 -0.030%

Coverage data generated from JaCoCo test results

@cowtowncoder cowtowncoder changed the title Honor @JsonValue accessor format with explicit serializers Honor @JsonValue accessor format with explicit serializers Oct 6, 2026
@cowtowncoder

Copy link
Copy Markdown
Member

@jin-sungdae any luck with CLA?

# Conflicts:
#	src/main/java/tools/jackson/databind/ser/jackson/JsonValueSerializer.java
@cowtowncoder

cowtowncoder commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

(by Claude Code on behalf of @cowtowncoder )


I merged 3.x into this branch and pushed it. There was one conflict in JsonValueSerializer.createContextual(), against the [databind#5380] withEmptyCheckDisabled() changes. I kept the 3.x structure and applied your change to the explicit-serializer path, which now also calls .withEmptyCheckDisabled(...). The full test suite passes.

A few review notes:

  1. Map key serializers use the same code path. BasicSerializerFactory.createKeySerializer() passes a non-null key delegate to JsonValueSerializer.construct() for @JsonKey/@JsonValue key types. So with this change, key serializers are also contextualized against the accessor-backed JsonValueProperty instead of the enclosing property. I tried a Date-returning @JsonValue with @JsonFormat(pattern = "yyyy") as a Map key, and the output was the same as before. Still, could you add a Map-key test to lock that behavior down?

  2. The type serializer and value serializer get different properties. vts.forProperty(ctxt, property) still uses the enclosing property, while the value serializer (and _property) now use _accessorProperty(property). Is that intentional? If it is, a short comment would help.

…reateContextual()`

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

Copy link
Copy Markdown

🧪 Code Coverage Report

Metric Coverage Change
Instructions coverage 83.36% 📈 +0.000%
Branches branches 77.79% 📉 -0.020%

Coverage data generated from JaCoCo test results

@gitar-bot

gitar-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk

Fixes @JsonValue accessors with an explicit serializer so their own @JsonFormat is honored, instead of being dropped in favor of the enclosing property context. No issues found.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

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

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

cla-needed PR needed, see https://github.com/FasterXML/jackson/blob/main/CONTRIBUTING.md#paperwork

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants