OpenAPITools / OpenAPITools/openapi-generator
[BUG] [JAVA] generator oneOf (<useOneOfInterfaces>, <useSealedOneOfInterfaces>-> true) doesn't work during deserialization
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Java generator output with the shown oneOf specification and useOneOfInterfaces/useSealedOneOfInterfaces options. Inspect the Java generator's oneOf interface generation and its Jackson annotations, then add coverage for deserializing the generated responseInfo variants. Done means generated sealed interfaces register all permitted subtypes without additionalOneOfTypeAnnotations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100