-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Quarkus REST fails to write polymorphic type property in JSON #43631
Comments
@mariofusco I wonder if you want to check this out at some point |
@geoand sure, I will give a look, feel free to assign this to me. |
🙏🏽 |
I even checked back with Quarkus 3.2.1.Final |
I can confirm the reproducer allows to reproduce the problem easily and that we are somehow missing the types (we should have a [
{
"id": "ID A",
"aProperty": "aProperty"
},
{
"id": "ID B",
"bProperty": "bProperty"
}
] Now as to why, this is the mystery we need to solve. Also just to clarify, this isn't a native executable oddity, the problem happens with plain JVM. |
I think, I found something. If you add the following test to the reproducer, it will always fail, no matter what version/kind of rest easy stack you are using:
So, Jackson is really missing the "type" property already, if you serialize a list. (My bad, that the Jackson-Test in the reproducer only serialized a single element, while the REST test was using a list). But the following works:
So it seems, that Jackson needs the hint about the generic type of list, in order to decide that it needs to generate the "type" properties. My guess would be that quarkus-resteasy-jackson was taking the generic return type of the method into account. And in fact, in Quarkus 3.14.1, org.jboss.resteasy.core.ServerResponseWriter, lines 111-125, look like this:
So the "generic" response type is taken into account when resolving the MessageBodyWriter. But in quarkus-rest, a "one-for-all" message writer is constructed in JacksonMessageBodyWriterUtil.createDefaultWriter(). And in io.quarkus.resteasy.reactive.jackson.runtime.serialisers.BasicServerJacksonMessageBodyWriter, lines 57-67, the generic Type is not taken into account (even though it is available as a method parameter):
Chosing the proper writer here might solve the problem. I'm thinking along the lines of
I don't known whether this has any negative performance implications and/or some (caching) factory must/should be used to resolve the proper writer. |
@danielbobbert definitely some nice detective work. I think you probably nailed it. From what I can see it's going to have some performance consequences though. Now if @geoand will probably have a lot of interest in your findings but we are currently preparing Devoxx so it might have to wait a bit. |
Excellent work @danielbobbert ! I won't have time now, but feel free to open a PR with your proposed solution and we can discuss it when time permits |
I have created a PR, including an in-memory cache of specialized writers to make sure that each generic writer is created only once, to mitigate any performance concerns. |
Describe the bug
When using inheritance with Jackson, Quarkus REST fails to write the type id property.
The problem was NOT present in quarkus-resteasy-jackson, but it was present in quarkus-resteasy-reactive-jackson (checked back to 3.8.6) and it is present in quarkus-rest 3.15.1
Expected behavior
The JSON representation of derived classes should contain the annotated type id property.
Actual behavior
The JSON representation of derived classes does not contain the annotated type id property. For example, RestAssured fails with error "Could not resolve subtype of [simple type, class org.acme.model.BaseClass]: missing type id property 'type'"
How to Reproduce?
See the attached reproducer project. It contains two tests:
The root of the problem must by Quarkus REST (reactive), because
So the reactive REST stack seems to do JSON serialization a bit differently than the old non-reactive "resteasy" stack.
Output of
uname -a
orver
No response
Output of
java -version
Java 21
Quarkus version or git rev
Quarkus 3.8.6 - 3.15.1
Build tool (ie. output of
mvnw --version
orgradlew --version
)mvn 3.9.9
Additional information
polymorphic-json-bug.zip
The text was updated successfully, but these errors were encountered: