quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.64k stars 2.64k forks source link

Quarkus REST fails to write polymorphic type property in JSON #43631

Open danielbobbert opened 2 days ago

danielbobbert commented 2 days ago

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:

  1. The first test uses Jackson manually to serialize a derived class. It contains a "type" property, as expected.
  2. The second test calls a REST endpoint which formally returns a list of base objects, which at runtime contains two instances of derived classes. This test fails, because the "type" property is missing from the rest objects.

The root of the problem must by Quarkus REST (reactive), because

  1. Serialization works correctly with plain Jackson
  2. The tests all succeed if you switch to "quarkus-resteasy-jackson" (the old sync API) in the pom.xml
  3. But it fails with "quarkus-resteasy-reactive-jackson" (no matter, what version of Quarkus you use. I tested 3.8.6 up to 3.15.1)
  4. If also fails with "quarkus-rest-jackson" 3.15.1 (which is the successor to "quarkus-resteasy-reactive-jackson", so that's not a surprise)

So the reactive REST stack seems to do JSON serialization a bit differently than the old non-reactive "resteasy" stack.

Output of uname -a or ver

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 or gradlew --version)

mvn 3.9.9

Additional information

polymorphic-json-bug.zip

geoand commented 2 days ago

@mariofusco I wonder if you want to check this out at some point

mariofusco commented 1 day ago

@geoand sure, I will give a look, feel free to assign this to me.

geoand commented 1 day ago

🙏🏽

danielbobbert commented 1 day ago

I even checked back with Quarkus 3.2.1.Final Same result: It works with "quarkus-resteasy-jackson", but it fails with "quarkus-resteasy-reactive-jackson".

gsmet commented 8 hours ago

I can confirm the reproducer allows to reproduce the problem easily and that we are somehow missing the types (we should have a type property in the JSON) from the serialized output:

[
    {
        "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.