From b0e2ab67a6dcee1a9b1c19a1f53225cc06fefe7d Mon Sep 17 00:00:00 2001 From: yongmingl <63903220+yongmingl@users.noreply.github.com> Date: Thu, 14 Oct 2021 14:16:22 +0800 Subject: [PATCH] Add hasQueryParamsOrAuth to reflect apiKey in query (#10582) Compilation fails in generated typescript-angular client when an `apiKey` is passed in query due to an uninitialised variable. This change adds `getHasQueryParamsOrAuth()` to reflect if there's at least one query parameter or passing API keys in query in the `hasQueryParamsOrAuth` template variable. --- .../java/org/openapitools/codegen/CodegenOperation.java | 9 +++++++++ .../resources/typescript-angular/api.service.mustache | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java index 0a8e7b31991c..8d8e9cdc13e9 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java @@ -87,6 +87,15 @@ public class CodegenOperation { return nonempty(queryParams); } + /** + * Check if there's at least one query parameter or passing API keys in query + * + * @return true if query parameter exists or passing API keys in query, false otherwise + */ + public boolean getHasQueryParamsOrAuth() { + return getHasQueryParams() || (authMethods != null && authMethods.stream().anyMatch(authMethod -> authMethod.isKeyInQuery)); + } + /** * Check if there's at least one header parameter * diff --git a/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache b/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache index 2ff5a17bb294..02c52d23355c 100644 --- a/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache @@ -178,7 +178,7 @@ export class {{classname}} { {{/required}} {{/allParams}} -{{#hasQueryParams}} +{{#hasQueryParamsOrAuth}} let localVarQueryParameters = new HttpParams({encoder: this.encoder}); {{#queryParams}} {{#isArray}} @@ -209,7 +209,7 @@ export class {{classname}} { {{/isArray}} {{/queryParams}} -{{/hasQueryParams}} +{{/hasQueryParamsOrAuth}} let localVarHeaders = this.defaultHeaders; {{#headerParams}} {{#isArray}} @@ -359,9 +359,9 @@ export class {{classname}} { {{#httpContextInOptions}} context: localVarHttpContext, {{/httpContextInOptions}} - {{#hasQueryParams}} + {{#hasQueryParamsOrAuth}} params: localVarQueryParameters, - {{/hasQueryParams}} + {{/hasQueryParamsOrAuth}} {{#isResponseFile}} responseType: "blob", {{/isResponseFile}}