Skip to content

[BUG] [JAVA] generator oneOf (<useOneOfInterfaces>, <useSealedOneOfInterfaces>-> true) doesn't work during deserialization #22927

@robert9580

Description

@robert9580
Description
<plugin>
              <groupId>org.openapitools</groupId>
              <artifactId>openapi-generator-maven-plugin</artifactId>
              <version>7.19.0</version>
              <executions>
                  <execution>
                      <id>generate-outbound-audit-event</id>
                      <goals>
                          <goal>generate</goal>
                      </goals>
                      <configuration>
                          <inputSpec>src/main/resources/openapi/openapi-outbound-audit-event-spec.yaml</inputSpec>
                          <generatorName>java</generatorName>
                          <generateApiTests>false</generateApiTests>
                          <generateModelTests>false</generateModelTests>
                          <configOptions>
                              <library>restclient</library>
                              <useJakartaEe>true</useJakartaEe>
                              <dateLibrary>java8</dateLibrary>
                              <sourceFolder>src/gen/java</sourceFolder>
                              <modelPackage>generated.event.audit</modelPackage>
                              <useOneOfInterfaces>true</useOneOfInterfaces>
                              <useSealedOneOfInterfaces>true</useSealedOneOfInterfaces>
                              <additionalOneOfTypeAnnotations>@JsonSubTypes({
                                  @JsonSubTypes.Type(SystemAuditEventData.class),
                                  @JsonSubTypes.Type(ResponseInfoError.class),
                                  @JsonSubTypes.Type(ChannelResponse.class)
                                  })
                              </additionalOneOfTypeAnnotations>
                          </configOptions>
                      </configuration>
                  </execution>
              </executions>
</plugin>

part of the file 'openapi-outbound-audit-event-spec.yaml'

        responseInfo:
          oneOf:
            - $ref: '#/components/schemas/SystemAuditEventData'
            - $ref: '#/components/schemas/ResponseInfoError'
            - $ref: '#/components/schemas/ChannelResponse'
        ....
    ResponseInfoError:
      type: object
      properties:
        httpCode:
          type: integer
        errorCode:
          type: string
        message:
          type: string

discriminator is not used

without adding this 'workaround'

<additionalOneOfTypeAnnotations>@JsonSubTypes({
        @JsonSubTypes.Type(SystemAuditEventData.class),
        @JsonSubTypes.Type(ResponseInfoError.class),
        @JsonSubTypes.Type(ChannelResponse.class)
         })
</additionalOneOfTypeAnnotations>

it will be generated

@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen",  comments = "Generator version: 7.19.0")
@JsonIgnoreProperties(
  value = "", // ignore manually set , it will be automatically generated by Jackson during serialization
  allowSetters = true // allows the  to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "", visible = true)

public sealed interface UserAuditEventResponseInfo permits SystemAuditEventData, ResponseInfoError, ChannelResponse {
}

what causes during the deserialization process

Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'ChannelResponse' as a subtype of `generated.event.audit.UserAuditEventResponseInfo`: known type ids = [] (for POJO property 'responseInfo')
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 474] (through reference chain: generated.event.audit.UserAuditEvent["responseInfo"])
	at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
	at com.fasterxml.jackson.databind.DeserializationContext.invalidTypeIdException(DeserializationContext.java:2068)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownTypeId(DeserializationContext.java:1617)
	at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._handleUnknownTypeId(TypeDeserializerBase.java:299)
	at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:164)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:150)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:135)
	at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:262)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:138)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:392)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
	at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2130)
	at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1602)
	at org.springframework.kafka.support.serializer.JsonDeserializer.deserialize(JsonDeserializer.java:600)

after adding the workaround it is generated and everything works fine

@JsonSubTypes({
@JsonSubTypes.Type(SystemAuditEventData.class),
@JsonSubTypes.Type(ResponseInfoError.class),
@JsonSubTypes.Type(ChannelResponse.class)
})
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.19.0")
@JsonIgnoreProperties(
  value = "", // ignore manually set , it will be automatically generated by Jackson during serialization
  allowSetters = true // allows the  to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "", visible = true)

public sealed interface UserAuditEventResponseInfo permits SystemAuditEventData, ResponseInfoError, ChannelResponse {
}
Related issues/PRs
Suggest a fix

@JsonSubTypes should be generated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions