lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.42k stars 489 forks source link

Including `length` as an enum value breaks spec generation #1523

Closed arcticfly closed 7 months ago

arcticfly commented 8 months ago

The word length, if included as part of the enum returned by a route on a controller, prevents the spec from being generated properly.

Sorting

Expected Behavior

The openapi documentation should have generated as normal.

components:
    examples: {}
    headers: {}
    parameters: {}
    requestBodies: {}
    responses: {}
    schemas: {}
    securitySchemes:
        Authorization:
            type: http
            scheme: bearer
info:
    title: 'OpenPipe API'
    version: 1.0.1
    description: 'The public API for reporting calls and generating inference with OpenPipe.'
openapi: 3.0.0
paths:
    /basic:
        post:
            operationId: CreateChatCompletion
            responses:
                '200':
                    description: Ok
                    content:
                        application/json:
                            schema:
                                type: string
                                enum:
                                    - hi
                                    - there
                                    - length
            security: []
            parameters: []
servers:
    -
        url: /api/v2

Current Behavior

No yaml file was generated. Instead, I see the following error:

node_modules/@tsoa/cli/dist/swagger/specGenerator3.js:556
                    enumValuesByType[type.type][enumValue] = enumValue;
                                                           ^

RangeError: Invalid array length
    at SpecGenerator3.groupEnums

Possible Solution

Steps to Reproduce

newControllers/basicController.ts:

import { Controller, Post, Route } from "tsoa";

@Route("basic")
export class BasicController extends Controller {
  @Post()
  public async createChatCompletion(): Promise<"hi" | "there" | "length"> {
    console.log("in basic controller");

    await new Promise((resolve) => setTimeout(resolve, 1000));
    return "hi";
  }
}

generate.ts:

  const swaggerDocsSpecOptions: ExtendedSpecConfig = {
    noImplicitAdditionalProperties: "ignore",
    controllerPathGlobs: [TSOA_BASE_PATH + "/newControllers/*Controller.ts"],
    basePath: "/api/v2",
    entryFile: "src/server.ts",
    securityDefinitions: {
      Authorization: {
        type: "http",
        scheme: "bearer",
      },
    },
    specVersion: 3,
    outputDirectory: TSOA_BASE_PATH + "/generated",
    name: "OpenPipe API",
    description: "The public API for reporting calls and generating inference with OpenPipe.",
    version: "1.0.1",
  };

  await generateSpec(swaggerDocsSpecOptions);
  1. Run tsx generate.ts
      1. 4.

Context (Environment)

Version of the library: 5.1.1 Version of NodeJS: 20.3.1

Detailed Description

I'd like to be able to use length as a potential enum value and still be able to generate openapi docs.

github-actions[bot] commented 8 months ago

Hello there arcticfly 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

jackey8616 commented 7 months ago

I think this issue is no longer exists @ 6.0.1. I cannot reproduce it.

@WoH please consider close this issue.