Skip to content

Commit 40c9739

Browse files
authored
Json property affects Record field serialization order (#3929)
1 parent 8fcf9ef commit 40c9739

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -437,17 +437,18 @@ protected void collectAll()
437437
LinkedHashMap<String, POJOPropertyBuilder> props = new LinkedHashMap<String, POJOPropertyBuilder>();
438438

439439
// First: gather basic data
440-
440+
final boolean isRecord = isRecordType();
441441
// 15-Jan-2023, tatu: [databind#3736] Let's avoid detecting fields of Records
442442
// altogether (unless we find a good reason to detect them)
443443
// 17-Apr-2023: Need Records' fields for serialization for cases like [databind#3895] & [databind#3628]
444-
if (!isRecordType() || _forSerialization) {
444+
if (!isRecord || _forSerialization) {
445445
_addFields(props); // note: populates _fieldRenameMappings
446446
}
447447
_addMethods(props);
448448
// 25-Jan-2016, tatu: Avoid introspecting (constructor-)creators for non-static
449449
// inner classes, see [databind#1502]
450-
if (!_classDef.isNonStaticInnerClass()) {
450+
// 13-May-2023, PJ: Need to avoid adding creators for Records when serializing [databind#3925]
451+
if (!_classDef.isNonStaticInnerClass() && !(_forSerialization && isRecord)) {
451452
_addCreators(props);
452453
}
453454

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.fasterxml.jackson.databind.records;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
5+
import com.fasterxml.jackson.databind.BaseMapTest;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
8+
public class RecordSerializationOrderTest extends BaseMapTest
9+
{
10+
record NestedRecordOne(String id, String email, NestedRecordTwo nestedRecordTwo) {}
11+
record NestedRecordOneWithJsonProperty(String id, String email,
12+
@JsonProperty("nestedProperty") NestedRecordTwo nestedRecordTwo) {}
13+
record NestedRecordOneWithJsonPropertyIndex(@JsonProperty(index = 2) String id,
14+
@JsonProperty(index = 0) String email,
15+
@JsonProperty(value = "nestedProperty", index = 1) NestedRecordTwo nestedRecordTwo) {}
16+
17+
@JsonPropertyOrder({"email", "nestedProperty", "id"})
18+
record NestedRecordOneWithJsonPropertyOrder(String id,
19+
String email,
20+
@JsonProperty(value = "nestedProperty") NestedRecordTwo nestedRecordTwo) {}
21+
22+
record NestedRecordTwo(String id, String passport) {}
23+
24+
private final ObjectMapper MAPPER = newJsonMapper();
25+
26+
/*
27+
/**********************************************************************
28+
/* Test methods, alternate constructors
29+
/**********************************************************************
30+
*/
31+
32+
public void testSerializationOrder() throws Exception {
33+
NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110");
34+
NestedRecordOne nestedRecordOne = new NestedRecordOne("1", "[email protected]", nestedRecordTwo);
35+
final String output = MAPPER.writeValueAsString(nestedRecordOne);
36+
final String expected = "{\"id\":\"1\",\"email\":\"[email protected]\",\"nestedRecordTwo\":{\"id\":\"2\",\"passport\":\"111110\"}}";
37+
assertEquals(expected, output);
38+
}
39+
40+
public void testSerializationOrderWithJsonProperty() throws Exception {
41+
NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110");
42+
NestedRecordOneWithJsonProperty nestedRecordOne =
43+
new NestedRecordOneWithJsonProperty("1", "[email protected]", nestedRecordTwo);
44+
final String output = MAPPER.writeValueAsString(nestedRecordOne);
45+
final String expected = "{\"id\":\"1\",\"email\":\"[email protected]\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}";
46+
assertEquals(expected, output);
47+
}
48+
49+
public void testSerializationOrderWithJsonPropertyIndexes() throws Exception {
50+
NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110");
51+
NestedRecordOneWithJsonPropertyIndex nestedRecordOne =
52+
new NestedRecordOneWithJsonPropertyIndex("1", "[email protected]", nestedRecordTwo);
53+
final String output = MAPPER.writeValueAsString(nestedRecordOne);
54+
final String expected = "{\"email\":\"[email protected]\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"},\"id\":\"1\"}";
55+
assertEquals(expected, output);
56+
}
57+
58+
public void testSerializationOrderWithJsonPropertyOrder() throws Exception {
59+
NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110");
60+
NestedRecordOneWithJsonPropertyOrder nestedRecordOne =
61+
new NestedRecordOneWithJsonPropertyOrder("1", "[email protected]", nestedRecordTwo);
62+
final String output = MAPPER.writeValueAsString(nestedRecordOne);
63+
final String expected = "{\"email\":\"[email protected]\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"},\"id\":\"1\"}";
64+
assertEquals(expected, output);
65+
}
66+
}

0 commit comments

Comments
 (0)