Skip to content

Commit 768670d

Browse files
authored
Add more informative exception for back-references with record type (#5197)
1 parent a16107c commit 768670d

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Project: jackson-databind
2222
#5192: Record types are broken on Android when using R8
2323
(reported by @HelloOO7)
2424
(fix by @pjfanning)
25+
#5197: Add more informative exception for back-references with `record` type
26+
(fix by Joo-Hyuk K)
2527
- Generate SBOMs [JSTEP-14]
2628
2729
2.19.1 (13-Jun-2025)

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,11 @@ protected void addBackReferenceProperties(DeserializationContext ctxt,
787787
}
788788
}
789789
*/
790+
if (beanDesc.isRecordType()) {
791+
ctxt.reportBadTypeDefinition(beanDesc,
792+
"Cannot add back-reference to a `java.lang.Record` type (property '%s')",
793+
refProp.getName());
794+
}
790795
String refName = refProp.findReferenceName();
791796
builder.addBackReferenceProperty(refName, constructSettableProperty(ctxt,
792797
beanDesc, refProp, refProp.getPrimaryType()));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fasterxml.jackson.databind.records;
2+
3+
import java.util.List;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import com.fasterxml.jackson.annotation.JsonBackReference;
8+
import com.fasterxml.jackson.annotation.JsonManagedReference;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
11+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
12+
13+
import static org.junit.jupiter.api.Assertions.fail;
14+
15+
// [databind#5188] JsonManagedReference/JsonBackReference exception for records #5188
16+
// (cannot workd
17+
public class RecordBackReference5188Test
18+
extends DatabindTestUtil
19+
{
20+
private final ObjectMapper MAPPER = newJsonMapper();
21+
22+
@Test
23+
public void testRecordDeserializationFail() throws Exception {
24+
final String json = "{\"children\":[{}]}";
25+
26+
try {
27+
MAPPER.readValue(json, Parent.class);
28+
fail("Should not pass");
29+
} catch (InvalidDefinitionException e) {
30+
verifyException(e, "Cannot add back-reference to a `java.lang.Record` type");
31+
verifyException(e, "Invalid type definition for ");
32+
verifyException(e, "(property 'parent')");
33+
}
34+
}
35+
36+
record Child(@JsonBackReference Parent parent) {}
37+
38+
record Parent(@JsonManagedReference List<Child> children) {}
39+
40+
}

0 commit comments

Comments
 (0)