Skip to content

Commit ba28fe3

Browse files
authored
fix #4610 (#4611)
1 parent 526f11d commit ba28fe3

File tree

4 files changed

+83
-39
lines changed

4 files changed

+83
-39
lines changed

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Project: jackson-databind
1818
(reported by @dmelisso)
1919
#4595: No way to explicitly disable wrapping in custom annotation processor
2020
(reported by @SimonCockx)
21+
#4610: `DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS` does not work when
22+
used with Polymorphic type handling
23+
(fix by Joo-Hyuk K)
2124

2225
2.17.1 (04-May-2024)
2326

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,10 @@ public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) t
372372
// [databind#3838]: since 2.16 Uniform handling of missing objectId
373373
// only for the specific "empty JSON Object" case
374374
if (_objectIdReader != null && p.hasTokenId(JsonTokenId.ID_END_OBJECT)) {
375-
ctxt.reportUnresolvedObjectId(_objectIdReader, bean);
375+
// [databind#4610]: check if we are to skip failure
376+
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS)) {
377+
ctxt.reportUnresolvedObjectId(_objectIdReader, bean);
378+
}
376379
}
377380
if (_injectables != null) {
378381
injectValues(ctxt, bean);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.fasterxml.jackson.databind.objectid;
2+
3+
import java.util.List;
4+
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.fasterxml.jackson.annotation.*;
9+
10+
import com.fasterxml.jackson.databind.DeserializationFeature;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
13+
14+
import static org.junit.Assert.fail;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
public class ObjectIdSubTypes4610Test extends DatabindTestUtil
18+
{
19+
// Unused @JsonIdentityInfo
20+
@JsonIdentityInfo(generator = ObjectIdGenerators.StringIdGenerator.class)
21+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
22+
@JsonSubTypes({
23+
@JsonSubTypes.Type(value = EnumTypeDefinition.class, name = "enum"),
24+
@JsonSubTypes.Type(value = NumberTypeDefinition.class, name = "number")
25+
})
26+
interface TypeDefinition {
27+
}
28+
29+
static class EnumTypeDefinition implements TypeDefinition {
30+
public List<String> values;
31+
}
32+
33+
static class NumberTypeDefinition implements TypeDefinition {
34+
}
35+
36+
private final ObjectMapper MAPPER = newJsonMapper();
37+
38+
@Test
39+
public void shouldHandleTypeDefinitionJson() throws Exception {
40+
String input = "{\"@type\": \"number\"}";
41+
42+
TypeDefinition model = MAPPER.readerFor(TypeDefinition.class)
43+
.without(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS)
44+
.readValue(input);
45+
46+
Assertions.assertInstanceOf(NumberTypeDefinition.class, model);
47+
}
48+
49+
@Test
50+
public void testRoundTrip() throws Exception {
51+
// Ser
52+
String JSON = MAPPER.writeValueAsString(new NumberTypeDefinition());
53+
assertTrue(JSON.contains("@id"));
54+
55+
// Deser
56+
TypeDefinition model = MAPPER.readerFor(TypeDefinition.class)
57+
.with(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS)
58+
.readValue(JSON);
59+
Assertions.assertInstanceOf(NumberTypeDefinition.class, model);
60+
}
61+
62+
@Test
63+
public void shouldHandleTypeDefinitionJsonFail() throws Exception {
64+
String JSON = "{\"@type\": \"number\"}";
65+
66+
try {
67+
/*TypeDefinition model =*/ MAPPER.readerFor(TypeDefinition.class)
68+
.with(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS)
69+
.readValue(JSON);
70+
fail("Should not pass");
71+
} catch (Exception e) {
72+
assertTrue(e.getMessage().startsWith("No Object Id found for an instance of"));
73+
}
74+
}
75+
}
76+

src/test/java/com/fasterxml/jackson/failing/ObjectIdSubTypes4607Test.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)