openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

transforming enum to delete the pathname at the enum name

Open doyoonkim12345 opened this issue 1 year ago • 4 comments

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.

doyoonkim12345 avatar Feb 19 '24 02:02 doyoonkim12345

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.

drwpow avatar Feb 19 '24 06:02 drwpow

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 ?

doyoonkim12345 avatar Feb 20 '24 05:02 doyoonkim12345

Oh I had assumed that when you referenced #/components/schemas/com.aaaa.server.core.common.Gender, you were referencing multiple nested schema objects comaaaservercorecommonGender, 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.

drwpow avatar Feb 23 '24 00:02 drwpow

Willing to look into it further if you can provide:

  1. An example OpenAPI schema that generates an enum
  2. What you expected it to generate

drwpow avatar Mar 06 '24 05:03 drwpow

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.

github-actions[bot] avatar Aug 06 '24 12:08 github-actions[bot]

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.

github-actions[bot] avatar Aug 14 '24 02:08 github-actions[bot]