Skip to content
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

Add failing test #4452 #4477

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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