swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

Enums not generated

Open AsasInnab opened this issue 3 years ago • 7 comments

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';
}

AsasInnab avatar Feb 23 '22 13:02 AsasInnab

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...

dkocich avatar Apr 14 '22 06:04 dkocich

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

aleshka-maple avatar Apr 20 '22 22:04 aleshka-maple

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

magersoft avatar Apr 27 '22 14:04 magersoft

Facing the same issue~

LintaoAmons avatar May 28 '22 03:05 LintaoAmons

Same here.

wirekang avatar Jul 20 '22 00:07 wirekang

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.

wirekang avatar Jul 26 '22 01:07 wirekang

I try this, but not working. I got same string in the generated file with different enumName property

magersoft avatar Aug 02 '22 09:08 magersoft

Any updates on this? Having the exact same issue. Should just work out of the box right?

evtk avatar Nov 10 '22 20:11 evtk

@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

js2me avatar Nov 10 '22 21:11 js2me

Any ideas of how can I name keys of enum construction based on this schema ?

image

enum TreeMode {
  100644 = 100644,
  100755= 100755,
  040000= 040000,
  160000= 160000,
  120000= 120000,
}

js2me avatar Nov 10 '22 21:11 js2me

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 )

image

@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

dkocich avatar Nov 11 '22 06:11 dkocich

@evtk @dkocich @AsasInnab problem should be fixed in Release 12.0.0.
Reopen this issue if problem is still occurring

js2me avatar Nov 13 '22 07:11 js2me

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"; }[]; }

evtk avatar Nov 14 '22 08:11 evtk

@js2me the index.js is missing the mapping for extactEnums inside the generateApi

evtk avatar Nov 14 '22 08:11 evtk

@evtk sorry about that :)
Fixed in 12.0.2, please check again

js2me avatar Nov 14 '22 10:11 js2me

@js2me yes confirmed that it now works. Thanks for the quicky!

evtk avatar Nov 14 '22 12:11 evtk

@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 avatar Nov 14 '22 14:11 dkocich

@dkocich can you share part of your schema where you have the enum which should be extracted ?

js2me avatar Nov 14 '22 20:11 js2me

@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"
}

dkocich avatar Nov 14 '22 21:11 dkocich