-
-
Notifications
You must be signed in to change notification settings - Fork 416
Path Parameters Are Not Encoded #679
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
Comments
@lew I don't think it's a good idea to encode every parameter automatically. Would be easier if you handle it with a mutator no? |
I believe it is a good idea not to allow the function's user to provide input that breaks the execution. The RFC 3986 explicitly mentions when to encode the URI:
In my opinion, this is within orval's code in lines such as return axios.get(`/test/${parameter}`, options) What are the downsides to encoding in this place? Side note: import querystring from 'querystring'
const qs = querystring.stringify({ param1: 'some&value', param2: 'some other value' })
console.log(qs)
// param1=some%26value¶m2=some%20other%20value |
Some related docs and issues:
|
We could add it conditionally with an option in the orval config if it's ok for you |
I definitely agree that it would be a useful feature to have. |
We used to generate API with openapi-generator-cli using const localVarPath = `/api/v1/search/{searchTerm}`.replace(`{${"searchTerm"}}`, encodeURIComponent(String(searchTerm))); Now with orval we got a bug when user tried to search for URL. Would be great to have this option. Here's my patch: diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts
index ab7cff8..74a7efc 100644
--- a/packages/query/src/index.ts
+++ b/packages/query/src/index.ts
@@ -37,6 +37,7 @@ import {
vueMakeRouteReactive,
vueWrapTypeWithMaybeRef,
isVue,
+ makeRouteSafe,
} from './utils';
const AXIOS_DEPENDENCIES: GeneratorDependency[] = [
@@ -234,13 +235,10 @@ const VUE_QUERY_V4_DEPENDENCIES: GeneratorDependency[] = [
exports: [
{ name: 'unref', values: true },
{ name: 'computed', values: true },
+ { name: 'MaybeRef' },
],
dependency: 'vue',
},
- {
- exports: [{ name: 'MaybeRef' }],
- dependency: '@tanstack/vue-query/build/lib/types',
- },
];
const isVueQueryV3 = (packageJson: PackageJson | undefined) => {
@@ -298,9 +296,9 @@ const generateQueryRequestFunction = (
outputClient: OutputClient | OutputClientFunc,
) => {
// Vue - Unwrap path params
- const route: string = isVue(outputClient)
- ? vueMakeRouteReactive(_route)
- : _route;
+ const route: string = makeRouteSafe(
+ isVue(outputClient) ? vueMakeRouteReactive(_route) : _route,
+ );
const isRequestOptions = override.requestOptions !== false;
const isFormData = override.formData !== false;
const isFormUrlEncoded = override.formUrlEncoded !== false;
diff --git a/packages/query/src/utils.ts b/packages/query/src/utils.ts
index 51e5e9a..fe35a5a 100644
--- a/packages/query/src/utils.ts
+++ b/packages/query/src/utils.ts
@@ -98,9 +98,19 @@ export function vueWrapTypeWithMaybeRef(input: string): string {
return output;
}
+const wrapRouteParameters = (
+ route: string,
+ prepend: string,
+ append: string,
+): string =>
+ (route ?? '').replaceAll(/\${(.+?)}/g, `\${${prepend}$1${append}}`);
+
// Vue persist reactivity
export const vueMakeRouteReactive = (route: string): string =>
- (route ?? '').replaceAll(/\${(\w+)}/g, '${unref($1)}');
+ wrapRouteParameters(route, 'unref(', ')');
+
+export const makeRouteSafe = (route: string): string =>
+ wrapRouteParameters(route, 'encodeURIComponent(String(', '))');
export const isVue = (client: OutputClient | OutputClientFunc) =>
OutputClient.VUE_QUERY === client; |
(cherry picked from commit 63d7777)
(cherry picked from commit 1c8a2ed)
What are the steps to reproduce this issue?
What happens?
The parameter
parameter
is just string concatenated into the URL:As a result, requests fail.
What were you expecting to happen?
Strings in the path fragment must be encoded
Any other comments?
URL encoding should take place here because neither the rest of the app should care about encoding nor is it possible to later encode a fully constructed URL.
A programmer would typically prefer to use a method like this to avoid having to worry about internals:
This bug happens at different places in the generated code.
What versions are you using?
The text was updated successfully, but these errors were encountered: