[Nodejs] Typescript-axios generated client not applying security schemes
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?
@mastersilv3r This should do the job for you!
const apiConfig: Configuration = {
basePath: Environment.rBaseUrl,
baseOptions: {
headers: {
'Authorization': `Bearer ${Environment.AuthToken}`,
},
},
};
I had a very similar issue. What @rey4eel suggested works for your case but the token in my case will expire after the timeout so I need accessToken: asyncTokenFcn. Long story short, for me to get the config to work, I just need to specify the security for my endpoint in my openapi yaml.
I had to go into the generated code - setBearerAuthToObject was generated but never called. Then check the template to see what condition the call to setBearerAuthToObject is generated. So finally I realize I should be able to specify security type for my endpoint. :P And now I finally understand the meaning of Defined By ToolingExtension for Authorizations in the doc.