-
-
Notifications
You must be signed in to change notification settings - Fork 414
Add option to generate union instead of enum #2010
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
Comments
Im not sure I understand the use case. Why are you not using the enum? How is your schema structured like?
|
This is a real world example of what Orval generates: /**
* Order status:
- `24` (new)
- `21` (review)
*/
export type StatusOrder = typeof StatusOrder[keyof typeof StatusOrder];
// eslint-disable-next-line @typescript-eslint/no-redeclare
export const StatusOrder = {
NUMBER_24: '24',
NUMBER_21: '21',
} as const; Above, you can see So the preferred generated result would be: /**
* Order status:
- `24` (new)
- `21` (review)
*/
export type StatusOrder = "24" | "21" OpenAPI schema for the example above: {
"components": {
"schemas": {
"status_order": {
"type": "string",
"enum": ["24", "21"],
"description": "Order status:\n- `24` (new)\n- `21` (review)"
}
}
}
}
Because we generate OpenAPI docs from TypeScript. We already have an The code below generates the OpenAPI snippet above. export const order = {
new: `24`,
review: `21`,
} as const satisfies Record<string, `${number}`> |
Ah i see, so you would like to be able to create a union type instead of enums? That makes sense! |
@soartec-lab maybe we should make enums:
| "const" // as todays default
| "enum" // same as useNativeEnums=true
| "union" // this new way |
That's good! |
Im thinking we could leave |
@AllieJonsson |
Yes, if you generate using the new suggested option the no. If you want to be able to do that you set Here is my suggestion: // types.ts
export type OverrideOutput = {
...
/**
* @deprecated use enumGeneration: "enum" instead
*/
useNativeEnums?: boolean;
enumGeneration?: 'const' | 'enum' | 'union';
}
export type NormalizedOverrideOutput = {
...
enumGeneration: 'const' | 'enum' | 'union';
} // options.ts
export const normalizeOptions = async (...) => {
...
if (outputOptions.override?.useNativeEnums !== undefined) {
log("'useNativeEnums' is deprecated, use 'enumGeneration: \"enum\"' instead.");
}
const normalizedOptions: NormalizedOptions = {
...
output: {
...
override: {
...
enumGeneration: !!outputOptions.override?.useNativeEnums ? 'enum' : (outputOptions.override?.enumGeneration ?? 'const'),
}
}
}
} |
I see, so does this mean that
|
@soartec-lab |
Oh, that makes sense. Would you like to submit a PR for this? |
Sure, Ill fix this tomorrow morning! |
You're great! |
Can we change the title of this issue to something along the lines of |
override.disableEnumGeneration
@AllieJonsson @soartec-lab Thank you, it works great, except for one case below. // eslint-disable-next-line @typescript-eslint/no-redeclare
export const PatchV1UserRelationshipIdBodyFollows = {...FollowsComputeOnBackend,...ValueToRemoveAnAttribute,} as const
// eslint-disable-next-line @typescript-eslint/no-redeclare
export const PatchV1UserRelationshipIdBodyBlocks = {...BlocksComputeOnBackend,...ValueToRemoveAnAttribute,} as const
export type PatchV1UserRelationshipIdBody = {
follows?: typeof PatchV1UserRelationshipIdBodyFollows[keyof typeof PatchV1UserRelationshipIdBodyFollows] ;
blocks?: typeof PatchV1UserRelationshipIdBodyBlocks[keyof typeof PatchV1UserRelationshipIdBodyBlocks] ;
}; Probably, the orval/packages/core/src/getters/enum.ts Line 14 in db15ab6
Should be used here:
orval/packages/core/src/getters/combine.ts Line 194 in db15ab6
Example OpenAPI spec (this is a slightly simplified version to easily reproduce the issue): paths/example-path: {
"patch": {
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"additionalProperties": false,
"type": "object",
"properties": {
"follows": {
"anyOf": [
{
"type": "string",
"enum": ["example-true"]
},
{
"type": "string",
"enum": ["example-false"]
}
]
},
},
"required": []
}
}
}
},
"responses": {
"200": {
"description": "Example OK"
},
},
}
} |
Uh oh!
There was an error while loading. Please reload this page.
Related
Problem
I've noticed I don't use the generated
enum
s inschemas
.I use only the generated
type
.I assume there are other users-devs who also need ability to disable enum/const generation.
So I propose to add a flag
override.disableEnumGeneration
, related tooverride.useNativeEnums
The text was updated successfully, but these errors were encountered: