Skip to content

Commit b4af86d

Browse files
committed
Fix #3160
1 parent 06ff803 commit b4af86d

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,11 @@ Tarekk Mohamed Abdalla (TarekkMA@github)
13471347
* Contributed #3154: Add ArrayNode#set(int index, primitive_type value)
13481348
(2.13.0)
13491349

1350+
Aritz Bastida (aritzbastida@github)
1351+
* Reported #3160: JsonStreamContext "currentValue" wrongly references to @JsonTypeInfo
1352+
annotated object
1353+
(2.13.0)
1354+
13501355
Morten Andersen-Gott (magott@github)
13511356
* Contributed #3174: DOM `Node` serialization omits the default namespace declaration
13521357
(2.13.0)

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Project: jackson-databind
4747
(reported by Halil İbrahim Ş)
4848
#3154: Add ArrayNode#set(int index, primitive_type value)
4949
(contributed by Tarekk Mohamed A)
50+
#3160: JsonStreamContext "currentValue" wrongly references to @JsonTypeInfo
51+
annotated object
52+
(reported by Aritz B)
5053
#3174: DOM `Node` serialization omits the default namespace declaration
5154
(contributed by Morten A-G)
5255
#3177: Support `suppressed` property when deserializing `Throwable`

src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,16 @@ public void serializeWithType(Object bean, JsonGenerator gen,
641641
throws IOException
642642
{
643643
if (_objectIdWriter != null) {
644-
gen.setCurrentValue(bean); // [databind#631]
644+
// 08-Jul-2021, tatu: Should NOT yet set, would override "parent"
645+
// context (wrt [databind#3160]
646+
// gen.setCurrentValue(bean);
645647
_serializeWithObjectId(bean, gen, provider, typeSer);
646648
return;
647649
}
648650

649-
gen.setCurrentValue(bean); // [databind#631]
650651
WritableTypeId typeIdDef = _typeIdDef(typeSer, bean, JsonToken.START_OBJECT);
651652
typeSer.writeTypePrefix(gen, typeIdDef);
653+
gen.setCurrentValue(bean); // [databind#878]
652654
if (_propertyFilterId != null) {
653655
serializeFieldsFiltered(bean, gen, provider);
654656
} else {
@@ -712,6 +714,8 @@ protected void _serializeObjectId(Object bean, JsonGenerator g,
712714
WritableTypeId typeIdDef = _typeIdDef(typeSer, bean, JsonToken.START_OBJECT);
713715

714716
typeSer.writeTypePrefix(g, typeIdDef);
717+
// 08-Jul-2021, tatu: Moved here from earlier place, wrt [databind#3160]
718+
g.setCurrentValue(bean); // [databind#631]
715719
objectId.writeAsField(g, provider, w);
716720
if (_propertyFilterId != null) {
717721
serializeFieldsFiltered(bean, g, provider);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.fasterxml.jackson.databind.ser.filter;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.annotation.*;
6+
import com.fasterxml.jackson.core.JsonGenerator;
7+
import com.fasterxml.jackson.core.JsonStreamContext;
8+
9+
import com.fasterxml.jackson.databind.*;
10+
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
11+
import com.fasterxml.jackson.databind.ser.PropertyWriter;
12+
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
13+
14+
// [databind#3160]
15+
public class CurrentObject3160Test extends BaseMapTest
16+
{
17+
@JsonFilter("myFilter")
18+
@JsonPropertyOrder({ "id", "strategy", "set" })
19+
public static class Item3160 {
20+
public Collection<String> set;
21+
public Strategy strategy;
22+
public String id;
23+
24+
public Item3160(Collection<String> set, String id) {
25+
this.set = set;
26+
this.strategy = new Foo(42);
27+
this.id = id;
28+
}
29+
}
30+
31+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
32+
@JsonSubTypes({ @JsonSubTypes.Type(name = "Foo", value = Foo.class) })
33+
interface Strategy { }
34+
35+
static class Foo implements Strategy {
36+
public int foo;
37+
38+
@JsonCreator
39+
Foo(@JsonProperty("foo") int foo) {
40+
this.foo = foo;
41+
}
42+
}
43+
44+
// from [databind#2475] test/filter
45+
static class MyFilter3160 extends SimpleBeanPropertyFilter {
46+
@Override
47+
public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) throws Exception {
48+
// Ensure that "current value" remains pojo
49+
final JsonStreamContext ctx = jgen.getOutputContext();
50+
final Object curr = ctx.getCurrentValue();
51+
52+
if (!(curr instanceof Item3160)) {
53+
throw new RuntimeException("Field '"+writer.getName()
54+
+"', context not that of `Item3160` instance but: "+curr.getClass().getName());
55+
}
56+
super.serializeAsField(pojo, jgen, provider, writer);
57+
}
58+
}
59+
60+
private final ObjectMapper MAPPER = newJsonMapper();
61+
62+
// [databind#2475]
63+
public void testIssue2475() throws Exception
64+
{
65+
SimpleFilterProvider provider = new SimpleFilterProvider().addFilter("myFilter",
66+
new MyFilter3160());
67+
ObjectWriter writer = MAPPER.writer(provider);
68+
69+
// contents don't really matter that much as verification within filter but... let's
70+
// check anyway
71+
assertEquals(a2q("{'id':'ID-1','strategy':{'type':'Foo','foo':42},'set':[]}"),
72+
writer.writeValueAsString(new Item3160(Arrays.asList(), "ID-1")));
73+
74+
assertEquals(a2q("{'id':'ID-2','strategy':{'type':'Foo','foo':42},'set':[]}"),
75+
writer.writeValueAsString(new Item3160(Collections.emptySet(), "ID-2")));
76+
}
77+
}

0 commit comments

Comments
 (0)