lukeautry / tsoa

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

ValidateError does not produce valid swagger.json #1500

Closed leherv closed 10 months ago

leherv commented 1 year ago

Like you describe in your docs (https://tsoa-community.github.io/docs/error-handling.html) one can return the FieldErrors type directly if a ValidateError occurs. In my API I use those FieldErrors to create an ErrorResponse type which looks like this:

export type ErrorResponse = {
  code: ErrorCode;
  message: string;
  details?: ErrorDetails;
};

export type ErrorDetails = {
  [name: string]: {
    message: string;
    value?: any;
  };
};

export const enum ErrorCode {
  generalError = "general.error",
  validationError = "validation.error",
}

As you can see ErrorDetails matches the FieldErrors. I then annotate a controller method with this response type in the case an error occurred

@Response(400, "Request validation failed.")

If I then look at the generated swagger.json output it looks like this for the ErrorResponse type:

ErrorCode:
      enum:
        - general.error
        - validation.error
      type: string
    ErrorDetails:
      properties: {}
      additionalProperties:
        properties:
          value: {}
          message:
            type: string
        required:
          - message
        type: object
      type: object
    ErrorResponse:
      properties:
        details:
          $ref: '#/components/schemas/ErrorDetails'
        message:
          type: string
        code:
          $ref: '#/components/schemas/ErrorCode'
      required:
        - message
        - code
      type: object

A generated client (swagger editor) typescript-axios generates this ErrorDetails type for the ErrorResponse:

export interface ErrorDetails extends null<String, any> {
}

Sorting

Expected Behavior

I would expect that I can use ErrorFields as a valid response, as it is also included in the docs.

Current Behavior

Currently as already mentioned this does not generate a valid client.

Possible Solution

As a workaround I tried mapping the ErrorFields to a Map<string, X> but quickly found out that there is an open issue with Maps.

github-actions[bot] commented 1 year ago

Hello there leherv 👋

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

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

leherv commented 12 months ago

Hello again,

it seems the problem could be related to this: https://github.com/swagger-api/swagger-codegen/issues/11901. Maybe you still have a hint or advice on how I can get this to work.

Best regards

github-actions[bot] commented 11 months ago

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