OpenAPITools / OpenAPITools/openapi-generator
[REQ] [typescript-fetch] Manual ordering of query params
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
In a project, we're using an API which supports ordering returned items using query params. If you want to order on two different properties (like first on name, then on ID), you can do so by passing multiple ordering params in the desired order.
From the docs of our API framework:
Given that the collection endpoint is
/offers, you can filter offers by name in ascending order and then by ID in descending order with the following query:/offers?order[name]=desc&order[id]=asc.
In this situation, the order of the query params matter:
| Request | Ordering |
|---|---|
/offers?order[name]=desc&order[id]=asc |
name DESC, id ASC |
/offers?order[id]=asc&order[name]=desc |
id ASC, name DESC |
However, the typescript-fetch API client does not support ordering query params. The query param string is constructed based on some fixed order of the params. The order in which we pass the params to the request method, does not make any difference.
As such, we cannot express in which order we want to perform the ordering in our API/
Describe the solution you'd like
It would be nice if the order of the query params was respected, based on how we ordered them in the object passed to the request method:
OfferApi.getOfferCollection({ orderName: "desc", orderId: "asc" })
// would call: /offers?order[name]=desc&order[id]=asc
OfferApi.getOfferCollection({ orderId: "asc", orderName: "desc" })
// would call: /offers?order[id]=asc&order[name]=desc
Describe alternatives you've considered
We created a proof-of-concept using a modified queryParamsStringify method, that would allow us to customise the order of the query params for the next API request. It required a global variable to store the desired order, with no way of attaching it to the specific request we were making. As such, we concluded that this approach was too hacky to use.
Additional context
The generated typescript-fetch API class/method for the above GET /offers endpoint looks like this (simplified example):
export class OrderApi extends runtime.BaseAPI {
async getOrderCollectionRaw(
requestParameters: GetOrderCollectionRequest
): Promise<runtime.ApiResponse<Array<OrderOrderGet>>> {
const queryParameters: any = {};
if (requestParameters.orderName !== undefined) {
queryParameters["order[name]"] = requestParameters.orderName;
}
if (requestParameters.orderId !== undefined) {
queryParameters["order[id]"] = requestParameters.orderId;
}
const response = await this.request({
path: `/orders`,
method: "GET",
query: queryParameters,
});
return new runtime.JSONApiResponse(response, (jsonValue) =>
jsonValue.map(OrderOrderGetFromJSON)
);
}
}
As can be seen, a new queryParameters object gets constructed, and any valid param from requestParameters gets created in this method. These params are added in a specific order, probably the order in which the query params are defined in the OpenAPI schema. In this case, first order[name] gets added, and then order[id]. As such, any custom order used in the requestParameters object gets lost, and there is no way to get order[id] before order[name] in the query param string.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the typescript-fetch generator code that builds the generated API method and the runtime queryParamsStringify method mentioned in the issue. Trace how requestParameters become queryParameters and then a query string, and check existing tests for query serialization. Done means callers can control the ordering of repeated ordering parameters per request without global state, with coverage for both example input orders.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100