Child class cannot be deserialized
Closed this issue · 3 comments
Hi! I use the latest generator and have schemas like this:
Parent:
type: object
description: parent object
properties:
name:
type: string
description: object name
Child:
allOf:
- $ref: '#/schemas/Parent'
- type: object
properties:
code:
type: integer
description: response code
The following files are generated:
- Sealed interface
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(as = ParentDefault.class)
public sealed interface Parent permits Child, ParentDefault {
...
}
- Its default implementation
public final class ParentDefault implements Parent {
...
}
- Child class
public final class Child implements Parent {
...
}
The problem with this annotation:
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(as = ParentDefault.class)
When we receive Child's json, Jackson fails to build deserializer, because Child class is not subclass of ParentDefault.
So we have an error:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Failed to narrow type [simple type, class Child] with annotation (value ParentDefault), from 'Child': Class
ParentDefault
not subtype ofChild
I tried to register custom deserializer, but Jackson prioritize JsonDeserialize annotation on deserializer lookup.
We tried x-is-one-of-interface
extension to fix this issue and it works. But our specification is used by other services with different generators (plain java with feign library) and this extension breaks their codegen.
On branch issue-356 i tried to reproduce your issue. But the ModelTest works for me. Please tell me the difference to your setup.
Hi, @sschnabe, sorry for the late response. I uploaded a little example of our way of serialization/desrialization - https://github.com/vgsub/micronaut-codegen-example/tree/main. We compared your test and our example and found out that the problem is that we do not use micronaut-serde-jackson module and generator's 'serde' property is disabled.
Seems like we have to migrate our apps to micronaut-serde-jackson, so I will close this issue. Thanks for your support!