Skip to content

Json property affects serialization order #3929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,18 @@ protected void collectAll()
LinkedHashMap<String, POJOPropertyBuilder> props = new LinkedHashMap<String, POJOPropertyBuilder>();

// First: gather basic data

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.fasterxml.jackson.databind.records;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;

public class RecordSerializationOrderTest extends BaseMapTest
{
record NestedRecordOne(String id, String email, NestedRecordTwo nestedRecordTwo) {}
record NestedRecordOneWithJsonProperty(String id, String email,
@JsonProperty("nestedProperty") NestedRecordTwo nestedRecordTwo) {}
record NestedRecordOneWithJsonPropertyIndex(@JsonProperty(index = 2) String id,
@JsonProperty(index = 0) String email,
@JsonProperty(value = "nestedProperty", index = 1) NestedRecordTwo nestedRecordTwo) {}

@JsonPropertyOrder({"email", "nestedProperty", "id"})
record NestedRecordOneWithJsonPropertyOrder(String id,
String email,
@JsonProperty(value = "nestedProperty") NestedRecordTwo nestedRecordTwo) {}

record NestedRecordTwo(String id, String passport) {}

private final ObjectMapper MAPPER = newJsonMapper();

/*
/**********************************************************************
/* Test methods, alternate constructors
/**********************************************************************
*/

public void testSerializationOrder() throws Exception {
NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110");
NestedRecordOne nestedRecordOne = new NestedRecordOne("1", "[email protected]", nestedRecordTwo);
final String output = MAPPER.writeValueAsString(nestedRecordOne);
final String expected = "{\"id\":\"1\",\"email\":\"[email protected]\",\"nestedRecordTwo\":{\"id\":\"2\",\"passport\":\"111110\"}}";
assertEquals(expected, output);
}

public void testSerializationOrderWithJsonProperty() throws Exception {
NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110");
NestedRecordOneWithJsonProperty nestedRecordOne =
new NestedRecordOneWithJsonProperty("1", "[email protected]", nestedRecordTwo);
final String output = MAPPER.writeValueAsString(nestedRecordOne);
final String expected = "{\"id\":\"1\",\"email\":\"[email protected]\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}";
assertEquals(expected, output);
}

public void testSerializationOrderWithJsonPropertyIndexes() throws Exception {
NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110");
NestedRecordOneWithJsonPropertyIndex nestedRecordOne =
new NestedRecordOneWithJsonPropertyIndex("1", "[email protected]", nestedRecordTwo);
final String output = MAPPER.writeValueAsString(nestedRecordOne);
final String expected = "{\"email\":\"[email protected]\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"},\"id\":\"1\"}";
assertEquals(expected, output);
}

public void testSerializationOrderWithJsonPropertyOrder() throws Exception {
NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110");
NestedRecordOneWithJsonPropertyOrder nestedRecordOne =
new NestedRecordOneWithJsonPropertyOrder("1", "[email protected]", nestedRecordTwo);
final String output = MAPPER.writeValueAsString(nestedRecordOne);
final String expected = "{\"email\":\"[email protected]\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"},\"id\":\"1\"}";
assertEquals(expected, output);
}
}