Type id handling not implemented for type com.google.protobuf.MessageOrBuilder
njofce opened this issue · 3 comments
Trying to serialize a generic object with a proto parameter, but I get the following error:
Type id handling not implemented for type com.google.protobuf.MessageOrBuilder (by serializer of type com.hubspot.jackson.datatype.protobuf.builtin.serializers.MessageSerializer)
The class I am trying to serialize is shown below, and the parameter T is a proto message.
@Value.Immutable
@JsonSerialize(as = ImmutableGenericType.class)
@JsonDeserialize(as = ImmutableGenericType.class)
interface GenericType<T> extends JsonSerializableMessage {
@JsonTypeInfo(use = Id.CLASS, property = "class")
T data();
String someString();
}
The object mapper I am using is configured to handle polymorphic types by enabling default typing:
PolymorphicTypeValidator ptv =
BasicPolymorphicTypeValidator.builder()
.allowIfBaseType(JsonSerializableMessage.class)
.build();
objectMapper.activateDefaultTyping(ptv);
Everything works fine with java POJOs, but not when using a proto message. Is this an internal issue or am I missing something?
Thanks in advance
As the error message suggested, and after digging into the source code, this lib doesn't support type id handling for generic types. The solution I found is to have a custom Serializer and Deserializer, that uses an ObjectMapper with the Protobuf module installed to serialize the object (serializing the generated java class as well to deserialize).
One word of caution: combination of true generic types and polymorphic type handling is quite difficult to make work in general, since former is mostly "syntactic sugar" at declaration level (thanks to Type Erasure), whereas latter relies on some level of static type information (to know base type of dynamic subtypes).
It may well be possible to support for your own use case, and if so, custom (de)serializer is probably the way to go.
As the error message suggested, and after digging into the source code, this lib doesn't support type id handling for generic types. The solution I found is to have a custom Serializer and Deserializer, that uses an ObjectMapper with the Protobuf module installed to serialize the object (serializing the generated java class as well to deserialize).
@njofce-plenty Can you please post an example of this?