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

Support for union type #551

Closed
guyariely opened this issue Aug 22, 2022 · 5 comments · Fixed by #645
Closed

Support for union type #551

guyariely opened this issue Aug 22, 2022 · 5 comments · Fixed by #645
Assignees
Labels
enhancement New feature or request

Comments

@guyariely
Copy link

The following schema:

Example:
      type: object
      properties:
        prop:
          type:
            - string
            - number

Will yield the following type:

interface Example {
  prop: unknown;
}

While the appropriate type would be:

interface Example {
  prop: string | number;
} 

It seems a property with more than one type is being treated as unknown.
This is especially apparent when trying to document nullable properties.

Package Version: 6.9.6

@anymaniax anymaniax self-assigned this Aug 22, 2022
@anymaniax anymaniax added the enhancement New feature or request label Aug 22, 2022
@mb21
Copy link

mb21 commented Sep 6, 2022

That would be great, also when using OpenAPI's oneOf with enum. For example:

openapi: 3.0.3
info:
  title: Sample API
  description: Sample
  version: '1.0'

paths:
  /pets:
    get:
      responses:
        '200':
          description: Pets
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Cat'
                  - $ref: '#/components/schemas/Dog'
components:
  schemas:
    Dog:
      type: object
      properties:
        kind:
          type: string
          enum: [dog]
    Cat:
      type: object
      properties:
        kind:
          type: string
          enum: [cat]

would generate:

interface Dog {
  kind: 'dog';
}

interface Cat {
  kind: 'cat';
}

type PetsResponse = Dog | Cat

which is a discriminated union.


The whole thing would be like openapi-typescript-codegen's --useUnionTypes option.

@DominicBach
Copy link
Contributor

FYI this is not a valid OpenAPI specification, see https://swagger.io/specification/#schema-object

The following properties are taken from the JSON Schema definition but their definitions were adjusted to the OpenAPI Specification.

type - Value MUST be a string. Multiple types via an array are not supported.

The correct way to model this would be

Example:
  type: object
  oneOf:
    - type: object
      properties:
        prop:
          type: string
    - type: object
      properties:
        prop:
          type: number

@mb21
Copy link

mb21 commented Sep 13, 2022

@DominicBach I updated the snipped to be complete (now it validates) and replaced the confusing double use of type with kind in one place. Please check out the links, hope it makes sense now?

@geirsagberg
Copy link
Contributor

@DominicBach Type arrays are supported in OpenAPI 3.1: https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0

@geirsagberg
Copy link
Contributor

I added a PR for nullability that should fix this one as well: #645

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants