-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JsonStreamContext "currentValue" wrongly references to @JsonTypeInfo annotated object #3160
Comments
…rary (implemented as a Jackson Property Filter) Reported here: FasterXML#3160
Quick question: is this during serialization (writing)? I think I'd need a minimal reproduction: I trust there is an issue, but fixing it requires careful verification as testing of current value stability is bit limited. |
…ue" wrongly references to @JsonTypeInfo annotated object)
…ue" wrongly references to @JsonTypeInfo annotated object)
Thanks for answering!! Yes, it happens during serialization. In fact, the issue is caused by the change made in As for the test case, I extended To fix this test, we can revert the changes in #631; that is, remove the statement package com.fasterxml.jackson.databind.ser;
public class TestCustomSerializers extends BaseMapTest
{
@JsonFilter("myFilter")
@JsonPropertyOrder({ "id", "strategy", "set" })
public static class Item2475 {
private Collection<String> set;
private Strategy strategy;
private String id;
public Item2475(Collection<String> set, String id) {
this.set = set;
this.strategy = new Foo(42);
this.id = id;
}
public Collection<String> getSet() {
return set;
}
public Strategy getStrategy() {
return strategy;
}
public String getId() {
return 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;
}
}
// [databind#2475]
public void testIssue2475() throws Exception {
SimpleFilterProvider provider = new SimpleFilterProvider().addFilter("myFilter", new MyFilter2475());
ObjectWriter writer = MAPPER.writer(provider);
// contents don't really matter that much as verification within filter but... let's
// check anyway
assertEquals(aposToQuotes("{'id':'ID-1','strategy':{'type':'Foo','foo':42},'set':[]}"),
writer.writeValueAsString(new Item2475(new ArrayList<String>(), "ID-1")));
assertEquals(aposToQuotes("{'id':'ID-2','strategy':{'type':'Foo','foo':42},'set':[]}"),
writer.writeValueAsString(new Item2475(new HashSet<String>(), "ID-2")));
}
} |
Thank you for adding a test case! I'll probably try to trim it down further. As to specific place(s), I'll have to look; but |
Exactly, I referred to @Override
public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider provider,
TypeSerializer typeSer) throws IOException {
if (_objectIdWriter != null) {
gen.setCurrentValue(bean); // [databind#631]
_serializeWithObjectId(bean, gen, provider, typeSer);
return;
}
gen.setCurrentValue(bean); // [databind#631]
...
} I look forward to your analysis. Thanks! |
Finally found time to check this and yes, ordering of that call is incorrect -- it needs to occur after START_OBJECT gets called, as that is the context in which current value should be updated. Test verifies this. |
Describe the bug
When a POJO attribute contains an object of a class annotated with
@JsonTypeInfo
, the methodserializeWithType()
changes the current value, so that it references that inner object when processing it (such asMailAttachment
, in the example below). This behavior was introduced due to #631.However, this current value also has a side-effect on subsequent sibling attributes. In the following sample, the JsonGenerator would state MailStatement as current value when processing "actions" and "properties". This behaviour breaks Jackson Property Filters, such as Squiggly (https://github.com/bohnman/squiggly/issues/62)
Note that if the Attachment class has no @JsonTypeInfo annotation, then "actions" and "properties" correctly state Issue object as current value.
Attribute processing, together with their JsonStreamContext "currentValue":
Version information
2.9 - 2.12.3
The text was updated successfully, but these errors were encountered: