OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-fetch] Missing 4XX and 5XX response

Open
#16,146 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I have an openapi spec for authentification with few responses types:

"/authentication": {
  "post": {
    "tags": [
      "authenticate_user"
    ],
    "operationId": "authenticate",
    "requestBody": {
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Auth"
          }
        }
      },
      "required": true
    },
    "responses": {
      "200": {
        "description": "Returns current authentication state.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/AuthResponse"
            }
          }
        }
      },
      "4XX": {
        "description": "",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponseObject"
            }
          }
        }
      },
      "5XX": {
        "description": "",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponseObject"
            }
          }
        }
      }
    }
  }
}

The AuthResponse & ErrorResponseObject are correctly created, but ErrorResponseObject is not returned in case of 4XX or 5XX errors.

This is the generated file:

import * as runtime from '../runtime';
import type {
  Auth,
  AuthResponse,
  ErrorResponseObject,
} from '../models';
import {
    AuthFromJSON,
    AuthToJSON,
    AuthResponseFromJSON,
    AuthResponseToJSON,
    ErrorResponseObjectFromJSON,
    ErrorResponseObjectToJSON,
} from '../models';

export interface AuthenticateRequest {
    auth: Auth;
}

export class AuthenticateUserApi extends runtime.BaseAPI {
    async authenticateRaw(requestParameters: AuthenticateRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<AuthResponseErrorResponseObject>> {
        if (requestParameters.auth === null || requestParameters.auth === undefined) {
            throw new runtime.RequiredError('auth','Required parameter requestParameters.auth was null or undefined when calling authenticate.');
        }

        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        headerParameters['Content-Type'] = 'application/json';

        const response = await this.request({
            path: `/authentication`,
            method: 'POST',
            headers: headerParameters,
            query: queryParameters,
            body: AuthToJSON(requestParameters.auth),
        }, initOverrides);

        return new runtime.JSONApiResponse(response, (jsonValue) => AuthResponseFromJSON(jsonValue));
    }

    async authenticate(requestParameters: AuthenticateRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<AuthResponse> {
        const response = await this.authenticateRaw(requestParameters, initOverrides);
        return await response.value();
    }

}

As you can see ErrorResponseObject is imported but never used.

This creates multiple issues down the line. The main one is that I cannot use the ErrorResponseObject attributes to let the user know what the error actually is.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the generated AuthenticateUserApi example and trace how the OpenAPI 4XX and 5XX responses are represented in the typescript-fetch generator. Add regression coverage for an operation with those responses, and consider the work complete when the generated client exposes and parses ErrorResponseObject for both status ranges.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.