-
Notifications
You must be signed in to change notification settings - Fork 43
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
When using sealed classes: Discriminator not included in serialized object #258
Comments
So full disclosure, I have never used this feature. But, I remember raising this with when @pschichtel added this feature. I think is answer then was that the json needs to contain the discriminator field, and Jackson can read it and decide what polymorphic type to use, but it does not need to actually be modelled in the data classes |
Oh actually, I think I see what you are saying. That feature is somehow stripping the status field. SateA and StateB look like they should contain a status property |
@cjbooms This was intentional. It strips the property that is being declared as the discriminator. Mainly to prevent accidental misconfiguration of the class when generating json. If you keep the discriminator property part of the data class, then you can construct a value of one class with the discriminator value of the other class. I'm pretty sure I used this successfully in the project I used fabrikt in. Both for parsing and generating. I might be able to publish the openapi files or at least the relevant sections as an example. I'll check next week. |
I checked the project. I definitely used this feature with success. Maybe it worked for me because I had the kotlin module enabled in jackson? Or maybe it's because the data classes are turned into objects here since no fields are left. The cases where this happened in my spec are never parsed, only generated. Does it work with additional fields in the objects? Does it work when you enable the kotlin module in the ObjectMapper? |
Deserialization When I enable the Kotlin module deserialization in the first example works. Although Jackson is able to determine the type I need to configure val mapper = ObjectMapper()
.registerKotlinModule()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
val parsedObj = mapper.readValue("""
{
"state": {
"status": "a"
}
}
""".trimIndent(), SomeObj::class.java)
println("Deserialized object: $parsedObj") // SomeObj(state=JacksonParsingTest$StateA@129fed45) So; adding the Kotlin module makes the generated models work for deserialization. Serialization Configuring the Kotlin module does not seem to change how the model is serialized and we still get JSON without the type info: |
Thanks @pschichtel and @ulrikandersen
That should fix this issue, and help avoid (but not prevent) constructing a value of one class with the discriminator value of the other class. See updated PR: |
Fixed in 13.0.0 |
@cjbooms I think #268 introduced a new issue: If additional properties are present in the model the default enum value is set to the discriminator value. #270 contains the updated spec with an additional property. The tests will to fail because the generated model differs from the expected. I would be happy to assist fixing it, but I would need some pointers as to where to look. |
#270 now contains a fix for the issue. Let me know what you think 🙏 |
Thanks for the fix, looks good. Released in 13.0.1. Should sync to maven central in the next 30 minutes or so |
Hi,
When using fabrikt with
SEALED_INTERFACES_FOR_ONE_OF
I am experiencing issues with the serialised object: It does not contain the discriminator property.The issue exists in when using fabrikt in my own code, but I have also tried using the models from the example and see the same issues:
status
to identify the sub class.status
it fails parsing.I am using Jackson with no extra configuration.
Example:
Output:
Example with added
status
:If I add the status property it works:
Output:
I am unsure if this is a Jackson issue or an issue with the generated code.
Do you have an idea?
The text was updated successfully, but these errors were encountered: