swagger-api / swagger-api/swagger-codegen
[Nodejs] Typescript-axios generated client not applying security schemes
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
The generated client is not setting the access token in the header to the API calls, as per the security scheme mentioned in the Swagger spec. Even though I am explicitly passing the accessToken in the Configuration to the factory method.
My Code
import {PetApiFactory} from './api';
import {Configuration} from './configuration';
const c = new Configuration({
"accessToken": "",
"basePath": "https://petstore3.swagger.io/api/v3"
})
PetApiFactory(c).getPetById(207)
.then(console.log)
.catch(console.error);
Swagger-codegen version
7.2.0
Swagger declaration file content or url
Relevant Swagger part
"/pet/{petId}": {
"get": {
"tags": [
"pet"
],
"summary": "Find pet by ID",
"description": "Returns a single pet",
"operationId": "getPetById",
"security": [
{
"api_key": []
},
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
]
},
Gist
Gist of Full Swagger File can be found here
Command line used for generation
java -jar openapi-generator-cli.jar generate -i swagger.json -o pet-store-client -g typescript-axios
Steps to reproduce
- Download the json file from the gist or use standard swagger 3.0 of pet store found at https://petstore3.swagger.io/
- Generate the code via command shared above
-
cd pet-store-client//Go to the generated folder -
touch main.ts//Create a ts file - Copy the code I have shared above in the ts file
- Build and run the file. You will see in the console.log that the request headers do not have any header called
accessTokenset in them.
headers: Object [AxiosHeaders] {
Accept: 'application/json, text/plain, */*',
'Content-Type': undefined,
'User-Agent': 'axios/1.6.5',
'Accept-Encoding': 'gzip, compress, deflate, br'
},
I am getting a successful response
data: {
id: 207,
category: { id: 207, name: 'Dogs' },
name: 'doggie',
photoUrls: [ 'string' ],
tags: [ [Object] ],
status: 'available'
}
- This is the issue. There should be
accessTokenheader set in this request because I am configuring it. I have searched the full log but did not find it anywhere.
Or, have I got it all wrong and am not using it properly?
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
Regenerate the pet-store client with the shown typescript-axios command and inspect the generated Configuration, PetApiFactory, and request code. Start with main.ts and the generated output, then compare the Swagger security schemes with the request headers; done means the documented configuration behavior is either corrected or clearly verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100