swagger-api / swagger-api/swagger-codegen
[TypeScript-Fetch] Invalid interface definition when using securityDefinitions
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
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;
};
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the provided Swagger YAML and the typescript-fetch generation command. Inspect the generated api.ts and configuration.ts files for the invalid x-api-key identifiers; done means the generated client is valid TypeScript and preserves the intended API-key authentication behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100