Skip to content

[kotlin] Add AnyOf/oneOf to multiplatform#22035

Merged
wing328 merged 1 commit intoOpenAPITools:masterfrom
chizhanov:add-anyOf-oneOf
Oct 1, 2025
Merged

[kotlin] Add AnyOf/oneOf to multiplatform#22035
wing328 merged 1 commit intoOpenAPITools:masterfrom
chizhanov:add-anyOf-oneOf

Conversation

@chizhanov
Copy link
Copy Markdown
Contributor

This PR adds support for AnyOf and OneOf with the generateOneOfAnyOfWrappers flag in Kotlin Multiplatform.

I removed some checks to compare with the original anyof_class.mustache and oneof_class.mustache because multiplutform is more limited(kotlinx.serialization only).

For testing, you can use the following code:

openapi: 3.0.0
info:
  version: 1.0.0
  title: pets

paths:
  /petsOneOf:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/PetByAge'
                - $ref: '#/components/schemas/PetByType'
      responses:
        '200':
          description: Updated

  /petsAnyOf:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/PetByAge'
                - $ref: '#/components/schemas/PetByType'
      responses:
        '200':
          description: Updated

components:
  schemas:
    PetByAge:
      type: object
      properties:
        age:
          type: integer
        nickname:
          type: string
      required:
        - age

    PetByType:
      type: object
      properties:
        pet_type:
          type: string
          enum: [Cat, Dog]
        hunts:
          type: boolean
      required:
        - pet_type

It would be great if someone added testing for these cases, but I can't do that because I don't understand how they are organized here.

@dr4ke616 (2018/08) @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06) @e5l (2024/10)

@wing328

@wing328
Copy link
Copy Markdown
Member

wing328 commented Sep 29, 2025

thanks for the PR

is it correct to say that you've tested this fix locally to confirm it works for your use cases?

@chizhanov
Copy link
Copy Markdown
Contributor Author

thanks for the PR

is it correct to say that you've tested this fix locally to confirm it works for your use cases?

Yes, that’s correct. I verified it with the example from the first post.
In addition, I tested it against a large OpenAPI specification containing AnyOf and OneOf.
The generated code worked correctly in that case as well. While the result is not very convenient to use, it is functionally correct.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Oct 1, 2025

let's give it a try. i'll try to add some tests later

@wing328 wing328 merged commit 28e7e7f into OpenAPITools:master Oct 1, 2025
14 checks passed
@wing328
Copy link
Copy Markdown
Member

wing328 commented Oct 1, 2025

While the result is not very convenient to use,

can you please elaborate a bit more?

@chizhanov
Copy link
Copy Markdown
Contributor Author

While the result is not very convenient to use,

can you please elaborate a bit more?

Yes. In the generated code(ResponseInner) our model is stored in the actualInstance field, which always has type Any. This is not ideal, because it loses type-safety.
It would be much better if the generator could produce a sealed class (or a similar construct) representing the possible types. That way the compiler could help ensure correct usage instead of relying on runtime checks.

@chizhanov chizhanov deleted the add-anyOf-oneOf branch October 2, 2025 18:01
@wing328 wing328 added this to the 7.17.0 milestone Nov 6, 2025
rajvesh pushed a commit to rajvesh/openapi-generator that referenced this pull request Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants