Skip to content

[typescript-fetch] (4.0.X) Missing queryParameters initialization #1535

@someone1

Description

@someone1
Description

Testing out the updated generator from PR #569 and I noticed an issue where I have multiple authentication requirements for an API endpoint (logical AND - both required) for an API key via query parameter and via an Authorization header. The generated code places both in correctly, however, for endpoints where I otherwise have no other query parameters (e.g. the authentication method is the only one), the variable queryParameters is never defined and its not passed in to the request.

openapi-generator version

4.0.0-20181118.115720-41

OpenAPI declaration file content or url (partial)
swagger: "2.0"

paths:
  "/admin/users/{id}/clearTokens":
    post:
      operationId: "users.clear_tokens"
      responses:
        204:
          description: Success
      parameters:
        - in: path
          name: id
          required: true
          type: string
      security:
        - google_id_token: []
          api_key: []

securityDefinitions:
  api_key:
    type: "apiKey"
    name: "key"
    in: "query"
  google_id_token:
    authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth"
    flow: "implicit"
    type: "oauth2"

Generated Code excerpt:

async usersClearTokensRaw(requestParameters: UsersClearTokensRequest): Promise<runtime.ApiResponse<void>> {
        if (requestParameters.id === null || requestParameters.id === undefined) {
            throw new runtime.RequiredError('id','Required parameter requestParameters.id was null or undefined when calling usersClearTokens.');
        }

        const headerParameters: runtime.HTTPHeaders = {};

        if (this.configuration && this.configuration.apiKey) {
            queryParameters["key"] = this.configuration.apiKey("key"); // api_key authentication
        }

        if (this.configuration && this.configuration.accessToken) {
            // oauth required
            if (typeof this.configuration.accessToken === 'function') {
                headerParameters["Authorization"] = this.configuration.accessToken("google_id_token", []);
            } else {
                headerParameters["Authorization"] = this.configuration.accessToken;
            }
        }

        const response = await this.request({
            path: `/admin/users/{id}/clearTokens`.replace(`{${"id"}}`, encodeURIComponent(String(requestParameters.id))),
            method: 'POST',
            headers: headerParameters,
        });

        return new runtime.VoidApiResponse(response);
    }
Command line used for generation

openapi-generator generate -i openapi.yaml -l typescript-fetch -c es6.json -o ./typescript-api/

Steps to reproduce
  1. Use the provided yaml excerpt above to generate a client with the provided command line
  2. Buggy code generated
Related issues/PRs

PR #569

Suggest a fix/enhancement

Template variable #hasQueryParams should be set to true if an authentication method required for that endpoint uses query parameters.

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