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

Invalid type files are generated with Null-only enum or Empty enum #1022

Closed
bigen1925 opened this issue Nov 8, 2023 · 5 comments · Fixed by #1023
Closed

Invalid type files are generated with Null-only enum or Empty enum #1022

bigen1925 opened this issue Nov 8, 2023 · 5 comments · Fixed by #1023
Assignees
Labels
bug Something isn't working
Milestone

Comments

@bigen1925
Copy link
Contributor

What are the steps to reproduce this issue?

  1. input this
# openapi.yaml
openapi: 3.0.0
info:
  title: title
components:
  schemas:
    NullOnlyStringEnum:
      type: string
      nullable: true
      enum:
        - null
    NullOnlyNumberEnum:
      type: number
      nullable: true
      enum:
        - null
    EmptyStringEnum:
      type: string
      enum: []
    EmptyNumberEnum:
      type: number
      enum: []
  1. generate
    npx orval --input ./openapi.yaml --output out.ts
  2. output
/**
 * Generated by orval v6.19.1 🍺
 * Do not edit manually.
 * title
 */
export type EmptyNumberEnum = typeof EmptyNumberEnum[keyof typeof EmptyNumberEnum];


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const EmptyNumberEnum = {
  '': , // invalid typescript
} as const;

export type EmptyStringEnum = typeof EmptyStringEnum[keyof typeof EmptyStringEnum];


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const EmptyStringEnum = {
  '': '', // valid typescript, but not appropriate
} as const;

export type NullOnlyNumberEnum = typeof NullOnlyNumberEnum[keyof typeof NullOnlyNumberEnum];


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const NullOnlyNumberEnum = {
  '': , // invalid typescript
  null: null,
} as const;

export type NullOnlyStringEnum = typeof NullOnlyStringEnum[keyof typeof NullOnlyStringEnum];


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const NullOnlyStringEnum = {
  '': '', // valid typescript, but not appropriate
  null: null,
} as const;

What happens?

An empty enum item not included in spec is generated.
In case of number, a const value is empty and it's invalid typescript.

What were you expecting to happen?

output

/**
 * Generated by orval v6.19.1 🍺
 * Do not edit manually.
 * title
 */
export type EmptyNumberEnum = typeof EmptyNumberEnum[keyof typeof EmptyNumberEnum]; // or, never


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const EmptyNumberEnum = {} as const;

export type EmptyStringEnum = typeof EmptyStringEnum[keyof typeof EmptyStringEnum]; // or, never


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const EmptyStringEnum = {} as const;

export type NullOnlyNumberEnum = typeof NullOnlyNumberEnum[keyof typeof NullOnlyNumberEnum];


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const NullOnlyNumberEnum = {
  null: null,
} as const;

export type NullOnlyStringEnum = typeof NullOnlyStringEnum[keyof typeof NullOnlyStringEnum];


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const NullOnlyStringEnum = {
  null: null,
} as const;

Any logs, error output, etc?

No information logs involving the behavior.

🍻 Start orval v6.19.1 - A swagger client generator for typescript
⚠️  SyntaxError: /Users/mitsurukasai/Documents/exercise/orval-ref-decode/openapi.yaml is not a valid Openapi API definition
🎉 title - Your OpenAPI spec has been converted into ready to use orval!

Any other comments?

Thanks for developing and maintaining this awesome package! :D

I try to make PR about this issue.

What versions are you using?

Operating System: MacOS 13.0
Package Version: 6.19.1
Browser Version: N/A

@melloware melloware added the bug Something isn't working label Nov 8, 2023
@melloware
Copy link
Collaborator

Interesting this was just fixed: #1010

Why are your enums totally null that is not really an enum right?

@melloware
Copy link
Collaborator

Oh and yes PR is welcome

@bigen1925
Copy link
Contributor Author

@melloware
Thank you for your comment!
I'm sorry in advance for my poor english.

Interesting this was just fixed: #1010

I tried with latest master (sha 7a9587, #1010 was merged) , and output was like bellow.
Type representation of null is improved, and still there are empty items.

export type NullOnlyNumberEnum = typeof NullOnlyNumberEnum[keyof typeof NullOnlyNumberEnum] | null;


// eslint-disable-next-line @typescript-eslint/no-redeclare
export const NullOnlyNumberEnum = {
  '': ,
  // `null : null` is removed, but `'': ,` is still there 
} as const;

Why are your enums totally null that is not really an enum right?

My API have schemas of objects that have exact null property, like

schemas:
  result:
    anyOf:
        -  type: object
           properties:
             data: ...
             error: # exact null
        -  type: object
           properties:
             data: # exact null
             error: ...

There are not null type in openapi 3.0.x, (added in 3.1.0) and the only way to represent exact null value is with nullable and enum

type: object # any type is ok
nullable: true
enum: 
  - null

@bigen1925
Copy link
Contributor Author

bigen1925 commented Nov 8, 2023

FYI, by Release Note of Openapi, enum is SHOULD NOT empty (means not recommended but valid) in 3.0.
It is MUST NOT (means invalid) in 3.1.

@melloware
Copy link
Collaborator

Cc @lenwhite please review

@melloware melloware added this to the 6.20.0 milestone Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants