Skip to content

[BUG] TypeScript-Fetch: Headers passed in configurations are dropped in API calls #1882

Closed
@emadelwany

Description

@emadelwany

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [] [Optional] Bounty to sponsor the fix (example)
Description

headers passed as part of a Configuration's BaseOptions property (Configuuration::BaseOptions::Headers) in calls to Api Factories are dropped.

The logic is in *FetchParamCreator doesn't copy them over to the final headers as part of param creation. This is obvious in the code below, the line

            const localVarHeaderParameter = {} as any;

Should copy them over

            const localVarPath = `/api/Users`;
            const localVarUrlObj = url.parse(localVarPath, true);
            let baseOptions;
            if (configuration) {
                baseOptions = configuration.baseOptions;
            }
            const localVarRequestOptions = Object.assign({ method: 'GET' }, baseOptions, options);
            const localVarHeaderParameter = {} as any;
            const localVarQueryParameter = {} as any;

            localVarUrlObj.query = Object.assign({}, localVarUrlObj.query, localVarQueryParameter, options.query);
            // fix override query string Detail: https://stackoverflow.com/a/7517673/1077943
            delete localVarUrlObj.search;
            localVarRequestOptions.headers = Object.assign({}, localVarHeaderParameter, options.headers);

            return {
                url: url.format(localVarUrlObj),
                options: localVarRequestOptions,
            };
openapi-generator version

Discovered in 3.2.3, still occurring in 3.3.4

OpenAPI declaration file content or url
{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "API"
  },
  "paths": {
    "/api/Users": {
      "get": {
        "tags": [
          "Users"
        ],
        "operationId": "Get",
        "consumes": [
          
        ],
        "produces": [
          "text/plain",
          "application/json",
          "text/json"
        ],
        "parameters": [
          
        ],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "$ref": "#/definitions/User"
            }
          }
        }
      }
    }
  },
  "definitions": 
    "User": {
      "type": "object",
      "properties": {
        "id": {
          "format": "uuid",
          "type": "string"
        },
        "email": {
          "type": "string"
        },
        "created": {
          "format": "date-time",
          "type": "string",
          "example": "2019-01-11T06:02:07.4817678Z"
        },
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "roles": {
          "uniqueItems": false,
          "type": "array",
          "items": {
            "$ref": "#/definitions/Role"
          }
        }
      }
    }
  }
}

(for JSON code), so it becomes more readable. If it is longer than about ten lines,
please create a Gist (https://gist.github.com) or upload it somewhere else and
link it here.
-->

Command line used for generation

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v3.3.4 generate
-i "http://host.docker.internal/swagger/v1/swagger.json"
-g typescript-fetch
--type-mappings Date=string
-o /local/src/swagger

Steps to reproduce
Related issues/PRs
Suggest a fix

One simple fix is to update the code under:
https://github.com/OpenAPITools/openapi-generator/blob/0eb385c0d6497c38c09b42158b2d66109b78b15b/modules/openapi-generator/src/main/resources/Javascript-Flowtyped/api.mustache

to properly assign Confiugration:BaseOptions:Headers to localVarHeaderParameter

This bug affects most TypeScript implementations and a similar fixed is needed in all of them.

I can create a PR if needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions