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

Fix #4452 by introspecting Creators for Record serialization too #4537

Merged
merged 1 commit into from
May 19, 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
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());
}

}
}