OpenAPITools / OpenAPITools/openapi-generator
[REQ][Rust] Encode parameters in URL with less allocation
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.
The parameters to each API call are passed into the client function as type &str but are then converted to String by the URL encoding call using the url::form_urlencoded crate. These newly allocated Strings are then passed to the format! macro to create the API URL.
pub async fn list_project_types(
configuration: &configuration::Configuration, project: &str
) -> Result<crate::models::ProjectTypesListResponse, Error<ListProjectTypesError>> {
...
let local_var_uri_str = format!(
"{}/project/{project}/types",
local_var_configuration.base_path,
project=crate::apis::urlencode(project) // (&str) -> String
);
...
This requires n+1 allocations for n parameters.
Describe the solution you'd like
The form_urlencoded crate depends on the percent-encoding crate. Using the percent-encoding crate directly for the generated urlencode function allows parameters to be returned as type Cow, avoiding allocation for parameters that do not require any percent encoding.
The existing function
pub fn urlencode<T: AsRef<str>>(s: T) -> String {
::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
}
can be replaced with:
pub fn urlencode<T: AsRef<str>>(s: T) -> Cow<str> {
::percent_encoding::utf8_percent_encode(s.as_ref(), &AN_ASCII_SET).into()
}
Note: I've skipped the lifetime annotations and definition of AN_ASCII_SET that would be required for a PR.
Describe alternatives you've considered
We could continue to use form_urlencoded but replace the collect with code duplicated from percent-encoding to generate the Cow from an iterator.
Additional context
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 at the generated Rust client's urlencode function and inspect how the Rust generator declares and uses URL-encoding dependencies. Replace the allocation-heavy encoding path with the requested Cow<str> approach, then verify that generated client URLs remain correctly encoded and avoid allocation when parameters need no percent encoding.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100