-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fb5637
commit e71e1a2
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...jdk17/java/com/fasterxml/jackson/databind/failing/RecordCreatorSerialization4452Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters