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

JDK Serializing and using Deserialized ObjectMapper loses linkage back from JsonParser.getCodec() #2038

Closed
243826 opened this issue May 15, 2018 · 13 comments
Milestone

Comments

@243826
Copy link

243826 commented May 15, 2018

I am attaching a test project which is linked against 2.9.5 version. The test errors out. But if I change the pom to link against 2.2.0, it passes. It seems that this is broken since 2.2.1.

java.lang.IllegalStateException: No ObjectCodec defined for parser, needed for deserialization
	at com.fasterxml.jackson.core.JsonParser._codec(JsonParser.java:1788)
	at com.fasterxml.jackson.core.JsonParser.readValueAsTree(JsonParser.java:1782)
	at com.celeral.serializedobjectmapper.NewClassTest$JSONWrapperDeserializer.deserialize(NewClassTest.java:87)
	at com.celeral.serializedobjectmapper.NewClassTest$JSONWrapperDeserializer.deserialize(NewClassTest.java:72)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
	at com.celeral.serializedobjectmapper.NewClassTest.testSerializationOfCodec(NewClassTest.java:164)
	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:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
	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:498)
	at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
	at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
	at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error:
  testSerializationOfCodec(com.celeral.serializedobjectmapper.NewClassTest): No ObjectCodec defined for parser, needed for deserialization

Tests run: 2, Failures: 0, Errors: 1, Skipped: 0

SerializedObjectMapper.zip

@cowtowncoder
Copy link
Member

Could you include relevant class(es) from the zip here? Exception suggests that linkage between JsonParser back to ObjectMapper is not working for some reason.

@243826
Copy link
Author

243826 commented May 15, 2018

There is only one file... NewClassTest.java

@cowtowncoder
Copy link
Member

Ok good luck. I hope someone has time to look into this.

@243826
Copy link
Author

243826 commented May 15, 2018

Do you know who can help? I found out what change in jackson-core between 2.2.0 and 2.2.1 broke this feature. But then I see recently (since late 2017) there has been a lot of refactoring to the code done by you; I can no longer do the mapping between the old code and the new code.

@cowtowncoder
Copy link
Member

That is a very, very old version. If you had added that one class in there, I would have looked at this earlier.

Looking at package now code looks ok, and I guess its JDK serialization that does something odd.

But instead of requiring use of getCodec() from parser, you can try achieving the same only through DeserializationContext, and creating JsonParser out of JsonNode.
That way you do not need mapper to use "treeToValue", but can instead use readValue(JsonParser, Class) in DeserializationContext. So something like

JsonNode n = context.readTree(jp);
JsonParser p2 = n.asParser();
MyType value = context.readValue(p2, MyType.class);
p2.close();

Hope this helps.

@cowtowncoder
Copy link
Member

Also: I think what you found is a bug, hope to find time to fix it if possible at some point.

@243826
Copy link
Author

243826 commented May 16, 2018

I tried to follow the comment with the code above with 2.9.5 version, but I am stuck at the first line itself.

JsonNode n = context.readTree(jp);

I did not find readTree method on DeserializationContext.

@243826
Copy link
Author

243826 commented May 16, 2018

btw - it seems this fixes the issue on top of jackson-core-2.9.5 :

diff --git a/src/main/java/com/fasterxml/jackson/core/JsonFactory.java b/src/main/java/com/fasterxml/jackson/core/JsonFactory.java
index 3abfbbb3..97152379 100644
--- a/src/main/java/com/fasterxml/jackson/core/JsonFactory.java
+++ b/src/main/java/com/fasterxml/jackson/core/JsonFactory.java
@@ -286,7 +286,7 @@ public class JsonFactory
      */
     protected JsonFactory(JsonFactory src, ObjectCodec codec)
     {
-        _objectCodec = null;
+        _objectCodec = codec;
         _factoryFeatures = src._factoryFeatures;
         _parserFeatures = src._parserFeatures;
         _generatorFeatures = src._generatorFeatures;

243826 added a commit to 243826/jackson-core that referenced this issue May 16, 2018
@cowtowncoder
Copy link
Member

@243826 Whoa. That seems like an ... interesting oversight :)

I'll want to add a regression test for this, since serializability of ObjectMapper is somewhat of a goal (although not often needed, is useful for some distributed systems). Thank you for reporting this!

As to readTree(), I think I must have added that in 3.0 for convenience, then. With 2.4 and above there is just readValue(...), for which you would give JsonNode.class as type and that should work similarly.

@cowtowncoder cowtowncoder added this to the 2.9.6 milestone May 16, 2018
@cowtowncoder cowtowncoder changed the title Serializing and using Deserialized ObjectMapper with Custom Serializer causes JSONParser.getCodec method to return NULL JDK Serializing and using Deserialized ObjectMapper loses linkage back from JsonParser.getCodec() May 16, 2018
@cowtowncoder
Copy link
Member

Ok. So. Will be fixed in 2.9.6, to be released within a week or two (before end of May).
It may still make sense to avoid the problem for this particular case. Thank you again for reporting and tracking down the problem. Interesting bug survived this long :)

@243826
Copy link
Author

243826 commented Jun 2, 2018 via email

@mserdur
Copy link

mserdur commented Nov 6, 2018

I could reproduce this with version 2.9.7

@cowtowncoder
Copy link
Member

@mserdur A reproduction would be needed to show how things do not work.

And if so, please file a new issue: release notes unfortunately do not work well with re-opened issues.

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