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

Serialization fails if the type of a value is more generic than the declared type of a field #543

Closed
ahgittin opened this issue Sep 13, 2014 · 5 comments
Milestone

Comments

@ahgittin
Copy link

It seems that serializing an object which has a field with a generic declared type (e.g. Supplier<Anything>) fails if that field is set to a value whose type is more generic (e.g. value of type SupplierComposition<AnotherThing,Anything> which extends Supplier<Anything>).

(I will supply a test case illustrating this more precisely!)

com.fasterxml.jackson.databind.JsonMappingException: 1 (through reference chain: com.fasterxml.jackson.databind.jsontype.WrappedContainerWithField["animalContainer"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:187)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:647)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:114)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:2866)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2323)
    at com.fasterxml.jackson.databind.jsontype.TestWithGenerics.testJacksonXXX(TestWithGenerics.java:234)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:303)
    at com.fasterxml.jackson.databind.type.TypeBindings._resolve(TypeBindings.java:203)
    at com.fasterxml.jackson.databind.type.TypeBindings.findType(TypeBindings.java:121)
    at com.fasterxml.jackson.databind.type.TypeFactory._fromVariable(TypeFactory.java:847)
    at com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:400)
    at com.fasterxml.jackson.databind.type.TypeBindings.resolveType(TypeBindings.java:102)
    at com.fasterxml.jackson.databind.introspect.Annotated.getType(Annotated.java:60)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory._constructWriter(BeanSerializerFactory.java:703)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.findBeanProperties(BeanSerializerFactory.java:559)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.constructBeanSerializer(BeanSerializerFactory.java:344)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.findBeanSerializer(BeanSerializerFactory.java:263)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory._createSerializer2(BeanSerializerFactory.java:222)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:152)
    at com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:1061)
    at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:1040)
    at com.fasterxml.jackson.databind.SerializerProvider.findPrimaryPropertySerializer(SerializerProvider.java:523)
    at com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.findAndAddPrimarySerializer(PropertySerializerMap.java:77)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._findAndAddDynamic(BeanPropertyWriter.java:654)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:471)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)
    ... 24 more
@ahgittin
Copy link
Author

Test case added in ahgittin@c9d89b4 .

It seems the root cause is that SimpleType._narrow(subclass) simply applies the generic signature of the parent (SimpleType) to the subclass (subclass). That's not right is it? It means when TypeBindings._resolveBindings(subclass) tries to tighten bounds, it loops through the subclass's type parameters but then looks them up against the same index of the parent's.

@cowtowncoder
Copy link
Member

Sounds like you have found an unexpected edge case here.

@cowtowncoder cowtowncoder added this to the 2.5.0 milestone Sep 15, 2014
cowtowncoder added a commit that referenced this issue Sep 15, 2014
@cowtowncoder cowtowncoder modified the milestones: 2.5.0, 2.4.3 Sep 15, 2014
cowtowncoder added a commit that referenced this issue Sep 15, 2014
cowtowncoder added a commit that referenced this issue Sep 15, 2014
@cowtowncoder
Copy link
Member

FWIW, added equivalent test to java-classmate -- it passes. So this issue, too, would be properly solved if we could make Jackson use classmate.

@mohanmca
Copy link

I use latest version, and have the same issue while trying to serialize javax.mail.internet.MimeMessaage

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: javax.mail.internet.MimeMessage["session"]->javax.mail.Session["transport"]->com.sun.mail.smtp.SMTPTransport["urlname"]->javax.mail.URLName["url"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:190)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:671)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3383)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2759)
at com.glassteapot.sample.MailParser.toJson(Start.scala:61)
at com.glassteapot.sample.Start$.main(Start.scala:29)
at com.glassteapot.sample.Start.main(Start.scala)
Caused by: java.lang.NullPointerException
at java.net.Parts.(URL.java:1290)
at java.net.URL.(URL.java:399)
at java.net.URL.(URL.java:303)
at javax.mail.URLName.getURL(URLName.java:412)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:536)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
... 16 more

@cowtowncoder
Copy link
Member

@mohanmca what makes you think it is the same problem? It does not look like similar to me.

davorbonaci pushed a commit to GoogleCloudPlatform/DataflowJavaSDK that referenced this issue Feb 2, 2016
FasterXML/jackson-databind#543
was fixed and we have deprecated KvCoderBase and MapCoderBase
which were used as work arounds for the issue.

----Release Notes----

[]
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=112801278
robertwb pushed a commit to robertwb/DataflowJavaSDK that referenced this issue Feb 19, 2016
FasterXML/jackson-databind#543
was fixed and we have deprecated KvCoderBase and MapCoderBase
which were used as work arounds for the issue.

----Release Notes----

[]
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=112801278
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

3 participants