OpenAPITools / OpenAPITools/openapi-generator

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

Open
#20,513 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?

)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal OpenAPI declaration and Maven Kotlin-generator configuration in the report, then inspect the generated SupersetOneOfType output and the Kotlin generator path that determines field optionality. Done means fieldA and fieldB are nullable while commonField remains non-nullable, with the behavior covered by an appropriate regression check.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.