Output from Angular-Typescript generator cannot be packaged/compiled because Authorization query parameter variable is not initialized.
public getKeyResult(id: number, observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
if (id === null || id === undefined) {
throw new Error('Required parameter id was null or undefined when calling getKeyResult.');
}
let headers = this.defaultHeaders;
// authentication (apiKeyHeader) required
if (this.configuration.apiKeys && this.configuration.apiKeys["Ocp-Apim-Subscription-Key"]) {
headers = headers.set('Ocp-Apim-Subscription-Key', this.configuration.apiKeys["Ocp-Apim-Subscription-Key"]);
}
// authentication (apiKeyQuery) required
if (this.configuration.apiKeys && this.configuration.apiKeys["subscription-key"]) {
queryParameters = queryParameters.set('subscription-key', this.configuration.apiKeys["subscription-key"]);
^^^^^^^^^^^^^^^ error is here
}
4.1.0
OpenAPI template is generated from an Azure Api Management Gateway. This condition only occurs when an API is not associated with a Product, and thus is considered not published.
{
"swagger": "2.0",
"info": {
"title": "Swashbuckle",
"version": "1.0",
"description": "Swagger document by Swashbuckle"
},
"host": "example-api.net",
"basePath": "",
"schemes": [
"https"
],
"securityDefinitions": {
"apiKeyHeader": {
"type": "apiKey",
"name": "Ocp-Apim-Subscription-Key",
"in": "header"
},
"apiKeyQuery": {
"type": "apiKey",
"name": "subscription-key",
"in": "query"
}
},
"security": [
{
"apiKeyHeader": []
},
{
"apiKeyQuery": []
}
],
...
$libraryName="example-api"
$libraryVersion="0.0.1"
$angularVersion="7"
$ouputDir="./client"
$generatorVersion="4.1.0"
$swaggerFile="c:\development\example-api.json"
Write-Host "Download Swagger Doc from Azure API Management"
$ApiMgmtContext = New-AzApiManagementContext -ResourceGroupName "example-api" -ServiceName "example-api"
Export-AzApiManagementApi -Context $ApiMgmtContext `
-ApiId "example-api" `
-SpecificationFormat "Swagger" `
-SaveAs $swaggerFile
Write-Host "Download OpenAPI generator java library"
Invoke-WebRequest -OutFile openapi-generator-cli.jar "http://central.maven.org/maven2/org/openapitools/openapi-generator-cli/$generatorVersion/openapi-generator-cli-$generatorVersion.jar"
Write-Host "generate example-api angular client libraries ($libraryVersion)"
java -jar openapi-generator-cli.jar generate `
-i $swaggerFile `
-g typescript-angular `
--additional-properties ngVersion=$angularVersion,npmName=$libraryName,npmVersion=$libraryVersion `
-o "$ouputDir"
Generate Angular-Typescript client from a swagger doc that has a query parameter securityDefinition. Then do npm install and npm run build
https://github.com/OpenAPITools/openapi-generator/issues/1535
馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
@bruceharrison1984 thanks for reporting.
would you like to implement a fix for this? I can point you to the relevant files, it should be somewhere here:
https://github.com/OpenAPITools/openapi-generator/blob/4575b3074ab6f2628837ca84ed5c287f2afaceee/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache#L161-L168
We are encountering this exact same issue. We're using an OpenAPI 2.0 config being generated by Azure Api Manager as well. Obviously it's a showstopper because we can't publish our angular libraries with these generated services without going through and manually initializing the queryParameters for every call individually. Would greatly appreciate this being fixed!
If it's any help this variable seems to be initialized correctly on the first call of one of our services but the remainder of calls in the same file are being generated without the initialization.
As a workaround you could add an optional dummy query parameter
So I investigated this further on our end and I think you're correct. The issue is the queryParameters initialization is gated behind the hasQueryParams check but the generated code that handles apiKeyQuery also expects that variable to be there.
There are two solutions, I'm not sure which is preferred:
Add a check in hasQueryParams for isKeyInQuery and set that flag to true if it is present. I think this method is more accurate because a if we do allow a key in a query param, the call certainly needs to support query params regardless of if they're used directly by the endpoint logic itself.
or
Add a check in isKeyInQuery for hasQueryParams and if it doesn't, initialize the queryParams variable in addition to what it's doing now.
@macjohnny We unfortunately have too many endpoints to use this workaround. For now we are manually removing the section of the OpenApi definition that causes isKeyInQuery to set to true. We aren't using this feature, instead opting to provide the keys in the headers but there is seemingly no way to get Azure API Manager to not include that section of the definition.
@rhedges thanks for the analysis!
Would you be up to filing a PR to fix this issue?
I am unfortunately in a heavy crunch time for the next few weeks. I'd be willing to take a look at this after my deadline if the issue is still open. I am not incredibly familiar with mustache templates or java though so it may take bit and I may need someone with more experience to take a look and ensure I'm following best practices etc.
We are supplementing our generated services with the proper variable initialization manually in the interim.