Skip to content

Commit

Permalink
Json property affects Record field serialization order (FasterXML#3929)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored May 15, 2023
1 parent 8fcf9ef commit 40c9739
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
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);
}
}

0 comments on commit 40c9739

Please sign in to comment.