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

Compilation failure for header values that are arrays #822

Open
zmitchell opened this issue Jun 4, 2024 · 0 comments
Open

Compilation failure for header values that are arrays #822

zmitchell opened this issue Jun 4, 2024 · 0 comments

Comments

@zmitchell
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant