-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add option DefaultTyping.EVERYTHING
to support Kotlin data classes
#2349
Comments
I am not sure I understand this request: default typing enabling is based on nominal base type (declared type of properties), and any such type declared Or could this be misunderstanding wrt role of |
It can, let me describe the use case we have. Is there another way to get the expected behavior? |
@sdeleuze No, that still does not make sense.
|
Ok so let's take a concrete focused repro where I implement the logic from our
This test is green with If I have a look to the Java bytecode, the only difference is the |
No, the problem is that due to Type Erasure, finding nominal base type differs for root values (no reference via property, only I actually have come to conclusion that it is often/usually bad idea to use either generic or polymorphic types as root values, and that it is better to have a wrapper as necessary. So you would have something like: public class Wrapper {
@JsonTypeInfo(....)
public Object value;
} which then establishes polymorphic nature of But as to your specific case: what would be nominal type to request on deserialization? That type would need to be the base type, and since it would not make much sense to use original type (if that is known why enable polymorphic type info in the first place). And on solution: I am not completely opposed to adding another type indicating "everything" if that is needed in practice. It should be also possible to separate intended base type from value type (former is for type id handling; latter actual Json(De)Serializer), and I think there is probably even an old issue for this. But it is not yet implemented. I will add this issue on my (long) W-I-P list since that old issue (once I locate) probably should be addressed. |
Hmmh. So after re-reading this again I finally understand the usage. I guess that although there are other problems involved here (ideally base type should be indicated somehow), adding of new choice is probably not a bad idea. I will add |
DefaultTyping.EVERYTHING
to support Kotlin data classes
@cowtowncoder could you please write an example for |
@Sadovnikov94 Unfortunately I don't use Kotlin myself so I don't think I can write good example code. But I don't think there is anything much more than enabling default typing with different setting. Note, however, that this setting is not needed at all if enabling polymorphic typing with |
val om = ObjectMapper()
.registerModule(KotlinModule())
.registerModule(JavaTimeModule())
.activateDefaultTyping(BasicPolymorphicTypeValidator.builder()
.allowIfBaseType(Any::class.java)
.build(), ObjectMapper.DefaultTyping.EVERYTHING)
val serializer = GenericJackson2JsonRedisSerializer(om) |
You better check the following issues before anyone uses |
Most usages of
ObjectMapper.DefaultTyping
areNON_FINAL
which seems the wider scope available, which is real issue with Kotlin data classes which are final by design and can't be open. As a consequence, usage in Spring Data Redis turn to be a very difficult journey (GenericJackson2JsonRedisSerializer
is very popular but not usable with Kotlin).Is it possible to include a more Kotlin friendly enum value that would support final classes?
The text was updated successfully, but these errors were encountered: