OpenAPITools / OpenAPITools/openapi-generator

[BUG] Change configuration parameters for typescript-fetch

Open
#10,065 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Yes. Previously, we used to be able to specify how username and password are fetched during API calls. The ConfigurationParameters was defined as:

interface ConfigurationParameters {
  // other fields
  username?: string | () => string;
  password?: string | () => string;
}

In this previous interface, we used to be able to specify configuration as follows:

const configuration = new Configuration({
  username: () => getUsernameFromLocalStorage(),
  password: () => getPasswordFromLocalStorage(),
});

But in the new versions, the ConfigurationParameters interface has been changed, where it only accepts a string for username and password:

interface ConfigurationParameters {
  // other fields
  username?: string;
  password?: string;
}

Link to the generated code

This has made it harder to login and logout the users as login means we specify new username and password and logout means we delete any username and password.

Describe the solution you'd like

A solution would be to revert back to the old ConfigurationParameters interface.

Another solution would be to generate Configuration like it is on angular's generated code:

export interface ConfigurationParameters {
    /**
     *  @deprecated Since 5.0. Use credentials instead
     */
    apiKeys?: {[ key: string ]: string};
    username?: string;
    password?: string;
    /**
     *  @deprecated Since 5.0. Use credentials instead
     */
    accessToken?: string | (() => string);
    basePath?: string;
    withCredentials?: boolean;
    encoder?: HttpParameterCodec;
    /**
     * The keys are the names in the securitySchemes section of the OpenAPI
     * document. They should map to the value used for authentication
     * minus any standard prefixes such as 'Basic' or 'Bearer'.
     */
    credentials?: {[ key: string ]: string | (() => string | undefined)};
}

Here, username and password have been deprecated and use only string, but credentials can use functions, which allows us to specify username and password when api is being called.

Describe alternatives you've considered

I have been using this hack to get around this problem:

const httpBasicInterceptor = new (class implements Middleware {
  async pre(context: RequestContext): Promise<FetchParams | void> {
    const username = AuthStorage.getUsername();
    const password = AuthStorage.getPassword();

    const oldHeaders = context.init.headers;
    let newHeaders;

    const AUTHORIZATION_KEY = 'Authorization';
    const basicAuth = 'Basic ' + btoa(`${username}:${password}`);

    if (oldHeaders !== undefined) {
      if (typeof oldHeaders == 'object') {
        newHeaders = {
          ...oldHeaders,
          [AUTHORIZATION_KEY]: basicAuth,
        };
      } else if (Array.isArray(oldHeaders)) {
        newHeaders = oldHeaders as string[][];
        newHeaders.push([AUTHORIZATION_KEY, basicAuth]);
      } else {
        newHeaders = oldHeaders as Headers;
        newHeaders.append(AUTHORIZATION_KEY, basicAuth);
      }
    }

    return {
      url: context.url,
      init: {
        ...context.init,
        headers: newHeaders,
      },
    };
  }
})();
const privateConfigParameters: ConfigurationParameters = {
  ...basicConfigParameters,
  middleware: [httpBasicInterceptor],
  username: '',
  password: '',
};
const privateConfig = new Configuration(privateConfigParameters);

But this is complex and harder to read. Also, there might be some circumstances where this solution might fail to work.

Additional context

No other context required to understand this problem.

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 by comparing the generated samples/client/petstore/typescript-fetch/builds/default/runtime.ts ConfigurationParameters with the referenced typescript-angular-v9-provided-in-root/builds/default/configuration.ts. Trace the typescript-fetch generator entry point and its templates to determine where credentials are defined. Done means generated TypeScript Fetch clients can obtain username and password dynamically without the middleware workaround.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.