Child class cannot be deserialized #356

Closed
opened 2024-01-12 13:37:47 +01:00 by vgsub · 3 comments
vgsub commented 2024-01-12 13:37:47 +01:00 (Migrated from github.com)

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:

  1. Sealed interface
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(as = ParentDefault.class)
public sealed interface Parent permits Child, ParentDefault {
   ...
}
  1. Its default implementation
public final class ParentDefault implements Parent {
   ...
}
  1. 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 of Child

I tried to register custom deserializer, but Jackson prioritize JsonDeserialize annotation on deserializer lookup.

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: 1. Sealed interface ``` @com.fasterxml.jackson.databind.annotation.JsonDeserialize(as = ParentDefault.class) public sealed interface Parent permits Child, ParentDefault { ... } ``` 2. Its default implementation ``` public final class ParentDefault implements Parent { ... } ``` 3. 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 of `Child` I tried to register custom deserializer, but Jackson prioritize JsonDeserialize annotation on deserializer lookup.
vgsub commented 2024-01-12 13:47:31 +01:00 (Migrated from github.com)

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.

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.
sschnabe commented 2024-02-23 11:09:25 +01:00 (Migrated from github.com)

On branch issue-356 i tried to reproduce your issue. But the ModelTest works for me. Please tell me the difference to your setup.

On branch [issue-356](https://github.com/kokuwaio/micronaut-openapi-codegen/compare/main...issue-356) i tried to reproduce your issue. But the ModelTest works for me. Please tell me the difference to your setup.
vgsub commented 2024-03-12 12:44:50 +01:00 (Migrated from github.com)

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!

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!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
kokuwaio/micronaut-openapi-codegen#356
No description provided.