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

Optional query is assigned "undefined" string #268

Closed
rojiwon123 opened this issue Mar 6, 2023 · 5 comments
Closed

Optional query is assigned "undefined" string #268

rojiwon123 opened this issue Mar 6, 2023 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@rojiwon123
Copy link
Contributor

findMany api has a optional query 'page'.
but if page is undefined, url is written as "/products?page=undefined". and this query value is string "undefined".

i think, "/products?" or "/products" is right.
(so i think, undefined type should be handled separately.)

In Korea,

findMany api를 구현했습니다. page는 optional한 쿼리입니다. sdk를 통해 findMany(connection)를 실행하면 undefined가 문자열 "undefined"로 전달되는 버그가 있습니다. ( ex) "/products?page=undefined" )
이는 URLSearchParams의 처리 방식 때문인 것 같습니다. 제 생각에는 undefined값은 키조차 UrlSearchParams 객체 생성 인자로 전달되지 않아야 할 것 같습니다. ( ex) "/products?", "/products" )

// This is nestia sdk.
export namespace findMany
{
    export type Output = PaginatedResponse<ProductSchema.General>;

    export const METHOD = "GET" as const;
    export const PATH: string = "/products";
    export const ENCRYPTED: Fetcher.IEncrypted = {
        request: false,
        response: false,
    };

    export function path(page: number | undefined): string
    {
        return `/products?${new URLSearchParams(
        {
            page
        } as any).toString()}`;
    }
}
@samchon samchon self-assigned this Mar 6, 2023
@samchon samchon added the bug Something isn't working label Mar 6, 2023
@samchon
Copy link
Owner

samchon commented Mar 6, 2023

Upgrade to @nestia/sdk@1.0.11, then would be fixed.

@samchon samchon closed this as completed in f20e670 Mar 6, 2023
samchon added a commit that referenced this issue Mar 6, 2023
Fix #268 - when optional query param comes
@rojiwon123
Copy link
Contributor Author

now @nestia/sdk 1.0.12, sdk path function logic is changed, but same bug still happens.

@samchon
Copy link
Owner

samchon commented Mar 7, 2023

@industriously Show me your code. It must be a full project containing nestia.config.ts

@rojiwon123
Copy link
Contributor Author

rojiwon123 commented Mar 7, 2023

branch: product

URLSearchParams 생성자의 인자는 undefined일 수 있는 값을 허용하지 않습니다. (ex page?:string, page: string | undefined )
그리고 실제로 undefined값을 할당하면, "?page=undefined"와 같은 형태로 나옵니다.
그래서 제 생각에는 런타임에서 값이 undefined인 키는 제거된 후 인자로 전달되어야 할 것 같습니다.
저는 쿼리를 키마다 각각 전달하지만 nestia.TypedQuery 처럼 전체 object타입으로 사용해도 마찬가지로 undefined인 값은 키까지 필터링되어야 할 것 같습니다.
( 일반적으로 optional한 키이고 undefined를 전달할 때, 키 자체를 추가하지 않은 객체를 전달하지만, 키를 생성하고 undefined를 전달해도 typecheck에서 통과합니다. 그리고 이 경우에는 undefined 문자열이 쿼리에 추가될 것입니다. - 정확히 이 상황을 테스트하진 못했습니다.)

new URLSearchParams() constructor not allow Record<string, undefined> type.
so if i write new URLSearchParams({ page: PageType }) // type PageType = string | undefined,
type error is throwed.
and a query string ( that made by call toString() at URLSearchParams instance ) is "?page=undefined".
so i think in runtime, if optional data is really undefined, object key ( that parameter of URLSearchParams constructor ) should be deleted.

PS. 혹시나 저 혼자만의 어이없는 실수로 인한 이슈라면 정말 죄송합니다. ㅜㅜ nestia up!!

@samchon
Copy link
Owner

samchon commented Mar 7, 2023

Thanks for detailed reporting.

I haven't known about the URLSearchParams, as I've not used query parameters at all.

Now, update to @nestia/sdk@1.0.13, then everything would be fine.

samchon added a commit that referenced this issue Mar 7, 2023
Fix #268 - filter out  `undefined` values from `URLSearchParams`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants