tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

Bug: Enums will not correctly generarted

Open fiftyy opened this issue 10 months ago • 5 comments

Hi, i try to generate some enums.

export type AddedRemoveEvent = {
  type: EventType.ADDED | EventType.REMOVED;
  target: EventUser;
};

export type AssignUnassignEvent = {
  type: EventType.ASSIGNED | EventType.UNASSIGNED;
  target: EventUser;
};

export type RatedEvent = {
  type: EventType.RATED;
  rating: number;
};

export type OpenClosedEvent = {
  type: EventType.OPENED | EventType.CLOSED;
  previousState: TicketStatus;
};

export type CreatedDeletedEvent = {
  type: EventType.CREATED | EventType.DELETED;
};

export type EventData =
  | AddedRemoveEvent
  | AssignUnassignEvent
  | RatedEvent
  | OpenClosedEvent
  | CreatedDeletedEvent;

export enum EventType {
  CREATED = "created",
  CLOSED = "closed",
  OPENED = "opened",
  DELETED = "deleted",
  RATED = "rated",
  ADDED = "added",
  REMOVED = "removed",
  COMMAND = "command",
  ASSIGNED = "assigned",
  UNASSIGNED = "unassigned",
  OTHER = "other",
}

This will generate:

"EventType.ADDED": {
        "enum": [
          "added"
        ],
        "type": "string",
        "x-enum-varnames": [
          "ADDED"
        ]
      },
      "EventType.REMOVED": {
        "enum": [
          "removed"
        ],
        "type": "string",
        "x-enum-varnames": [
          "REMOVED"
        ]
      },
      "AddedRemoveEvent": {
        "properties": {
          "target": {
            "$ref": "#/components/schemas/EventUser"
          },
          "type": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/EventType.ADDED"
              },
              {
                "$ref": "#/components/schemas/EventType.REMOVED"
              }
            ]
          }
        },
        "required": [
          "target",
          "type"
        ],
        "type": "object"
      },
      "EventType.ASSIGNED": {
        "enum": [
          "assigned"
        ],
        "type": "string",
        "x-enum-varnames": [
          "ASSIGNED"
        ]
      },
      "EventType.UNASSIGNED": {
        "enum": [
          "unassigned"
        ],
        "type": "string",
        "x-enum-varnames": [
          "UNASSIGNED"
        ]
      },
      "AssignUnassignEvent": {
        "properties": {
          "target": {
            "$ref": "#/components/schemas/EventUser"
          },
          "type": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/EventType.ASSIGNED"
              },
              {
                "$ref": "#/components/schemas/EventType.UNASSIGNED"
              }
            ]
          }
        },
        "required": [
          "target",
          "type"
        ],
        "type": "object"
      },

I dont post the full swagger file.

You see that tsoa is generating everytime a new enum for each used enum. Is their away to prevent this?

fiftyy avatar Mar 04 '25 11:03 fiftyy

There's a bug in the typeResolver.ts that enums types are resolved to the first element of the enum values list

clostao avatar Mar 07 '25 01:03 clostao

There's a bug in the typeResolver.ts that enums types are resolved to the first element of the enum values list

I think that might be less of a bug and more of a choice. A lot of tools don't support them.

jefflembeck avatar May 15 '25 15:05 jefflembeck

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Oct 13 '25 00:10 github-actions[bot]

As always: TSOA cannot handle interface properties that use already-initialized enums.

fiftyy avatar Nov 05 '25 23:11 fiftyy

How is that a feature, this breaks my whole type validation

CowCatDE avatar Nov 05 '25 23:11 CowCatDE