Skip to content
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

Weird Exception during read with Type info #97

Closed
pgelinas opened this issue Feb 5, 2014 · 4 comments
Closed

Weird Exception during read with Type info #97

pgelinas opened this issue Feb 5, 2014 · 4 comments
Milestone

Comments

@pgelinas
Copy link
Member

pgelinas commented Feb 5, 2014

I've hit a rather nasty problem when deserializing some XML, which seems to be caused by the order the attributes appear on an xml element. For exemple <Foo type=\"bad\" data=\"dummy\"><bar><bar>FOOBAR</bar></bar></Foo> parses fine, but inverting type and data attributes' order result in this weird exception:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: java.io.StringReader@42787d6a; line: 1, column: 36] (through reference chain: FooBad["bar"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:698)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:694)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.handleNonArray(StringCollectionDeserializer.java:210)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:164)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:154)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:1)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:538)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:155)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:110)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:81)
    at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:132)
    at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:41)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3008)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2113)

I've also noted that this doesn't cause problem when the property is not a List. I'll attach a unit test shortly.

I've debugged a little bit and it seems that because the type property is not the first encountered during parsing there's some buffering (with TokenBuffer) involved, which doesn't seem to play well with the Collection deserializer. This might also be related to the virtual wrapping necessary for collections?

pgelinas pushed a commit to pgelinas/jackson-dataformat-xml that referenced this issue Feb 5, 2014
@cowtowncoder
Copy link
Member

Interesting... seems odd that ordering of attributes should matter.

@pgelinas
Copy link
Member Author

pgelinas commented Feb 6, 2014

Ok, I've been able to debug the thing, here's what I found: when deserializing with type, when the type property is not the first found, the AsPropertyTypeDeserializer buffers the token stream until it finds the property. When it finds the type property, it creates a JsonParserSequence from the token buffer and the underlying xml parser, then resumes processing. When trying to deserialize a Collection, the Collection deserializer calls isExpectedStartArrayToken(), which FromXmlParser overrides to transform a START_OBJECT into a START_ARRAY. Unfortunately, both JsonParserSequence and JsonParserDelegate do not override this method to delegate to the underlying parser; fix is simply to add the appropriate override in JsonParserDelegate in jackson-databind.

pgelinas pushed a commit to pgelinas/jackson-core that referenced this issue Feb 6, 2014
pgelinas pushed a commit to pgelinas/jackson-core that referenced this issue Feb 6, 2014
cowtowncoder added a commit to FasterXML/jackson-core that referenced this issue Feb 6, 2014
@pgelinas pgelinas closed this as completed Feb 6, 2014
@cowtowncoder
Copy link
Member

Very good detective work...

cowtowncoder added a commit that referenced this issue Mar 21, 2014
Added unit test for issue #97.
@cowtowncoder
Copy link
Member

Looks like 'bad' test still fails tho?

cowtowncoder added a commit that referenced this issue Jun 25, 2015
@cowtowncoder cowtowncoder reopened this Apr 22, 2016
@cowtowncoder cowtowncoder modified the milestones: 2.12.-, 2.12.0 Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants