Skip to content
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

Incorrect handling of query parameters with values 0 or empty strings in CodeGeneration output #4781

Open
yukiyokotani opened this issue Dec 19, 2024 · 0 comments · May be fixed by #4782
Open

Comments

@yukiyokotani
Copy link

yukiyokotani commented Dec 19, 2024

In RTK Query's CodeGeneration, there is an issue with the generated code when encodeQueryParams is set to true. The current generated code outputs the following logic:

queryArg.status
  ? encodeURIComponent(String(queryArg.status))
  : undefined,

With this logic, if queryArg.status is '' or 0, it returns undefined, which results in the status query parameter being omitted from the request URL. However, I believe it is more natural for the values '' and 0 to be represented as ?status= and ?status=0 in query parameters. In fact, the behavior of URLSearchParams demonstrates this as follows:

const paramsObj = { foo: "", bar: 0 };
const searchParams = new URLSearchParams(paramsObj);
console.log(searchParams.toString()); // "foo=&bar=0"

Therefore, in the generated code from the CodeGenerator, the logic should be correctly implemented as follows:

queryArg.status != null
  ? encodeURIComponent(String(queryArg.status))
  : undefined,

This code ensures that only undefined and null result in queryArg.status being undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant