Skip to content

Possible bug in mergeRequestParams when --axios is used #203

Closed
@kamzcgcg

Description

@kamzcgcg

Summary:
I believe I identified a bug in the generated code when axios is enabled:

  private mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
    return {
      ...this.instance.defaults,
      ...params1,
      ...(params2 || {}),
      headers: {
        ...this.instance.defaults, // this should be ...this.instance.defaults.headers
        ...(params1.headers || {}),
        ...((params2 && params2.headers) || {}),
      },
    };
  }

line ...this.instance.defaults,

should become

...this.instance.defaults.headers

How to reproduce:

I have debugged the generated code and found that I can reproduce the issue by creating a sample axios code and utilize the mergeRequestParams that was generated by swagger-typescript-api.

Here's a sample code to show the issue:

import axios, {  AxiosRequestConfig } from "axios";

// function copied from the generated code.
function mergeRequestParams(defaultParams: AxiosRequestConfig, params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
  return {
    ...defaultParams,
    ...params1,
    ...(params2 || {}),
    headers: {
      ...defaultParams,
      ...(params1.headers || {}),
      ...((params2 && params2.headers) || {}),
    },
  };
}

(async () => {
  try {

    const client = axios.create({

      transformRequest: [(data, headers) => { return data; }],   // Without this I get: Invalid character in header content ["transformRequest"] 
      transformResponse: [(data, headers) => { return data; }], // Without this I get: Invalid character in header content ["transformResponse"] 

      baseURL: "https://localhost:7001/",
    });

    const requestParams = mergeRequestParams(client.defaults, {}, {})

    client.request({
      ...requestParams,
      headers: {
        "Content-Type": "application/json",
        ...requestParams.headers,
      },
      params: {date:'2020-03-31'},
      responseType: 'json',
      url: '/overview',
    });

  } catch (e) {
    console.log( e);
  }
})();

The error I get here is:

(node:60845) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["adapter"]
    at ClientRequest.setHeader (_http_outgoing.js:529:3)
    at new ClientRequest (_http_client.js:241:14)
    at Object.request (https.js:314:10)
    at RedirectableRequest._performRequest (/Users/kamz/workspace/clarity/clarity-model/node_modules/follow-redirects/index.js:262:24)
    at new RedirectableRequest (/Users/kamz/workspace/clarity/clarity-model/node_modules/follow-redirects/index.js:60:8)
    at Object.request (/Users/kamz/workspace/clarity/clarity-model/node_modules/follow-redirects/index.js:458:14)
    at dispatchHttpRequest (/Users/kamz/workspace/clarity/clarity-model/node_modules/axios/lib/adapters/http.js:195:25)
    at new Promise (<anonymous>)
    at httpAdapter (/Users/kamz/workspace/clarity/clarity-model/node_modules/axios/lib/adapters/http.js:46:10)
    at dispatchRequest (/Users/kamz/workspace/clarity/clarity-model/node_modules/axios/lib/core/dispatchRequest.js:52:10)
(node:60845) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:60845) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions