Skip to content

Commit

Permalink
Fix #4452 by introspecting Creators for Record serialization too
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 19, 2024
1 parent c95c2ca commit 1661f47
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Project: jackson-databind

2.18.0 (not yet released)

#4452: `@JsonProperty` not serializing field names properly
on `@JsonCreator` in Record
(reported by @Incara)
#4453: Allow JSON Integer to deserialize into a single-arg constructor of
parameter type `double`
(contributed by David M)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ protected void collectAll()
// 25-Jan-2016, tatu: Avoid introspecting (constructor-)creators for non-static
// inner classes, see [databind#1502]
// 13-May-2023, PJ: Need to avoid adding creators for Records when serializing [databind#3925]
if (!_classDef.isNonStaticInnerClass() && !(_forSerialization && isRecord)) {
// 18-May-2024, tatu: Serialization side does, however, require access to renaming
// etc (see f.ex [databind#4452]) so let's not skip
if (!_classDef.isNonStaticInnerClass()) { // && !(_forSerialization && isRecord)) {
_addCreators(props);
}

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

import java.util.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.records.Jdk8ConstructorParameterNameAnnotationIntrospector;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

// [databind#4452] : JsonProperty not serializing field names properly on JsonCreator in record #4452
class RecordCreatorSerialization4452Test {
Expand Down Expand Up @@ -37,32 +38,22 @@ public CreatorTestObject(
.annotationIntrospector(new Jdk8ConstructorParameterNameAnnotationIntrospector())
.build();

// supposed to pass, and yes it does
@Test
public void testPlain()
throws Exception
public void testPlain() throws Exception
{
String result = OBJECT_MAPPER.writeValueAsString(new PlainTestObject("test", 1));
assertEquals("{\"strField\":\"test\",\"intField\":1}", result);
}

// Should pass but doesn't
// It did pass in 2.15 or earlier versions, but it fails in 2.16 or later
@Test
public void testWithCreator()
throws Exception
public void testWithCreator() throws Exception
{
String result = OBJECT_MAPPER
String json = OBJECT_MAPPER
.writeValueAsString(new CreatorTestObject("test", 2, 1));
//System.err.println("JSON: "+json);

/*
Serializes to (using System.err.println("JSON: "+result); )
{"testFieldName":"test","testOtherField":3}
*/
assertTrue(result.contains("intField"));
assertTrue(result.contains("strField"));
@SuppressWarnings("unchecked")
Map<String, Object> asMap = (Map<String, Object>) OBJECT_MAPPER.readValue(json, Map.class);
assertEquals(new HashSet<>(Arrays.asList("intField", "strField")), asMap.keySet());
}

}
}

0 comments on commit 1661f47

Please sign in to comment.