Accessing a third party API from Nodejs/Axios using swagger-codegen generated client

35 views Asked by At
Description

The generated client from swagger codegen 7.2.0 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": [
                            "write:pets",
                            "read:pets"]
                    },
                    {
                        "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

How to reproduce

If run the main.ts file, you will see in the console.log that the request headers do not have any header called accessToken set 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'
    },

Problem

There should be accessToken header 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?

0

There are 0 answers