Skip to content

Commit

Permalink
Fix #3160
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 9, 2021
1 parent 06ff803 commit b4af86d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,11 @@ Tarekk Mohamed Abdalla (TarekkMA@github)
* Contributed #3154: Add ArrayNode#set(int index, primitive_type value)
(2.13.0)

Aritz Bastida (aritzbastida@github)
* Reported #3160: JsonStreamContext "currentValue" wrongly references to @JsonTypeInfo
annotated object
(2.13.0)

Morten Andersen-Gott (magott@github)
* Contributed #3174: DOM `Node` serialization omits the default namespace declaration
(2.13.0)
Expand Down
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Project: jackson-databind
(reported by Halil İbrahim Ş)
#3154: Add ArrayNode#set(int index, primitive_type value)
(contributed by Tarekk Mohamed A)
#3160: JsonStreamContext "currentValue" wrongly references to @JsonTypeInfo
annotated object
(reported by Aritz B)
#3174: DOM `Node` serialization omits the default namespace declaration
(contributed by Morten A-G)
#3177: Support `suppressed` property when deserializing `Throwable`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,14 +641,16 @@ public void serializeWithType(Object bean, JsonGenerator gen,
throws IOException
{
if (_objectIdWriter != null) {
gen.setCurrentValue(bean); // [databind#631]
// 08-Jul-2021, tatu: Should NOT yet set, would override "parent"
// context (wrt [databind#3160]
// gen.setCurrentValue(bean);
_serializeWithObjectId(bean, gen, provider, typeSer);
return;
}

gen.setCurrentValue(bean); // [databind#631]
WritableTypeId typeIdDef = _typeIdDef(typeSer, bean, JsonToken.START_OBJECT);
typeSer.writeTypePrefix(gen, typeIdDef);
gen.setCurrentValue(bean); // [databind#878]
if (_propertyFilterId != null) {
serializeFieldsFiltered(bean, gen, provider);
} else {
Expand Down Expand Up @@ -712,6 +714,8 @@ protected void _serializeObjectId(Object bean, JsonGenerator g,
WritableTypeId typeIdDef = _typeIdDef(typeSer, bean, JsonToken.START_OBJECT);

typeSer.writeTypePrefix(g, typeIdDef);
// 08-Jul-2021, tatu: Moved here from earlier place, wrt [databind#3160]
g.setCurrentValue(bean); // [databind#631]
objectId.writeAsField(g, provider, w);
if (_propertyFilterId != null) {
serializeFieldsFiltered(bean, g, provider);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.fasterxml.jackson.databind.ser.filter;

import java.util.*;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonStreamContext;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;

// [databind#3160]
public class CurrentObject3160Test extends BaseMapTest
{
@JsonFilter("myFilter")
@JsonPropertyOrder({ "id", "strategy", "set" })
public static class Item3160 {
public Collection<String> set;
public Strategy strategy;
public String id;

public Item3160(Collection<String> set, String id) {
this.set = set;
this.strategy = new Foo(42);
this.id = id;
}
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(name = "Foo", value = Foo.class) })
interface Strategy { }

static class Foo implements Strategy {
public int foo;

@JsonCreator
Foo(@JsonProperty("foo") int foo) {
this.foo = foo;
}
}

// from [databind#2475] test/filter
static class MyFilter3160 extends SimpleBeanPropertyFilter {
@Override
public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) throws Exception {
// Ensure that "current value" remains pojo
final JsonStreamContext ctx = jgen.getOutputContext();
final Object curr = ctx.getCurrentValue();

if (!(curr instanceof Item3160)) {
throw new RuntimeException("Field '"+writer.getName()
+"', context not that of `Item3160` instance but: "+curr.getClass().getName());
}
super.serializeAsField(pojo, jgen, provider, writer);
}
}

private final ObjectMapper MAPPER = newJsonMapper();

// [databind#2475]
public void testIssue2475() throws Exception
{
SimpleFilterProvider provider = new SimpleFilterProvider().addFilter("myFilter",
new MyFilter3160());
ObjectWriter writer = MAPPER.writer(provider);

// contents don't really matter that much as verification within filter but... let's
// check anyway
assertEquals(a2q("{'id':'ID-1','strategy':{'type':'Foo','foo':42},'set':[]}"),
writer.writeValueAsString(new Item3160(Arrays.asList(), "ID-1")));

assertEquals(a2q("{'id':'ID-2','strategy':{'type':'Foo','foo':42},'set':[]}"),
writer.writeValueAsString(new Item3160(Collections.emptySet(), "ID-2")));
}
}

0 comments on commit b4af86d

Please sign in to comment.