-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(typescript-axios): use baseURL of (custom) axios instance #16125
Conversation
Code looks good; Question on the implementation: Wouldn't it make more sense to just get rid of the entire basePath construct and just set the right basePath in the That significantly reduces the number of different parameters being passed around. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
thanks for your contribution!
this makes sense. I merge this PR now, so maybe this can be done in a follow-up |
I agree. I think we should drop the external All of these are breaking changes though. |
This causes baseURL append twice if you have changed global default by
|
Please could you elaborate? I don't understand the problem. |
This change breaks the default behavior of using globalAxios
considering the following codes: import axios from "axios";
axios.defaults.baseURL = '/basic-api';
DefaultApiFactory()
.authIsEmailAvailablePost(email)
.then((response: boolean) => {
console.log(response);
})
.catch((e) => {
console.error(e);
});
const axiosRequestArgs = {...axiosArgs.options, url: (configuration?.basePath || axios.defaults.baseURL || basePath) + axiosArgs.url}; the second in axios itself return axios.request<T, R>(axiosRequestArgs); In your case, you might need to set {
"host": "localhost:3000",
"schemes": [
"http"
],
} Another solution is to allow
|
So that would both solve my problem without hurting the default behavior existing prior to this PR? |
Closes #11799
This PR is a minor change affecting
typescript-axios
.I tried to pass a custom axios instance but it wouldn't take into account its baseURL:
I checked in the generated code, the customAxios does have the baseURL and is properly loaded, but instead of
http://localhost:3000/auth/isEmailAvailable
, the URL called washttp://localhost/auth/isEmailAvailable
, becauseBASE_PATH
is loaded, which ishttp://localhost
, andcustomAxios.baseURL
is never read whatsover:https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-axios/common.mustache#L139
By default, axios global object has baseURL===undefined, so the patch I did should not affect anything if no custom axios instance is passed.
Note that with this patch, typescript-axios would also work as expected when no custom axios instance is passed but the global axios baseURL is modified.
I am not sure why
BASE_PATH
is called like this, it should beBASE_URL
. Similarly,Configuration.basePath
should beConfiguration.baseURL
.basePath
(as opposed tobaseURL
), as something like/v2
, is added here:https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache#L277
==>
request(axios, basePath)
That
basePath
comes from thebasePath
in param of the factory, but I think it should bebasePath || Configuration.basePath
instead. And we should add another option to Configuration which will beConfiguration.baseURL
- and use it incommon.mustache
like I did in this patch.@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04)
To me, this is a bug fix, that's not breaking (except for people relying on the bug...) - that's why I target master.
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
(6.3.0) (minor release - breaking changes with fallbacks),7.0.x
(breaking changes without fallbacks)