Skip to content

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

Open
@yukiyokotani

Description

@yukiyokotani

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions