Skip to content

Query parameters serialization is broken in generated API client #228

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

Closed
prog13 opened this issue Apr 6, 2021 · 2 comments
Closed

Query parameters serialization is broken in generated API client #228

prog13 opened this issue Apr 6, 2021 · 2 comments

Comments

@prog13
Copy link
Contributor

prog13 commented Apr 6, 2021

Hi team!

I believe that PR #223 has introduced a regression to the generated API query parameters serialization. The issue is in this template:

protected toQueryString(rawQuery?: QueryParamsType): string {
    const query = rawQuery || {};
    const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
    return keys
            .map((key) =>
                typeof query[key] === "object" && !Array.isArray(query[key])
                ? this.toQueryString(query[key] as QueryParamsType)
                : this.addArrayQueryParam(query, key),
            )
            .join("&");
}

Condition typeof query[key] === "object" && !Array.isArray(query[key]) incorrectly handles primitive types: if type of query[key] is, for example, number, it will be handled by the this.addArrayQueryParam method, which is incorrect, and will throw an exception further on this line:

private addArrayQueryParam(query: QueryParamsType, key: string) {
    const value = query[key];
    const encodedKey = encodeURIComponent(key);
    return `${value.map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`).join('&')}`;
}

Since type of value is number, obviously it's doesn't have .map method and the code will break.

@prog13
Copy link
Contributor Author

prog13 commented Apr 6, 2021

I've opened the MR #229, hope I've got it right 🤞

@js2me
Copy link
Member

js2me commented Apr 8, 2021

Hello @prog13 !
Thanks for your contribution, fix of this bug was added in 8.0.3

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

No branches or pull requests

2 participants