'Override OpenAPI Generator BasePath on Generation
I am creating a TypeScript Axios REST API client with OpenAPI Generator. I'm doing this in my terminal using npx @openapitools/openapi-generator-cli generate -i ../swagger.json -g typescript-axios -o ./rest/.
Since my host is depending on my .env variables, I want to read my API_URL from my env variables and pass it to the generating process of the generator.
I found in the docs that changing the BasePath is in some way possible, but I can't figure out the way to do it. Adding --basePath=1.2.3.4:8080 does not work, and I don't understand what "Defined by ToolingExtension" means.
Solution 1:[1]
In the latest openapi-generator-cli 5.4.0 the typescript-axios generator respects the servers setting and uses the first server object in the list as the basePath - i.e. if you specify
{
"openapi": "3.0.2",
"info": { ... },
"paths": { ... },
"components": { ... },
"servers": [ { "url" : "https://testurl.com" } ]
}
then the generated base.ts file should contain
export const BASE_PATH = "https://testurl.com".replace(/\/+$/, "");
Solution 2:[2]
Unfortunately the "localhost" is hardcoded and as far as I can tell (after digging into the source code for the OpenAPI Generator) there is no way to change it. The documentation that mentions it is, I believe, referring to to the fact that the client that gets outputted allows that value to be overridden.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gregsmi |
| Solution 2 | Robert Hafner |
