Skip to content

Add support for x-enum-varnames and x-enum-descriptions #1374

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

Merged
merged 2 commits into from
Oct 11, 2023

Conversation

ElForastero
Copy link
Contributor

Changes

This MR adds support for x-enum-varnames and x-enum-descriptions fields.

x-enum-varnames is used to set enum members names.
x-enum-descriptions is used to add comments for enum members.

Both fields are optional and affect generated code only if they are present in the schema.

How to Review

There are no specific requirements.

The feature is similar to:

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Oct 8, 2023

🦋 Changeset detected

Latest commit: 5d9f276

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-typescript Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ElForastero ElForastero mentioned this pull request Oct 8, 2023
@@ -0,0 +1,5 @@
---
"openapi-typescript": major
Copy link
Contributor

@drwpow drwpow Oct 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"openapi-typescript": major
"openapi-typescript": minor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -229,6 +229,8 @@ export function tsDedupe(types: ts.TypeNode[]): ts.TypeNode[] {
export function tsEnum(
name: string,
members: (string | number)[],
membersNames?: string[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
membersNames?: string[],
metadata?: { name?: string, description?: string }[],

I think this is an easier-to-use signature; I find the former where multiple ordered params have the same type to be easy to make mistakes in rearranging them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, done 👍

@drwpow
Copy link
Contributor

drwpow commented Oct 8, 2023

Great tests!

Before we ship this though this will need to be added to the docs somewhere. Perhaps in a section in advanced.md for now?

@@ -521,3 +521,44 @@ Cat: { type?: "cat"; } & components["schemas"]["PetCommonProperties"];
_Note: you optionally could provide `discriminator.propertyName: "type"` on `Pet` ([docs](https://spec.openapis.org/oas/v3.1.0#discriminator-object)) to automatically generate the `type` key, but is less explicit._

While the schema permits you to use composition in any way you like, it’s good to always take a look at the generated types and see if there’s a simpler way to express your unions & intersections. Limiting the use of `oneOf` is not the only way to do that, but often yields the greatest benefits.

### Enum with custom names and descriptions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't come up with something better than copying the existing description from one of the OpenAPI generators projects :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me!

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much! This will go out in the v7 stable release 🙂

@drwpow drwpow merged commit 7ac5174 into openapi-ts:main Oct 11, 2023
This was referenced Oct 11, 2023
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.

2 participants