Skip to content

Commit 668b3be

Browse files
committed
minor simplifications for #1298 test
1 parent 24cdbde commit 668b3be

File tree

3 files changed

+107
-99
lines changed

3 files changed

+107
-99
lines changed

src/test/java/com/fasterxml/jackson/databind/jsontype/TestSubtypes.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,25 @@ public Default1125(int a0, int b0, int def0) {
133133
def = def0;
134134
}
135135
}
136-
136+
137+
// [databind#1311]
138+
@JsonTypeInfo(property = "type", use = JsonTypeInfo.Id.NAME, defaultImpl = Factory1311ImplA.class)
139+
interface Factory1311 { }
140+
141+
@JsonTypeName("implA")
142+
static class Factory1311ImplA implements Factory1311 { }
143+
144+
@JsonTypeName("implB")
145+
static class Factory1311ImplB implements Factory1311 { }
146+
137147
/*
138148
/**********************************************************
139149
/* Unit tests
140150
/**********************************************************
141151
*/
142152

143153
private final ObjectMapper MAPPER = new ObjectMapper();
144-
145-
// JACKSON-510
154+
146155
public void testPropertyWithSubtypes() throws Exception
147156
{
148157
ObjectMapper mapper = new ObjectMapper();
@@ -325,4 +334,14 @@ public void testIssue1125WithDefault() throws Exception
325334
assertEquals(5, impl.b);
326335
assertEquals(9, impl.def);
327336
}
337+
338+
// [databind#1311]
339+
public void testSubtypeAssignmentCheck() throws Exception
340+
{
341+
ObjectMapper mapper = new ObjectMapper();
342+
mapper.registerSubtypes(Factory1311ImplA.class, Factory1311ImplB.class);
343+
Factory1311ImplB result = mapper.readValue("{\"type\":\"implB\"}", Factory1311ImplB.class);
344+
assertNotNull(result);
345+
assertEquals(Factory1311ImplB.class, result.getClass());
346+
}
328347
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.io.StringWriter;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import com.fasterxml.jackson.annotation.*;
8+
import com.fasterxml.jackson.core.JsonGenerator;
9+
import com.fasterxml.jackson.databind.BaseMapTest;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
12+
// Test case for https://github.com/FasterXML/jackson-databind/issues/1298
13+
public class TestObjectIdWithUnwrapping1298 extends BaseMapTest
14+
{
15+
private static Long nextId = 1L;
16+
17+
public static final class ListOfParents{
18+
@JsonUnwrapped
19+
public List<Parent> parents = new ArrayList<>();
20+
21+
public void addParent( Parent parent) { parents.add(parent);}
22+
}
23+
24+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Parent.class)
25+
public static final class Parent {
26+
public Long id;
27+
28+
@JsonUnwrapped
29+
public Child child;
30+
public Parent() { this.id = nextId++;}
31+
}
32+
33+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Child.class)
34+
public static final class Child {
35+
36+
public Long id;
37+
38+
@JsonProperty
39+
private final String name;
40+
41+
public Child(@JsonProperty("name") String name) {
42+
this.name = name;
43+
this.id = TestObjectIdWithUnwrapping1298.nextId++;
44+
}
45+
}
46+
47+
public void testObjectIdWithRepeatedChild() throws Exception
48+
{
49+
ObjectMapper mapper = new ObjectMapper();
50+
mapper.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
51+
52+
// Equivalent to Spring _embedded for Bean w/ List property
53+
ListOfParents parents = new ListOfParents();
54+
55+
//Bean with Relationship
56+
Parent parent1 = new Parent();
57+
Child child1 = new Child("Child1");
58+
parent1.child = child1;
59+
parents.addParent(parent1);
60+
61+
// serialize parent1 and parent2
62+
String json = mapper
63+
// .writerWithDefaultPrettyPrinter()
64+
.writeValueAsString(parents);
65+
System.out.println("This works: " + json);
66+
67+
// Add parent3 to create ObjectId reference
68+
// Bean w/ repeated relationship from parent1, should generate ObjectId
69+
Parent parent3 = new Parent();
70+
parent3.child = child1;
71+
parents.addParent(parent3);
72+
StringWriter sw = new StringWriter();
73+
74+
try {
75+
mapper
76+
// .writerWithDefaultPrettyPrinter()
77+
.writeValue(sw, parents);
78+
} catch (Exception e) {
79+
System.out.println("Failed output so far: " + json);
80+
throw e;
81+
}
82+
83+
// System.out.println("Also works: " + sw);
84+
}
85+
}

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

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

0 commit comments

Comments
 (0)