Looked up serializer contains incompletely configured ObjectIdWriter
See original GitHub issueThe following test case
class SerializerLookupTest {
@Test
void lookedUpSerializerSerializesObjectId() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SerializerProvider serializers = mapper.getSerializerProviderInstance();
JsonSerializer<Object> serializer = serializers.findValueSerializer(Sample.class);
serializer.serialize(new Sample(), mapper.getFactory().createGenerator(System.out), serializers);
}
@JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class, property = "@jsonObjectId")
public class Sample {}
}
fails with:
ava.lang.NullPointerException
at com.fasterxml.jackson.databind.ser.impl.WritableObjectId.writeAsField(WritableObjectId.java:79)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase._serializeWithObjectId(BeanSerializerBase.java:678)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:171)
at dcs.spring.hateoas.poc.endpoint.SerializerLookupTest.lookedUpSerializerSerializesObjectId(SerializerLookupTest.java:39)
The gist of it is that serializers looked up from the SerializerProvider
contain incompletely configured ObjectIdWriter
instances (essentially the serializer held in it is null
, likely because ObjectIdWriter.construct(…)
initializes it with null
). One way to make this work is to invoke ….createContextual(providers, null)
but, I’d argue, that either the serializer should be properly initialized right away or the serialization of the object id has to be able to deal with the fact that the underlying serializer is null
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Django REST Framework: adding additional field to ...
I want to serialize a model, but want to include an additional field that requires doing some database lookups on the model instance...
Read more >Serializers - Django REST framework
We'll declare a serializer that we can use to serialize and deserialize data that corresponds to Comment objects. Declaring a serializer looks ......
Read more >BeanSerializer (jackson-databind 2.12.0 API) - FasterXML
Serializer class that can serialize Java objects that map to JSON Object output. ... resulting serializer, to figure out actual serializers for final...
Read more >Serializers - Axon Reference Guide
The Message Serializer is in charge of de-/serializing the command and query messages (used in a distributed application set up).
Read more >EsotericSoftware/kryo: Java binary serialization and cloning
Kryo is a fast and efficient binary object graph serialization framework for Java. The goals of the project are high speed, low size,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Right, implement
ContextualSerializer
does make serializer get callback method called during its contextualization.I mostly mentioned delegating base type as possible inspiration, to help implement what you need. It may or may not be useful on its own.
One caveat on
createContextual()
is that it can get called multiple times on “blueprint” serializer instance: so if any changes are needed, a fresh new instance of the serializer must be constructed. This is different fromresolve()
method ofResolvableSerializer
(which is rarely if ever needed by non-POJO serializers) which has to modify instance directly (and has different semantics).Ok, at this point I should probably just document that non-Jackson code should never try to construct a
SerializerProvider
, ever. This is not an extension point meant for external use: it is only there to be used for variouswriteValue()
calls.Instead,
SerialierProvider
should be provided as part of context for all calls where it may be needed.