From 0f86b7c075ba2e8ad829ce65965b0fba32b9b434 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 15 May 2023 12:35:22 +0100 Subject: [PATCH 1/5] add serialization order test --- .../databind/ser/SerializationOrderTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java index bd7caa259f..bcfa83b4d4 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java @@ -131,6 +131,72 @@ public BeanForStrictOrdering(@JsonProperty("c") int c, @JsonProperty("a") int a) public int getC() { return c; } } + static class NestedClassOne { + private String id; + private String name; + private NestedClassTwo nestedClassTwo; + + NestedClassOne(String id, + String name, + @JsonProperty(value = "nestedProperty") NestedClassTwo nestedClassTwo) { + this.id = id; + this.name = name; + this.nestedClassTwo = nestedClassTwo; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public NestedClassTwo getNestedClassTwo() { + return nestedClassTwo; + } + + public void setNestedClassTwo(NestedClassTwo nestedClassTwo) { + this.nestedClassTwo = nestedClassTwo; + } + } + + static class NestedClassTwo { + + private String id; + private String passport; + + NestedClassTwo(String id, + String passport) { + this.id = id; + this.passport = passport; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getPassport() { + return passport; + } + + public void setPassport(String passport) { + this.passport = passport; + } + } + /* /********************************************* /* Unit tests @@ -225,4 +291,12 @@ public void testStrictAlphaAndCreatorOrdering() throws Exception assertEquals(a2q("{'a':2,'b':0,'c':1}"), STRICT_ALPHA_MAPPER.writeValueAsString(new BeanForStrictOrdering(1, 2))); } + + public void testSerializationOrderWithJsonProperty() throws Exception { + NestedClassTwo nestedTwo = new NestedClassTwo("2", "111110"); + NestedClassOne nestedOne = new NestedClassOne("1", "test@records.com", nestedTwo); + final String output = MAPPER.writeValueAsString(nestedOne); + final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; + assertEquals(expected, output); + } } From 3d60f6ffa9bb1d72fd791923f0d61e39bb0f9ce3 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 15 May 2023 14:02:45 +0100 Subject: [PATCH 2/5] Update SerializationOrderTest.java --- .../databind/ser/SerializationOrderTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java index bcfa83b4d4..d9099a4ebf 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java @@ -134,6 +134,8 @@ public BeanForStrictOrdering(@JsonProperty("c") int c, @JsonProperty("a") int a) static class NestedClassOne { private String id; private String name; + + @JsonProperty(value = "nestedProperty") private NestedClassTwo nestedClassTwo; NestedClassOne(String id, @@ -169,6 +171,46 @@ public void setNestedClassTwo(NestedClassTwo nestedClassTwo) { } } + static class NestedClassOneWithDuplicatedJsonPropertyAnnotations { + private String id; + private String name; + + @JsonProperty(value = "nestedProperty") + private NestedClassTwo nestedClassTwo; + + NestedClassOneWithDuplicatedJsonPropertyAnnotations(String id, + String name, + @JsonProperty(value = "nestedProperty") NestedClassTwo nestedClassTwo) { + this.id = id; + this.name = name; + this.nestedClassTwo = nestedClassTwo; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public NestedClassTwo getNestedClassTwo() { + return nestedClassTwo; + } + + public void setNestedClassTwo(NestedClassTwo nestedClassTwo) { + this.nestedClassTwo = nestedClassTwo; + } + } + static class NestedClassTwo { private String id; @@ -299,4 +341,12 @@ public void testSerializationOrderWithJsonProperty() throws Exception { final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; assertEquals(expected, output); } + + public void testSerializationOrderWithDuplicatedJsonProperty() throws Exception { + NestedClassTwo nestedTwo = new NestedClassTwo("2", "111110"); + NestedClassOne nestedOne = new NestedClassOne("1", "test@records.com", nestedTwo); + final String output = MAPPER.writeValueAsString(nestedOne); + final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; + assertEquals(expected, output); + } } From 6df14ede3817751af1cf100dc02031d87917da39 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 8 Jun 2024 11:19:20 +0100 Subject: [PATCH 3/5] move failing tests --- .../databind/ser/SerializationOrderTest.java | 124 -------------- .../failing/SerializationOrder3932Test.java | 151 ++++++++++++++++++ 2 files changed, 151 insertions(+), 124 deletions(-) create mode 100644 src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java index a6cbbe5fbc..14ff8c447e 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/SerializationOrderTest.java @@ -136,114 +136,6 @@ public BeanForStrictOrdering(@JsonProperty("c") int c, @JsonProperty("a") int a) public int getC() { return c; } } - static class NestedClassOne { - private String id; - private String name; - - @JsonProperty(value = "nestedProperty") - private NestedClassTwo nestedClassTwo; - - NestedClassOne(String id, - String name, - @JsonProperty(value = "nestedProperty") NestedClassTwo nestedClassTwo) { - this.id = id; - this.name = name; - this.nestedClassTwo = nestedClassTwo; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public NestedClassTwo getNestedClassTwo() { - return nestedClassTwo; - } - - public void setNestedClassTwo(NestedClassTwo nestedClassTwo) { - this.nestedClassTwo = nestedClassTwo; - } - } - - static class NestedClassOneWithDuplicatedJsonPropertyAnnotations { - private String id; - private String name; - - @JsonProperty(value = "nestedProperty") - private NestedClassTwo nestedClassTwo; - - NestedClassOneWithDuplicatedJsonPropertyAnnotations(String id, - String name, - @JsonProperty(value = "nestedProperty") NestedClassTwo nestedClassTwo) { - this.id = id; - this.name = name; - this.nestedClassTwo = nestedClassTwo; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public NestedClassTwo getNestedClassTwo() { - return nestedClassTwo; - } - - public void setNestedClassTwo(NestedClassTwo nestedClassTwo) { - this.nestedClassTwo = nestedClassTwo; - } - } - - static class NestedClassTwo { - - private String id; - private String passport; - - NestedClassTwo(String id, - String passport) { - this.id = id; - this.passport = passport; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getPassport() { - return passport; - } - - public void setPassport(String passport) { - this.passport = passport; - } - } - /* /********************************************* /* Unit tests @@ -348,20 +240,4 @@ public void testStrictAlphaAndCreatorOrdering() throws Exception assertEquals(a2q("{'a':2,'b':0,'c':1}"), STRICT_ALPHA_MAPPER.writeValueAsString(new BeanForStrictOrdering(1, 2))); } - - public void testSerializationOrderWithJsonProperty() throws Exception { - NestedClassTwo nestedTwo = new NestedClassTwo("2", "111110"); - NestedClassOne nestedOne = new NestedClassOne("1", "test@records.com", nestedTwo); - final String output = MAPPER.writeValueAsString(nestedOne); - final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; - assertEquals(expected, output); - } - - public void testSerializationOrderWithDuplicatedJsonProperty() throws Exception { - NestedClassTwo nestedTwo = new NestedClassTwo("2", "111110"); - NestedClassOne nestedOne = new NestedClassOne("1", "test@records.com", nestedTwo); - final String output = MAPPER.writeValueAsString(nestedOne); - final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; - assertEquals(expected, output); - } } diff --git a/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java b/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java new file mode 100644 index 0000000000..5bc64e48a0 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java @@ -0,0 +1,151 @@ +package com.fasterxml.jackson.failing; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit tests for verifying that constraints on ordering of serialized + * properties are held. This is a regression test for [databind#3932]. + */ +public class SerializationOrder3932Test + extends DatabindTestUtil +{ + static class NestedClassOne { + private String id; + private String name; + + @JsonProperty(value = "nestedProperty") + private NestedClassTwo nestedClassTwo; + + NestedClassOne(String id, + String name, + @JsonProperty(value = "nestedProperty") NestedClassTwo nestedClassTwo) { + this.id = id; + this.name = name; + this.nestedClassTwo = nestedClassTwo; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public NestedClassTwo getNestedClassTwo() { + return nestedClassTwo; + } + + public void setNestedClassTwo(NestedClassTwo nestedClassTwo) { + this.nestedClassTwo = nestedClassTwo; + } + } + + static class NestedClassOneWithDuplicatedJsonPropertyAnnotations { + private String id; + private String name; + + @JsonProperty(value = "nestedProperty") + private NestedClassTwo nestedClassTwo; + + NestedClassOneWithDuplicatedJsonPropertyAnnotations(String id, + String name, + @JsonProperty(value = "nestedProperty") NestedClassTwo nestedClassTwo) { + this.id = id; + this.name = name; + this.nestedClassTwo = nestedClassTwo; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public NestedClassTwo getNestedClassTwo() { + return nestedClassTwo; + } + + public void setNestedClassTwo(NestedClassTwo nestedClassTwo) { + this.nestedClassTwo = nestedClassTwo; + } + } + + static class NestedClassTwo { + + private String id; + private String passport; + + NestedClassTwo(String id, + String passport) { + this.id = id; + this.passport = passport; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getPassport() { + return passport; + } + + public void setPassport(String passport) { + this.passport = passport; + } + } + + /* + /********************************************* + /* Unit tests + /********************************************* + */ + + private final ObjectMapper MAPPER = newJsonMapper(); + + @Test + public void testSerializationOrderWithJsonProperty() throws Exception { + NestedClassTwo nestedTwo = new NestedClassTwo("2", "111110"); + NestedClassOne nestedOne = new NestedClassOne("1", "test@records.com", nestedTwo); + final String output = MAPPER.writeValueAsString(nestedOne); + final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; + assertEquals(expected, output); + } + + @Test + public void testSerializationOrderWithDuplicatedJsonProperty() throws Exception { + NestedClassTwo nestedTwo = new NestedClassTwo("2", "111110"); + NestedClassOneWithDuplicatedJsonPropertyAnnotations nestedOne = new NestedClassOneWithDuplicatedJsonPropertyAnnotations( + "1", "test@records.com", nestedTwo); + final String output = MAPPER.writeValueAsString(nestedOne); + final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; + assertEquals(expected, output); + } +} From b655cdc0d8f0bb54b7ca435caf637ebd27a2377d Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 8 Jun 2024 09:27:53 -0700 Subject: [PATCH 4/5] Minor readability tweak --- .../jackson/failing/SerializationOrder3932Test.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java b/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java index 5bc64e48a0..8d70adf12c 100644 --- a/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java +++ b/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java @@ -135,7 +135,8 @@ public void testSerializationOrderWithJsonProperty() throws Exception { NestedClassTwo nestedTwo = new NestedClassTwo("2", "111110"); NestedClassOne nestedOne = new NestedClassOne("1", "test@records.com", nestedTwo); final String output = MAPPER.writeValueAsString(nestedOne); - final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; + final String expected = a2q( + "{'id':'1','email':'test@records.com','nestedProperty':{'id':'2','passport':'111110'}}"); assertEquals(expected, output); } @@ -145,7 +146,8 @@ public void testSerializationOrderWithDuplicatedJsonProperty() throws Exception NestedClassOneWithDuplicatedJsonPropertyAnnotations nestedOne = new NestedClassOneWithDuplicatedJsonPropertyAnnotations( "1", "test@records.com", nestedTwo); final String output = MAPPER.writeValueAsString(nestedOne); - final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; + final String expected = a2q( + "{'id':'1','email':'test@records.com','nestedProperty':{'id':'2','passport':'111110'}}"); assertEquals(expected, output); } } From bf44874dd9e0d78ce2fddf21453436b1203b408b Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 8 Jun 2024 09:28:16 -0700 Subject: [PATCH 5/5] ... --- .../fasterxml/jackson/failing/SerializationOrder3932Test.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java b/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java index 8d70adf12c..ffac6f7e94 100644 --- a/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java +++ b/src/test/java/com/fasterxml/jackson/failing/SerializationOrder3932Test.java @@ -1,9 +1,10 @@ package com.fasterxml.jackson.failing; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals;