From 1eb4bc270247249421f09191ad424f7446ebe077 Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sun, 9 Feb 2025 00:03:30 +0900 Subject: [PATCH 1/2] Add tofix test for 4961 --- .../ShapeArrayWithAnyGetter4961Test.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java diff --git a/src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java b/src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java new file mode 100644 index 0000000000..01aa77d76d --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java @@ -0,0 +1,74 @@ +package com.fasterxml.jackson.databind.tofix; + +import java.util.Map; +import java.util.TreeMap; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +// [databind#4961] Serialization for `JsonFormat.Shape.ARRAY` does not work when there is `@JsonAnyGetter` +public class ShapeArrayWithAnyGetter4961Test + extends DatabindTestUtil +{ + + static class WrapperForAnyGetter { + public BeanWithAnyGetter value; + } + + @JsonPropertyOrder({ "firstProperty", "secondProperties", "anyProperty", "forthProperty" }) + @JsonFormat(shape = JsonFormat.Shape.ARRAY) + static class BeanWithAnyGetter { + public String firstProperty = "first"; + public String secondProperties = "second"; + public String forthProperty = "forth"; + @JsonAnyGetter + public Map getAnyProperty() { + Map map = new TreeMap<>(); + map.put("third_A", "third_A"); + map.put("third_B", "third_B"); + return map; + } + } + + final ObjectMapper MAPPER = newJsonMapper(); + + @Test + public void testSerializeArrayWithAnyGetterWithWrapper() throws Exception { + WrapperForAnyGetter wrapper = new WrapperForAnyGetter(); + wrapper.value = new BeanWithAnyGetter(); + + String json = MAPPER.writeValueAsString(wrapper); + + // Fails Actual :{"value":{"firstProperty":"first","secondProperties":"second","forthProperty":"forth","third_A":"third_A","third_B":"third_B"}} + assertEquals(a2q("{'value':" + + "[" + + "'first'," + + "'second'," + + "'forth'," + + "'third_A'," + + "'third_B'" + + "]" + + "}"), json); + } + + @Test + public void testSerializeArrayWithAnyGetterAsRoot() throws Exception { + BeanWithAnyGetter bean = new BeanWithAnyGetter(); + + String json = MAPPER.writeValueAsString(bean); + + // Fails Actual : {"firstProperty":"first","secondProperties":"second","forthProperty":"forth","third_A":"third_A","third_B":"third_B"} + assertEquals(a2q("[" + + "'first'," + + "'second'," + + "'forth'," + + "'third_A'," + + "'third_B'" + + "]"), json); + } +} From 5da2bb6526f9852e5c2dc52565737d173046c757 Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sun, 9 Feb 2025 00:07:57 +0900 Subject: [PATCH 2/2] Add `tofix` annotations --- .../databind/tofix/ShapeArrayWithAnyGetter4961Test.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java b/src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java index 01aa77d76d..1058f58ecb 100644 --- a/src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,6 +38,7 @@ public Map getAnyProperty() { final ObjectMapper MAPPER = newJsonMapper(); + @JacksonTestFailureExpected @Test public void testSerializeArrayWithAnyGetterWithWrapper() throws Exception { WrapperForAnyGetter wrapper = new WrapperForAnyGetter(); @@ -56,6 +58,7 @@ public void testSerializeArrayWithAnyGetterWithWrapper() throws Exception { "}"), json); } + @JacksonTestFailureExpected @Test public void testSerializeArrayWithAnyGetterAsRoot() throws Exception { BeanWithAnyGetter bean = new BeanWithAnyGetter();