Skip to content

Array inside enum is generated incorrectly #445

Closed
@bramirezMAP

Description

@bramirezMAP

When an array is used as the value of an enum, the generated TypeScript code is a comma-separated string of the array's values instead of a proper array.

Steps to reproduce

I created this OpenAPI schema (schema.yml), which is a minimal reproducible example of the issue:

openapi: "3.0.2"
paths:
  /:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  myEnum:
                    enum:
                      - - foo
                        - bar
                        - baz

Then I ran npx swagger-typescript-api -p schema.yml to generate an Api.ts file from it:

/tmp/openapi-mre$ npx swagger-typescript-api -p schema.yml
✨   swagger-typescript-api(11.1.3),npm/8.19.2 node/v16.18.0 linux x64 workspaces/false
✨   try to read templates from directory "/home/myuser/.npm/_npx/ad2f6472e51cc62f/node_modules/swagger-typescript-api/templates/default"
✨   "api" template found in "/home/myuser/.npm/_npx/ad2f6472e51cc62f/node_modules/swagger-typescript-api/templates/default"
✨   "route types" template found in "/home/myuser/.npm/_npx/ad2f6472e51cc62f/node_modules/swagger-typescript-api/templates/default"
✨   try to get swagger by path "/tmp/openapi-mre/schema.yml"
☄️    start generating your typescript api
⚠️    wrong name of the model name [ 'foo', 'bar', 'baz' ]
✅   typescript api file Api.ts created in /tmp/openapi-mre

Actual

In Api.ts, the Api.getRoot function appears as follows:

getRoot = (params: RequestParams = {}) =>
    this.request<
      {
        myEnum?: "foo,bar,baz";  // [problem is here]
      },
      any
    >({
      path: `/`,
      method: "GET",
      format: "json",
      ...params,
    });

Expected

In Api.ts, the Api.getRoot function should appear as follows:

getRoot = (params: RequestParams = {}) =>
    this.request<
      {
        myEnum?: ["foo", "bar", "baz"];
      },
      any
    >({
      path: `/`,
      method: "GET",
      format: "json",
      ...params,
    });

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions