Skip to content

Commit e71e1a2

Browse files
authored
Add failing test for #4452 (#4477)
1 parent 3fb5637 commit e71e1a2

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fasterxml.jackson.databind.failing;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.json.JsonMapper;
7+
import com.fasterxml.jackson.databind.records.Jdk8ConstructorParameterNameAnnotationIntrospector;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
13+
// [databind#4452] : JsonProperty not serializing field names properly on JsonCreator in record #4452
14+
class RecordCreatorSerialization4452Test {
15+
16+
public record PlainTestObject(
17+
@JsonProperty("strField") String testFieldName,
18+
@JsonProperty("intField") Integer testOtherField
19+
) { }
20+
21+
public record CreatorTestObject(
22+
String testFieldName,
23+
Integer testOtherField
24+
) {
25+
@JsonCreator
26+
public CreatorTestObject(
27+
@JsonProperty("strField") String testFieldName,
28+
@JsonProperty("someOtherIntField") Integer testOtherIntField,
29+
@JsonProperty("intField") Integer testOtherField)
30+
{
31+
this(testFieldName, testOtherField + testOtherIntField);
32+
}
33+
}
34+
35+
private final ObjectMapper OBJECT_MAPPER =
36+
JsonMapper.builder()
37+
.annotationIntrospector(new Jdk8ConstructorParameterNameAnnotationIntrospector())
38+
.build();
39+
40+
// supposed to pass, and yes it does
41+
@Test
42+
public void testPlain()
43+
throws Exception
44+
{
45+
String result = OBJECT_MAPPER.writeValueAsString(new PlainTestObject("test", 1));
46+
assertEquals("{\"strField\":\"test\",\"intField\":1}", result);
47+
}
48+
49+
// Should pass but doesn't
50+
// It did pass in 2.15 or earlier versions, but it fails in 2.16 or later
51+
@Test
52+
public void testWithCreator()
53+
throws Exception
54+
{
55+
String result = OBJECT_MAPPER
56+
.writeValueAsString(new CreatorTestObject("test", 2, 1));
57+
/*
58+
Serializes to:
59+
60+
{"testFieldName":"test","testOtherField":3}
61+
62+
*/
63+
assertTrue(result.contains("intField"));
64+
assertTrue(result.contains("strField"));
65+
}
66+
67+
}

src/test-jdk17/java/com/fasterxml/jackson/databind/records/Jdk8ConstructorParameterNameAnnotationIntrospector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
66
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
77

8-
class Jdk8ConstructorParameterNameAnnotationIntrospector extends JacksonAnnotationIntrospector
8+
public class Jdk8ConstructorParameterNameAnnotationIntrospector extends JacksonAnnotationIntrospector
99
{
1010
private static final long serialVersionUID = 1L;
1111

0 commit comments

Comments
 (0)