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

Array inside enum is generated incorrectly

Open bramirezMAP opened this issue 4 years ago • 3 comments

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,
    });

bramirezMAP avatar Nov 07 '22 19:11 bramirezMAP

Hi @bramirezMAP
I think expected should be

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

Because swagger schema has this enum declaration

myEnum:
                    enum:
                      - - foo
                        - bar
                        - baz

Where

- - 
   - 
   - 

is meaning the array of values

js2me avatar Nov 08 '22 09:11 js2me

Hi @js2me, could you please double-check your expectations with json-schema-to-typescript. An equivalent JSON schema would be:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "myEnum": {
      "enum": [["foo", "bar", "baz"]]
    }
  },
  "additionalProperties": false
}

The output is:

/* tslint:disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

export interface ExampleSchema {
  myEnum?: ["foo", "bar", "baz"];
}

Each element inside an enum clause is a literal value. This schema describes a property myEnum which can only have a single value, which is the array ["foo", "bar", "baz"]. See the draft-04 reference.

bramirezMAP avatar Nov 08 '22 15:11 bramirezMAP

Hi @bramirezMAP , your problem should be fixed in 12.0.0.
Please reopen this issue if problem is still occurring.

js2me avatar Nov 13 '22 07:11 js2me