oxidecomputer / oxidecomputer/progenitor
Compilation failure for header values that are arrays
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 1k
- Forks
- 136
- Avg merge
- 8h 36m
- Merged PRs (30d)
- 14
Description
Given this snippet from an OpenAPI spec:
"/metrics/": {
"get": {
"summary": "Handle Metrics",
"operationId": "handle_metrics_metrics__get",
"parameters": [
{
"name": "accept",
"in": "header",
"required": false,
"schema": {
"type": "array",
"items": {
"type": "string"
},
"title": "Accept"
}
}
],
"responses": {
"200": {
"description": "Service Status",
"content": {
"application/json": {
"schema": {
"type": "string",
"title": "Response 200 Handle Metrics Metrics Get"
}
}
}
},
"422": {
"description": "Unprocessable Entity",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
}
}
}
}
You'll generate the following code:
pub async fn handle_metrics_metrics_get<'a>(
&'a self,
accept: Option<&'a Vec<String>>,
) -> Result<ResponseValue<String>, Error<types::ErrorResponse>> {
let url = format!("{}/metrics/", self.baseurl,);
let mut header_map = HeaderMap::with_capacity(1usize);
if let Some(v) = accept {
header_map.append("accept", HeaderValue::try_from(v.first().unwrap())?);
}
#[allow(unused_mut)]
let mut request = self
.client
.get(url)
.header(
reqwest::header::ACCEPT,
reqwest::header::HeaderValue::from_static("application/json"),
)
.headers(header_map)
.build()?;
let result = self.client.execute(request).await;
let response = result?;
match response.status().as_u16() {
200u16 => ResponseValue::from_response(response).await,
422u16 => Err(Error::ErrorResponse(
ResponseValue::from_response(response).await?,
)),
_ => Err(Error::UnexpectedResponse(response)),
}
}
This fails to compile:
error[E0277]: the trait bound `HeaderValue: From<&Vec<std::string::String>>` is not satisfied
--> catalog-api-v1/src/client.rs:2635:41
|
2635 | header_map.append("accept", HeaderValue::try_from(v)?);
| ^^^^^^^^^^^ the trait `From<&Vec<std::string::String>>` is not implemented for `HeaderValue`, which is required by `HeaderValue: TryFrom<&Vec<std::string::String>>`
|
= help: the following other types implement trait `From<T>`:
<HeaderValue as From<isize>>
<HeaderValue as From<i16>>
<HeaderValue as From<i32>>
<HeaderValue as From<i64>>
<HeaderValue as From<usize>>
<HeaderValue as From<u16>>
<HeaderValue as From<u32>>
<HeaderValue as From<u64>>
and 2 others
= note: required for `&Vec<std::string::String>` to implement `Into<HeaderValue>`
= note: required for `HeaderValue` to implement `TryFrom<&Vec<std::string::String>>`
In short, all that's necessary for this to compile is for the generated code to loop over the values in the Vec and append them individually rather than appending the entire Vec. Now, detecting that you have a Vec and special casing it may be another story that I'll leave as an exercise to the readerimplementor.
Contributor guide
No contributing guide indexed for this repository
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 by tracing the generator code that produces the header handling shown in catalog-api-v1/src/client.rs, using the OpenAPI snippet in the issue as the reproduction case. Generate the Rust client and verify that array-valued headers are appended individually and that the resulting code compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, rust
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100