question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Looked up serializer contains incompletely configured ObjectIdWriter

See original GitHub issue

The 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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Sep 14, 2021

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 from resolve() method of ResolvableSerializer (which is rarely if ever needed by non-POJO serializers) which has to modify instance directly (and has different semantics).

0reactions
cowtowncodercommented, Sep 28, 2021

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 various writeValue() calls.

Instead, SerialierProvider should be provided as part of context for all calls where it may be needed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found