swagger-api/swagger-codegen
View on GitHub[TypeScript-Fetch] Invalid interface definition when using securityDefinitions
Open
#4,895 opened on Mar 2, 2017
Client: TypeScriptIssue: Bughelp wanted
Description
Description
Compiling the following spec into a TypeScript-Fetch client results in invalid code.
Specifically, the generated api.ts file contains
// authentication (mykey) required
if (configuration.apiKey && configuration.apiKey.x-api-key) {
fetchOptions.headers = assign({
"x-api-key": configuration.apiKey.x-api-key,
}, contentTypeHeader);
}
and configuration.ts contains
apiKey: {
x-api-key: string;
};
Swagger-codegen version
Tested on 2.2.2 and on 2.3.0 (7aebcfa).
Swagger declaration file content or url
swagger: "2.0"
info:
version: "1.0.0"
title: Test
host: localhost:8080
basePath: /
securityDefinitions:
mykey:
type: apiKey
in: header
name: x-api-key
security:
- mykey: []
paths:
/test:
get:
responses:
"200":
description: Success
schema:
type: string
default:
description: Error
schema:
type: string
Command line used for generation
java -jar ../../swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ../my/test.yaml -l typescript-fetch
Steps to reproduce
Generate the TypeScript-Fetch definition for a Swagger spec that contains securityDefinitions with the - character in the key name.
Related issues
The lack of an implementation for security definitions was reported in #3970.
Suggest a Fix
Wrapping the identifiers in " and performing field accesses through the [] syntax as follows seems to fix the issue.
// authentication (mykey) required
if (configuration.apiKey && configuration.apiKey["x-api-key"]) {
fetchOptions.headers = assign({
"x-api-key": configuration.apiKey["x-api-key"],
}, contentTypeHeader);
}
...
apiKey: {
"x-api-key": string;
};