[BUG][typescript-angular] Compile-error TS7030 in configuration.ts when using Basic-Auth and "noImplicitReturns"
Created by: HexagonSun
Description
When generating a typescript-angular client using security schema "basicAuth", the generated configuration.ts
does not compile when using typescript's compiler-flag noImplicitReturns
.
Although this flag is not enabled by default, Angular's official examples do use it (see e.g. this stackblitz example linked in the Tour of Heroes example here).
openapi-generator version
v5.1.0
The bug occurs in all versions since at least 5.0.0. I've previously used 4.3.0 where the issue did not occur.
OpenAPI declaration file content or url
openapi: 3.0.0
info:
version: '1.0'
title: Test API
paths:
/test:
get:
responses:
default:
description: test path
components:
securitySchemes:
basicAuth:
type: http
scheme: basic
security:
- basicAuth: []
Generation Details
Nothing special: a regular generation command for a typescript-angular client, see steps to reproduce below.
Steps to reproduce
- Generate an angular-typescript client from the OpenAPI declaration above
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \ -i $TMPDIR/configuration-TS7030/issue.yaml \ -g typescript-angular \ -o $TMPDIR/configuration-TS7030/client
- Compile the file using compiler-flag
noImplicitReturns
npx -p typescript tsc \ --noEmit \ --lib es2015,dom \ --noImplicitReturns \ $TMPDIR/configuration-TS7030/client/configuration.ts
The output shows a compiler error TS7030:
$ npx -p typescript tsc --noEmit --lib es2015,dom --noImplicitReturns $TMPDIR/configuration-TS7030/client/configuration.ts
../../../../../var/folders/9n/_t8fsm2d4hv8lyjl_zh5873c0000gn/T/configuration-TS7030/client/configuration.ts:1:36 - error TS2307: Cannot find module '@angular/common/http' or its corresponding type declarations.
1 import { HttpParameterCodec } from '@angular/common/http';
~~~~~~~~~~~~~~~~~~~~~~
../../../../../var/folders/9n/_t8fsm2d4hv8lyjl_zh5873c0000gn/T/configuration-TS7030/client/configuration.ts:63:45 - error TS7030: Not all code paths return a value.
63 this.credentials['basicAuth'] = () => {
~~~~~~~
Found 2 errors.
(Note that the first error is caused by the generated OpenAPI client not being part of an Angular project).
Related issues/PRs
The issue seems to be a regression of #6944 (closed) / PR 6953
Suggest a fix
The issue is trivially resolved by changing the mustache template configuration.moustache
:
diff --git a/modules/openapi-generator/src/main/resources/typescript-angular/configuration.mustache b/modules/openapi-generator/src/main/resources/typescript-angular/configuration.mustache
index e3a5e824385..1566cd4c946 100644
--- a/modules/openapi-generator/src/main/resources/typescript-angular/configuration.mustache
+++ b/modules/openapi-generator/src/main/resources/typescript-angular/configuration.mustache
@@ -73,6 +73,7 @@ export class {{configurationClassName}} {
if (this.username || this.password) {
return btoa(this.username + ':' + this.password);
}
+ return undefined;
};
{{/isBasicBasic}}
{{#isBasicBearer}}
Or, a bit nicer using a ternary operator as other methods nearby do, too:
diff --git a/modules/openapi-generator/src/main/resources/typescript-angular/configuration.mustache b/modules/openapi-generator/src/main/resources/typescript-angular/configuration.mustache
index e3a5e824385..6c2082f5175 100644
--- a/modules/openapi-generator/src/main/resources/typescript-angular/configuration.mustache
+++ b/modules/openapi-generator/src/main/resources/typescript-angular/configuration.mustache
@@ -70,9 +70,9 @@ export class {{configurationClassName}} {
{{#isBasic}}
{{#isBasicBasic}}
this.credentials['{{name}}'] = () => {
- if (this.username || this.password) {
- return btoa(this.username + ':' + this.password);
- }
+ return this.username || this.password
+ ? btoa(this.username + ':' + this.password)
+ : undefined;
};
{{/isBasicBasic}}
{{#isBasicBearer}}
Changing the code to always provide a return value does not affect compilation without noImplicitReturns
(equally trivially verified by running the steps to reproduce withouth specifying --noImplicitReturns
).