From 8305a0b6e6ecc99995c30af37049152bce966370 Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sat, 24 Feb 2024 23:12:17 +0900 Subject: [PATCH] Add test wrt @JsonPropertyOrder --- .../ser/JsonPropertyOrder4388Test.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/JsonPropertyOrder4388Test.java b/src/test/java/com/fasterxml/jackson/databind/ser/JsonPropertyOrder4388Test.java index d9130d23c2..cc4324c32d 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/JsonPropertyOrder4388Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/JsonPropertyOrder4388Test.java @@ -1,6 +1,7 @@ package com.fasterxml.jackson.databind.ser; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import org.junit.jupiter.api.Test; @@ -78,6 +79,21 @@ static class IgnoreOnFieldPojo { public Map map = new HashMap<>(); } + static class AlphabeticOrderOnAnyGetterBean { + @JsonPropertyOrder(alphabetic = true) + @JsonAnyGetter + public Map map = new LinkedHashMap<>(); + } + + @JsonPropertyOrder(alphabetic = true) + static class AlphabeticOrderOnClassBean { + public int c = 3; + public int a = 1; + public int b = 2; + @JsonAnyGetter + public Map map = new LinkedHashMap<>(); + } + private final ObjectMapper MAPPER = newJsonMapper(); // For [databind#4388] @@ -177,4 +193,35 @@ public void testIgnoreProperties() throws Exception { bean3.map.put("b", 3); assertEquals(a2q("{'a':1,'b':3}"), MAPPER.writeValueAsString(bean3)); } + + // Sorting works on @JsonAnyGetter, when adding @JsonPropertyOrder directly on the AnyGetter method + @Test + public void testSortingOnAnyGetter() throws Exception { + AlphabeticOrderOnAnyGetterBean bean = new AlphabeticOrderOnAnyGetterBean(); + bean.map.put("zd", 4); + bean.map.put("zc", 3); + bean.map.put("za", 1); + bean.map.put("zb", 2); + assertEquals(a2q("{" + + "'za':1," + + "'zb':2," + + "'zc':3," + + "'zd':4}"), MAPPER.writeValueAsString(bean)); + } + + // Sorting does not work on @JsonAnyGetter, when adding @JsonPropertyOrder on the class + @Test + public void testSortingOnClassNotPropagateToAnyGetter() throws Exception { + AlphabeticOrderOnClassBean bean = new AlphabeticOrderOnClassBean(); + bean.map.put("zc", 3); + bean.map.put("za", 1); + bean.map.put("zb", 2); + assertEquals(a2q("{" + + "'a':1," + + "'b':2," + + "'c':3," + + "'zc':3," + + "'za':1," + + "'zb':2}"), MAPPER.writeValueAsString(bean)); + } }