acacode / acacode/swagger-typescript-api

Generated HTTP client adds extra ampersands to the request URL for each empty array passed in query

Đang mở
#621 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
4.1k
Fork
436
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

The generated HTTP client adds extra ampersands to the request URL for each empty array passed in query.

I noticed this with my API that does a database query with a lot of possible filters where many of them are arrays. If I pass most of my filter options as empty arrays the request URL will contain parts like `...&&&&&&&&SomeFilter=...`.

This seems to be because of the logic in the [toQueryString](https://github.com/prog13/swagger-typescript-api/blob/167523600c57cfc3c6be7e933c5c4ec08e5d6c97/templates/base/http-clients/fetch-http-client.eta#L87) method. The `query` may contain empty arrays that are not filtered out so they will be set in `keys` which then maps an empty array to an empty string (via `addArrayQueryParam`) and then joins the result with `&` which causes the issue.

To solve this you could either filter out the empty arrays already when setting `keys` (same place where it's now filtering out undefined values) or you could process the joined string to filter out extra consecutive ampersands and a trailing ampersand with for example this `.replaceAll(/&&+/g, '&').replace(/&$/, '')`. I think it's cleaner to filter out the empty arrays earlier though.

I might have some time to submit a PR for this.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.