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

Type and Enum duplicated when schema property enum as $ref #2099

Open
1 of 2 tasks
Ginxo opened this issue Jan 13, 2025 · 3 comments
Open
1 of 2 tasks

Type and Enum duplicated when schema property enum as $ref #2099

Ginxo opened this issue Jan 13, 2025 · 3 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@Ginxo
Copy link

Ginxo commented Jan 13, 2025

openapi-typescript version

7.5.2

Node.js version

20.11.0

OS + version

Fedora Linux 41

Description

after executing the tool with --root-types --root-types-no-schema-prefix --enum flags (since types are required for not refactoring the whole application and Schema prefix avoided due to the same reason...) some of the enums are duplicated also as type aliases, which is producing an error at TS validation time.

I'm clicking My OpenAPI schema is valid and passes the [Redocly validator](https://redocly.com/docs/cli/commands/lint/) (npx @redocly/cli@latest lint) * because it is required, I have few issues but none of them related with the enums

It is mandatory for us to use --root-types-no-schema-prefix not to affect the whole application

Reproduction

npx openapi-typescript https://api.stage.openshift.com/api/clusters_mgmt/v1/openapi -o src/types/clusters_mgmt.v1/schema.ts --root-types --root-types-no-schema-prefix --enum

Expected result

Maybe different type alias name, see case for AWSInfrastructureAccessRoleState where enum is AWSInfrastructureAccessRoleState (AWS uppercase) and type AwsInfrastructureAccessRoleGrantState (Aws upper first)

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

@Ginxo Ginxo added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Jan 13, 2025
@Ginxo
Copy link
Author

Ginxo commented Jan 15, 2025

@drwpow @htunnicliff @kerwanp any idea about how to workaround it?

@Ginxo
Copy link
Author

Ginxo commented Jan 15, 2025

for now as workaround, I'm moving enums to enums.ts file and pointing every reference to it

export enum AWSInfrastructureAccessRoleGrantState {
  deleting = 'deleting',
  failed = 'failed',
  pending = 'pending',
  ready = 'ready',
  removed = 'removed',
}
...
...
export enum WildcardPolicy {
  WildcardsAllowed = 'WildcardsAllowed',
  WildcardsDisallowed = 'WildcardsDisallowed',
}

and generating the rest of the model without --enum flag

npx openapi-typescript https://api.stage.openshift.com/api/clusters_mgmt/v1/openapi -o src/types/clusters_mgmt.v1/schema.ts --root-types --root-types-no-schema-prefix

this way, whenever the issue is solved or a workaround exists the refactoring should be pretty straightforward

@Ginxo
Copy link
Author

Ginxo commented Jan 16, 2025

so I think we came to a better conclusion by generating the model twice

  • one for Schema without enum and without prefix
  • one for enums with Schema prefix

like:

npx openapi-typescript ./openapi/clusters_mgmt.v1.json -o src/types/clusters_mgmt.v1/index.ts --root-types --root-types-no-schema-prefix
npx openapi-typescript ./openapi/clusters_mgmt.v1.json -o src/types/clusters_mgmt.v1/enums.ts --root-types --enum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

1 participant