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

fix(getters): handle OpenAPI 3.1 nullability #645

Merged
merged 1 commit into from
Nov 7, 2022

Conversation

geirsagberg
Copy link
Contributor

@geirsagberg geirsagberg commented Nov 6, 2022

Status

READY

Description

In OpenAPI 3.1, the nullable keyword has been removed, and nullability is handled by oneOf, anyOf or type arrays: https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0

Currently, Orval does not support this, with nullable types ending up as unknown (for type arrays) or e.g. string | unknown (for oneOf/anyOf).

This PR adds support both for oneOf, anyOf and type arrays:

components:
  schemas:
    NullableObject:
      type: object
      properties:
        name:
          type:
            - 'string'
            - 'null'
        age:
          anyOf:
            - type: 'integer'
            - type: 'null'
        address:
          oneOf:
            - type: 'string'
            - type: 'null'

Resulting in:

export interface NullableObject {
  name?: NullableObjectName
  age?: NullableObjectAge
  address?: NullableObjectAddress
}

export type NullableObjectAddress = string | null
export type NullableObjectAge = number | null
export type NullableObjectName = string | null

Also fixes #551 by interpreting type arrays as anyOf (as suggested by https://stackoverflow.com/a/48114924/500976)

Todos

  • Tests

@vercel
Copy link

vercel bot commented Nov 6, 2022

Someone is attempting to deploy a commit to a Personal Account owned by @anymaniax on Vercel.

@anymaniax first needs to authorize it.

@anymaniax
Copy link
Collaborator

Seems good thanks @geirsagberg

@anymaniax anymaniax merged commit 622919a into orval-labs:master Nov 7, 2022
@geirsagberg
Copy link
Contributor Author

Thanks for merging :D any ETA for next release?

@anymaniax
Copy link
Collaborator

it's release

@arthurfiorette
Copy link
Contributor

Hey @anymaniax does support for union types with mock: true is being tested? I cannot get it to work :/

image

image

It generates correct typings, but the mock is being generated with a simple {} object.

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.

Support for union type
3 participants