Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 of 6 tasks
tomas0svk opened this issue Jan 20, 2025 · 0 comments
Open
4 of 6 tasks

Comments

@tomas0svk
Copy link

tomas0svk commented Jan 20, 2025

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?

)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant