Skip to content

Commit

Permalink
Add failing test for #4452 (#4477)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim committed Apr 16, 2024
1 parent 3fb5637 commit e71e1a2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fasterxml.jackson.databind.failing;

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 {

public record PlainTestObject(
@JsonProperty("strField") String testFieldName,
@JsonProperty("intField") Integer testOtherField
) { }

public record CreatorTestObject(
String testFieldName,
Integer testOtherField
) {
@JsonCreator
public CreatorTestObject(
@JsonProperty("strField") String testFieldName,
@JsonProperty("someOtherIntField") Integer testOtherIntField,
@JsonProperty("intField") Integer testOtherField)
{
this(testFieldName, testOtherField + testOtherIntField);
}
}

private final ObjectMapper OBJECT_MAPPER =
JsonMapper.builder()
.annotationIntrospector(new Jdk8ConstructorParameterNameAnnotationIntrospector())
.build();

// supposed to pass, and yes it does
@Test
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
{
String result = OBJECT_MAPPER
.writeValueAsString(new CreatorTestObject("test", 2, 1));
/*
Serializes to:
{"testFieldName":"test","testOtherField":3}
*/
assertTrue(result.contains("intField"));
assertTrue(result.contains("strField"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;

class Jdk8ConstructorParameterNameAnnotationIntrospector extends JacksonAnnotationIntrospector
public class Jdk8ConstructorParameterNameAnnotationIntrospector extends JacksonAnnotationIntrospector
{
private static final long serialVersionUID = 1L;

Expand Down

0 comments on commit e71e1a2

Please sign in to comment.