swagger-typescript-api
swagger-typescript-api copied to clipboard
Enums not generated
I can't seem to get enums to be generated. What I understand from the documentation is that enums should be generated by default.
I'm trying out the petstore example:
npx swagger-typescript-api -p https://petstore.swagger.io/v2/swagger.json -o ./generated2 --enum-names-as-values
And I get status as an union type instead of an enum although I have not set --union-enums
flag:
export interface Pet {
/** @format int64 */
id?: number;
category?: Category;
/** @example doggie */
name: string;
photoUrls: string[];
tags?: Tag[];
/** pet status in the store */
status?: 'available' | 'pending' | 'sold';
}
I also noticed this. We have multiple docs and from YAML we get enums successfully but from JSON we do not and there is always string union...
I guess this happens because enam "status" is defined as a property of Pet with definition
{ type: 'string', enum: ['available', 'pending', 'sold'] }
in swagger.json but this lib generates types relaying on 'components/schemas/...' from swagger.json
Working example (swagger.json)
{
...,
"components": {
"schemas": {
"Pet": {
"type": "object",
"properties": {
"status": {
"$ref": "#/components/schemas/PetStatusEnum"
}
}
},
"PetStatusEnum": {
"type": "string",
"enum": ["available", "pending", "sold"]
}
}
}
}
It also makes PetStatusEnam reusable for other definitions
I have the same issue, Enums not generated
my swagger schema
{
"name": "type",
"required": false,
"in": "query",
"schema": {
"enum": [
"TEXT",
"DIGIT",
"DATE",
"TABLE"
],
"type": "string"
}
},
but generated file
type: 'TEXT' | 'DIGIT' | 'DATE' | 'TABLE'
I have not set --union-enums flag
Facing the same issue~
Same here.
I found solution!
Not working example:
enum MyEnum {
Asdf = 'asdf',
Qwer = 'qwer',
}
export class MyClass {
@ApiProperty({enum: MyEnum })
my: MyEnum
}
Working example:
enum MyEnum {
Asdf = 'asdf',
Qwer = 'qwer',
}
export class MyClass {
@ApiProperty({enum: MyEnum, enumName: "MyEnum" })
my: MyEnum
}
If you set enumName
property, as @aleshka-maple said, the type generated in components/schemas
.
I try this, but not working. I got same string in the generated file with different enumName property
Any updates on this? Having the exact same issue. Should just work out of the box right?
@evtk @dkocich @aleshka-maple @magersoft will add flag --extract-enums
for extracting enums into enum
Currently It's not working as you want because I don't have any name for enums, but I with using --extract-enums
flag swagger-typescript-api will generate enum name based on property and schema name
Any ideas of how can I name keys of enum construction based on this schema ?
enum TreeMode {
100644 = 100644,
100755= 100755,
040000= 040000,
160000= 160000,
120000= 120000,
}
we started naming enums manually based on the generated enum and the property so we can find the generated schema easily and migrate later to generated enums (so ...DTO + State/Type )

@js2me I would add an extra character in front of numeric values (something like _ / x / num) but those can be also -1, 0.1, etc. so it might be harder to check
@evtk @dkocich @AsasInnab problem should be fixed in Release 12.0.0.
Reopen this issue if problem is still occurring
Thanks @js2me. I tested out locally, but cannot confirm yet it is working. So I took your test example to verify.
npm ls swagger-typescript-api
-> [email protected]
swagger-typescript-api -p path-to-the.json --output -o output -n api.ts --extract-enums
Expected Result export interface Tree { tree?: { mode?: TreeMode; "mode-num"?: TreeModeNum; type?: TreeType; bereke?: TreeBereke; }[]; }
Actual Result export interface Tree { tree?: { mode?: "100644" | "100755" | "040000" | "160000" | "120000"; "mode-num"?: 100644 | 100755 | 40000 | 160000 | 120000; type?: "blob" | "tree" | "commit"; bereke?: "Bla" | "Blabla" | "Boiler"; }[]; }
@js2me the index.js is missing the mapping for extactEnums
inside the generateApi
@evtk sorry about that :)
Fixed in 12.0.2, please check again
@js2me yes confirmed that it now works. Thanks for the quicky!
@js2me I was checking 12.0.0, it did not work; now with 12.0.2 it is hanging there with no progress s in my two projects (I used it with and without pre/suffixes swagger-typescript-api -p src/docs/swaggerSpecV2.json --extract-enums --type-prefix "prefix" --type-suffix "suffix" --debug -o ./src/gen/api
)... with additional --debug
I can see only this which does not tell much where the problem is; any clue what might go wrong or how to provide more info?
resolving name with using [ null ]
trying to resolve name with using fallback name generator
resolving name with using [Function (anonymous)]
resolving name with using [ null ]
trying to resolve name with using fallback name generator
resolving name with using [Function (anonymous)]
@dkocich can you share part of your schema where you have the enum which should be extracted ?
@js2me I tried only with one API route and state
which was successfully extracted to the TS file. The problem is somewhere else in the rest of 25000+ lines long API spec which appears to be correct and displayed correctly in web IDE such as https://editor-next.swagger.io/ . I can't share the complete file. I see it hangs in the same state when I tried to run transformation inside the project's repo using "cli:json" NPM script. I will try to debug a give more info in a day or two
{
"openapi": "3.0.0",
"paths": {
"/api/v1/assign-subtype/{subtypeId}": {
"post": {
"tags": [""],
"summary": "",
"parameters": [
],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AdminUserCardResponseDTO"
}
}
}
},
"404": { "description": "" }
},
"operationId": "AdminUserCard_addAUserCardSubtypeToAUserCard"
}
}
},
"components": {
"schemas": {
"AdminUserCardResponseDTO": {
"properties": {
"id": { "type": "number", "example": "100" },
"state": {
"type": "string",
"enum": [
"NEW",
"PENDING"
],
"example": "APPROVED"
},
},
"type": "object",
"required": [
"id",
"state"
]
}
},
"securitySchemes": {
"Bearer": {
"type": "apiKey",
"description": "Value: Bearer {token}",
"name": "Authorization",
"in": "header"
}
}
},
"security": [{ "Bearer": [] }],
"info": {
"contact": {
"name": "",
"url": "",
"email": ""
},
"description": "",
"title": "API",
"version": "1.0.0",
"license": { "name": "MIT", "url": "https://opensource.org/licenses/MIT" },
"termsOfService": "",
"x-logo": { "url": "" },
"x-tagGroups": [
{
"name": "Admin",
"tags": [
"Admin User",
"Admin Feed"
]
},
{
"name": "General",
"tags": [
"Feed"
]
}
]
}
}
export interface AdminUserCardResponseDTO {
/** @example "100" */
id: number;
/** @example "APPROVED" */
state: AdminUserCardResponseDtoState;
}
/** @example "APPROVED" */
export enum AdminUserCardResponseDtoState {
NEW = "NEW",
PENDING = "PENDING"
}