Skip to content

Add more informative exception for back-references with record type #5197

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

Merged
merged 7 commits into from
Jun 18, 2025
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
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Project: jackson-databind
#5192: Record types are broken on Android when using R8
(reported by @HelloOO7)
(fix by @pjfanning)
#5197: Add more informative exception for back-references with `record` type
(fix by Joo-Hyuk K)
- Generate SBOMs [JSTEP-14]
2.19.1 (13-Jun-2025)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,11 @@ protected void addBackReferenceProperties(DeserializationContext ctxt,
}
}
*/
if (beanDesc.isRecordType()) {
ctxt.reportBadTypeDefinition(beanDesc,
"Cannot add back-reference to a `java.lang.Record` type (property '%s')",
refProp.getName());
}
String refName = refProp.findReferenceName();
builder.addBackReferenceProperty(refName, constructSettableProperty(ctxt,
beanDesc, refProp, refProp.getPrimaryType()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fasterxml.jackson.databind.records;

import java.util.List;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.fail;

// [databind#5188] JsonManagedReference/JsonBackReference exception for records #5188
// (cannot workd
public class RecordBackReference5188Test
extends DatabindTestUtil
{
private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testRecordDeserializationFail() throws Exception {
final String json = "{\"children\":[{}]}";

try {
MAPPER.readValue(json, Parent.class);
fail("Should not pass");
} catch (InvalidDefinitionException e) {
verifyException(e, "Cannot add back-reference to a `java.lang.Record` type");
verifyException(e, "Invalid type definition for ");
verifyException(e, "(property 'parent')");
}
}

record Child(@JsonBackReference Parent parent) {}

record Parent(@JsonManagedReference List<Child> children) {}

}