Skip to content

Commit 2a15b6c

Browse files
committed
Fix #5405, @jsonformat(shape = Shape.POJO) does not work for java.util.Map serialization via property annotation
1 parent 8f49fb7 commit 2a15b6c

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/main/java/tools/jackson/databind/SerializationContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,13 @@ public ValueSerializer<Object> findPrimaryPropertySerializer(JavaType valueType,
695695
ValueSerializer<Object> ser = _knownSerializers.untypedValueSerializer(valueType);
696696
if (ser == null) {
697697
ser = _createAndCachePropertySerializer(valueType, property);
698+
} else if (property != null) {
699+
// [databind#5405]: property-level @JsonFormat overrides must be applied even with cached serializers
700+
JsonFormat.Value overrides = property.findFormatOverrides(_config);
701+
if (overrides != null && !overrides.equals(JsonFormat.Value.empty())) {
702+
BeanDescription.Supplier beanDescRef = lazyIntrospectBeanDescription(valueType);
703+
ser = _checkShapeShifting(valueType, beanDescRef, property, ser);
704+
}
698705
}
699706
return handlePrimaryContextualization(ser, property);
700707
}

src/test/java/tools/jackson/databind/tofix/MapFormatShape5405Test.java renamed to src/test/java/tools/jackson/databind/MapFormatShape5405Test.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tools.jackson.databind.tofix;
1+
package tools.jackson.databind;
22

33
import java.util.*;
44

@@ -8,9 +8,7 @@
88
import com.fasterxml.jackson.annotation.JsonInclude;
99
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
1010

11-
import tools.jackson.databind.*;
1211
import tools.jackson.databind.testutil.DatabindTestUtil;
13-
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected;
1412

1513
import static org.junit.jupiter.api.Assertions.assertEquals;
1614

@@ -72,13 +70,23 @@ public Bean5405Override(int value) {
7270
// [databind#5045]: property overrides for @JsonFormat.shape won't work for Maps
7371
// 30-Nov-2025, tatu: Something about caching is the issue: if "b" commented out,
7472
// override appears to work; with "b" not
75-
@JacksonTestFailureExpected
7673
@Test
7774
public void serializeAsPOJOViaProperty() throws Exception
75+
{
76+
String result = MAPPER.writeValueAsString(new Bean5405Container(1,0,3));
77+
assertEquals(a2q(
78+
"{'a':{'extra':13,'empty':false},'c':{'extra':13,'empty':false}}"),
79+
result);
80+
}
81+
82+
// [databind#5405]:
83+
// 01-Dec-2025, JacksonJang: In this case, the @JsonFormat(shape = POJO) override behaves correctly even with b included.
84+
@Test
85+
public void serializeAsPOJOViaFullProperty() throws Exception
7886
{
7987
String result = MAPPER.writeValueAsString(new Bean5405Container(1,2,3));
8088
assertEquals(a2q(
81-
"{'a':{'extra':13,'empty':false},'b':{'value':2},'c':{'extra':13,'empty':false}}"),
89+
"{'a':{'extra':13,'empty':false},'b':{'value':2},'c':{'extra':13,'empty':false}}"),
8290
result);
8391
}
8492

0 commit comments

Comments
 (0)