-
-
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
Failure to resolve generic type parameters on serialization #2632
Comments
Thank you for submitting this issue! As a general warning: generic types and polymorphic type handling do not play well together, unfortunately. So I usually avoid doing combination. This is not to say it can not work, and use case here looks reasonable to me. Now... you definitely should not see an |
Looking at the original code, there is one part that does not make sense to me (possibly since I do not know Kotlin very well):
seems illegal, because
and So my problem is that I do not know how to create equivalent Java-only reproduction of the issue. |
Hi @cowtowncoder, thank you for answering :) The code is legal Kotlin code, |
Right, I sort of understand what Exception seems to be from JDK version 9 or above; does exception different on JDK 8? If so, that might give another clue on what might be happening. |
@cowtowncoder I tried to run the same code with java 1.8.0_232 (Amazon Corretto JDK), but the error is the same. |
@simonedavico Ok. I guess that at least makes it easier to reproduce automatically as I work on Java 8 (I have JDK 11 locally and it's easy to switch, but all release builds must be on pre-Java 9). |
@cowtowncoder Ok, is there any way I can help? Unfortunately at the moment I do not have the time to explore Jackson codebase to understand what causes the bug. I will try to give you more context on how I reproduce it: I am writing a service with Micronaut (version @Get("/foo")
fun getFoo() = Foo(
bar = Either.Left("aaa"),
second = "bbbb"
) This get serialized by a Micronaut managed The curious thing is that if I define a controller like this @Get("/plain")
fun getPlain() = Either.Left("aaa") Jackson can serialize it correctly, and returns the string |
I think that even simple Kotlin-only test, PR, would be fine -- I should be able to debug to see why type resolution crashes, and then perhaps create Java-only equivalent. But even without that it should be possible to fix, it's just little bit more prone to regression as we do not yet have good system to rebuild all components (that is: when |
Hmmh. One practical problem I have is that:
so at this point I can reproduce Kotlin test but haven't found an efficient way to really troubleshoot it. |
Attempt at creating plain Java version not successful either; I can not find a way to satisfy type constraints as |
@cowtowncoder Kotlin's |
@frost13it thank you for repro; updated priority of this issue to hopefully have a look tonight or at least this week. |
EitherTest.java
|
Ok so, first things first: changed code to avoid Now... while type parameterization is, as far as I can see, wrong, this probably should NOT fail when serializing: after all, object instance exists. Failing on deserialization would seem legit. Another possibility, altogether, could also be to consider whether substitution from |
Was able to actually prevent this failure: changed the type specialization in such a way that validation problems are ignored on serialization side. That solves this case and in general should not break existing working cases, although it is possible there are other cases where something different is needed to actually resolve type discrepancy. But we'll cross that bridge when we get there -- no known problems exist. |
@cowtowncoder thank you! I was finally able to try this and I can confirm it works like a charm!
which is a nice starting point. Would there be a way for me to configure the annotations in such a way that it serialises to the plain `value? For example, for the above I would like to get:
EDIT: I was able to achieve the serialisation format I wanted, I am pasting here my implementation in case anyone needs it. Feedback is welcome of course, I am no expert with Jackson API. class EitherSerializer: JsonSerializer<Either<Any, Any>>() {
override fun serialize(value: Either<Any, Any>?, gen: JsonGenerator?, provider: SerializerProvider?) {
when (value) {
is Either.Left -> provider!!.defaultSerializeValue(value.value, gen)
is Either.Right -> provider!!.defaultSerializeValue(value.value, gen)
}
}
override fun serializeWithType(value: Either<Any, Any>?, gen: JsonGenerator?, serializers: SerializerProvider?, typeSer: TypeSerializer?) {
serialize(value, gen, serializers)
}
} |
If you do not want to include Type Id, then just do not use |
(note: originally reported against Kotlin module, example in Kotlin but failure in databind, transferred)
I have a
Either
definition:that I use in a simple data class:
I create an instance and try to serialize:
but jackson throws an
IndexOutOfBoundsException
:Is it some misconfiguration on my side? I am using jackson
2.10.1
.The text was updated successfully, but these errors were encountered: