-
Notifications
You must be signed in to change notification settings - Fork 378
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
Add x-enum-varnames
support
#405
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
Can you also add a normalizer step to convert tsEnumNames
-> x-enum-varnames
, and throw if the latter is already defined and different from what we would have generated?
That will also let you simplify your changes to parser.ts a tiny bit.
@bcherny Would I add the following rule: rules.set('Convert tsEnumNames -> x-enum-varnames', schema => {
if (schema.tsEnumNames) {
schema['x-enum-varnames'] = schema.tsEnumNames
delete schema.tsEnumNames
}
}) And then be able to always reference |
Yup, exactly. Don't forget to validate when you do that too, to make sure the schema doesn't already have conflicting |
Is there a way to log a deprecation warning so that consumers know |
@bcherny are you OK with the way I'm handling the existence of both |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking better -- almost there!
rules.set('Convert tsEnumNames -> x-enum-varnames', schema => { | ||
if (schema.tsEnumNames) { | ||
if (Object.keys(schema).includes('x-enum-varnames')) { | ||
throw new Error('Cannot set both x-enum-varnames and tsEnumNames in the same schema. Only use x-enum-varnames.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind being a little more lenient, and only throwing if the two don't match? These should also have tests, if possible.
ie.
// ok
{
tsEnumNames: ['a']
}
// ok
{
x-enum-varnames: ['a']
}
// ok
{
tsEnumNames: ['a'],
x-enum-varnames: ['a']
}
// error
{
tsEnumNames: ['a'],
x-enum-varnames: []
}
// error
{
tsEnumNames: [],
x-enum-varnames: ['a']
}
// error
{
tsEnumNames: ['a'],
x-enum-varnames: ['b']
}
// error
{
tsEnumNames: ['a'],
x-enum-varnames: ['a', 'b']
}
@@ -93,10 +97,18 @@ export interface EnumJSONSchema extends NormalizedJSONSchema { | |||
enum: any[] | |||
} | |||
|
|||
export interface NamedEnumJSONSchema extends NormalizedJSONSchema { | |||
interface DeprecatedNamedEnumJSONSchema extends NormalizedJSONSchema { | |||
'x-enum-varnames'?: never | |||
tsEnumNames: string[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normalized schemas shouldn't contain tsEnumNames
, since you're normalizing it away.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I should delete DeprecatedNamedEnumJSONSchema
and rename _NamedEnumJSONSchema
to NamedEnumJSONSchema
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah just add x-enum-varnames: string[]
and tsEnumNames: never
to NormalizedJSONSchema
. No need for new types if you can avoid it.
/** | ||
* `tsEnumNames` is being replaced by `x-enum-varnames`, but we'll leave this | ||
* validator until `tsEnumNames` is removed. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure this is still covered by tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I copy enumValidation.1.ts
and enumValidation.2.ts
, but sed "s/tsEnumNames/x-enum-varnames/"
in the new test files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds ok to me, or merge into a single test file that covers all the cases (x-, ts-, x- and ts-, etc.).
/** | ||
* `tsEnumNames` is being replaced by `x-enum-varnames`, but we'll leave this | ||
* validator until `tsEnumNames` is removed. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure this is still covered by tests.
Hi, is there any chance it'll get merged soon? |
@@ -93,10 +97,18 @@ export interface EnumJSONSchema extends NormalizedJSONSchema { | |||
enum: any[] | |||
} | |||
|
|||
export interface NamedEnumJSONSchema extends NormalizedJSONSchema { | |||
interface DeprecatedNamedEnumJSONSchema extends NormalizedJSONSchema { | |||
'x-enum-varnames'?: never | |||
tsEnumNames: string[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah just add x-enum-varnames: string[]
and tsEnumNames: never
to NormalizedJSONSchema
. No need for new types if you can avoid it.
/** | ||
* `tsEnumNames` is being replaced by `x-enum-varnames`, but we'll leave this | ||
* validator until `tsEnumNames` is removed. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds ok to me, or merge into a single test file that covers all the cases (x-, ts-, x- and ts-, etc.).
No worries, @bcherny! I started taking a look at this PR again and I'm having a hard time understanding my changes 😅. I think it'd take some effort to ramp myself back up, unfortunately. If this is something you could bang out quickly, then that's probably best. But if that's not the case then I can try to work on this again |
Closing out stale PRs. If you'd like to revisit this, feel free to re-open. |
Currently,
json-schema-to-typescript
uses a customtsEnumNames
extension for naming enum members. However, this extension is specific only to this library. Other major OpenAPI generators (1, 2) have adoptedx-enum-varnames
for the same purpose, so this PR bringsjson-schema-to-typescript
in line with them.This PR addresses feature request #200.