openapi-typescript
openapi-typescript copied to clipboard
transforming enum to delete the pathname at the enum name
Now i am using 7.0.0-next.7 version with generating enum option at node cli.
with generation enum options, i can easliy generate enum from scheme. but when i checked the generated enum, it has name that is combined with path and enum name.
for instance ,
path : #/components/schemas/com.aaaa.server.core.common.Gender created Enum name : ComAaaaServerCoreCommonGender expected Enum name : Gender
I am trying to remove the path name at the generated enum name.
it seems transform / posttransform can help me, but i'm not used to modifying ts.TypeNode.
i hope some to tell me recommended way.
At this time renaming isn’t supported in TypeScript because it could deviate from your schema; if two objects contained .gender
how would the collision be fixed? Would you have Gender
and Gender2
? And more importantly, would it be clear when importing the type you were importing the correct one?
I’d recommend if you want to refer to an enum often, then move it to be its own top-level schema object in your OpenAPI spec itself. That way your generated types always match 1:1 to your schema, and you’ll never accidentally refer to one type thinking it’s another.
Every Enum that we defined has unique name. there are no way to create several kinds of enum called duplicated name.
is you mean that change the directory of the file that the enum defined to the top ?
Oh I had assumed that when you referenced #/components/schemas/com.aaaa.server.core.common.Gender
, you were referencing multiple nested schema objects com
→ aaa
→ server
→ core
→ common
→ Gender
, where each level was a property. In which case my suggestion would be to move it to #/components/schemas/Gender
, and simply use $ref
wherever it’s needed.
But if you are saying that is just a single schema object with periods in the title, then I think that’s the correct name? We want to match your schema 1:1 perfectly, without renames; but as those contain invalid characters we just do our best translating it into a valid JS identifier. But try and preserve the name as-authored so it’s easy-to-find and there’s no confusion.
Willing to look into it further if you can provide:
- An example OpenAPI schema that generates an enum
- What you expected it to generate
This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.
This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.