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

Enhancing openapi-typescript for Improved Type Safety: Generating Arrays from Enum Definitions #1616

Open
1 task done
nakamurau1 opened this issue Apr 11, 2024 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed openapi-ts Relevant to the openapi-typescript library

Comments

@nakamurau1
Copy link

Description

I'm utilizing openapi-typescript to enhance API type safety. However, when parsing parameters using user-defined type guards, it's necessary to validate the elements of a Union type, requiring an array of Enum values. Currently, openapi-typescript only generates Union types, necessitating manual array definition, which introduces redundancy.

Proposal

I propose adding an option to openapi-typescript output to generate an array of Enum values. This would allow for the simultaneous generation of Union types and arrays, improving type safety and reducing code redundancy. For instance, consider the following option:

// Generated type definitions
type SortKey = 'id' | 'name' | 'cost';
// Proposed array
const sortKeys: SortKey[] = ['id', 'name', 'cost'];

Checklist

@nakamurau1 nakamurau1 added enhancement New feature or request help wanted Extra attention is needed openapi-ts Relevant to the openapi-typescript library labels Apr 11, 2024
@luchsamapparat
Copy link
Contributor

That would be very helpful!

We validate our forms with Zod to ensure that the form values match the OpenAPI spec. If the spec provides an enum with valid values, I want to pass those values to z.enum(...) to make sure that a valid value has been selected.

@drwpow
Copy link
Owner

drwpow commented May 10, 2024

I’d be open to this! While it’s still a library goal to have no runtime, technically TypeScript enums do have a runtime, so that line has been crossed already, though we do want to be reserved in what we generate (because this library tries to steer away from runtime codegen).

If anyone wants to open a PR for this, I’d like to see:

  1. The original types have the full path, e.g. const sortKeys: components["schemas"]["SortKey"][] = […]
  2. It’s behind a CLI flag (not default behavior)
  3. Lots and lots of tests 😄 (including tests that make sure that things like components["schemas"]["00-illegal-js.key"] can generate a valid JS identifier

If all 3 criteria are satisfied, happy to ship it 🙂. Just note that the current main is for the upcoming 7.x release, so if you’d like this on 6.x, base that off the 6.x branch (though a 7.0 release candidate will happen very soon, promise 😅)

@luchsamapparat
Copy link
Contributor

I started working on it and managed to emit this in packages/openapi-typescript/test/fixtures/redocly/output/b.ts for the gender enum property in openapi/b.yaml

export const personGenderValues: components["schemas"]["person"]["gender"][] = ["male", "female", "unknown"];

I also put it behind an optional CLI flag:

  --enumValues               Export union values as arrays

Not sure about the naming and description though.

I'll create a PR next week, but I have to clean up my code first and figure add tests for it.

luchsamapparat added a commit to luchsamapparat/openapi-typescript that referenced this issue May 14, 2024
luchsamapparat added a commit to luchsamapparat/openapi-typescript that referenced this issue May 14, 2024
@luchsamapparat
Copy link
Contributor

@drwpow I've created a PR for this: #1661

I used the enum tests as a template for testing this feature.

Also, I'm not sure about the flag's name and description and am open to suggestions for better wording.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

3 participants