Skip to content

[BUG] Kotlin - Optionality of fields not generated correctly when using oneOf #20513

@tomas0svk

Description

@tomas0svk

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If I specify a field, that is "oneOf" two types, a new type is generated, which contains a superset of all fields in those two types
(or perhaps more than two) - so far correct.
However if those two types contain required fields, the generated superset type also contains required fields, probably for any field, that is required in any of oneOf types, not only for intersection (see example)

openapi-generator version

7.10

OpenAPI declaration file content or url
 IrrelevantWrapperType:
     type: object
     required:
       - oneOfField
     properties:
       oneOfField:
         $ref: '#/components/schemas/SupersetOneOfType'
   SupersetOneOfType:
     type: object
     oneOf:
       - $ref: '#/components/schemas/TypeA'
       - $ref: '#/components/schemas/TypeB'

   TypeA:
     type: object
     required:
       - fieldA
       - commonField
     properties:
       fieldA:
         type: integer         
       commonField:
         type: integer          

   TypeB:
     type: object
     required:
       - fieldB
       - commonField
     properties:
       fieldB:
         type: integer        
       commonField:
         type: integer
Generation Details
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>${openapi-generator-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>generate-betslip-schema</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
               <inputSpecRootDirectory>${project.build.directory}/classes/oas/foo</inputSpecRootDirectory>
                <generatorName>kotlin</generatorName>
                <configOptions>
                    <library>jvm-ktor</library>
                    <serializationLibrary>jackson</serializationLibrary>
                    <modelMutable>true</modelMutable> <!-- true or false, makes no difference in this -->
                    <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
                    <sourceFolder>src/main/kotlin</sourceFolder>
                    <additionalModelTypeAnnotations>@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY)</additionalModelTypeAnnotations>
                </configOptions>
                <openapiNormalizer>REF_AS_PARENT_IN_ALLOF=true</openapiNormalizer>
                <generateApis>false</generateApis>
                <generateModelTests>false</generateModelTests>
                <generateApiTests>false</generateApiTests>
                <generateSupportingFiles>false</generateSupportingFiles>
                <modelPackage>foo.model</modelPackage>
                <packageName>foo</packageName>
                <additionalProperties>removeEnumValuePrefix=false</additionalProperties>
            </configuration>
        </execution>         
    </executions>
</plugin>
Expected vs actual

Generated code:

data class SupersetOneOfType (

    @field:JsonProperty("fieldA")
    val fieldA: kotlin.Int, //this should be Int?

    @field:JsonProperty("commonField")
    val commonField: kotlin.Int, //OK to be Int!!

    @field:JsonProperty("fieldB")
    val fieldB: kotlin.Int //this should be Int?

)

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