poem icon indicating copy to clipboard operation
poem copied to clipboard

Using HashMap with poem_openapi::param::Query

Open hverlin opened this issue 1 year ago • 2 comments

Hi, thanks for this library!

I am running into an issue. I would like to use a HashMap with poem_openapi::param::Query. Here is a small example:

#[OpenApi(prefix_path = "/v1")]
impl Api {
    #[oai(path = "/my_endpoint", method = "get")]
    async fn my_endpoint(
        &self,
        query: poem_openapi::param::Query<HashMap<String, String>>,
    ) -> PlainText<String> {
        let maybeQueryValue = query.0.get("prop1");
        PlainText(
            maybeQueryValue
                .unwrap_or(&"missing".to_string())
                .to_string(),
        )
    }
}

One could, for example, call the endpoint like this:

/v1/my_endpoint?prop1=test&prop2=test2&additionalProp3=string

The expected swagger spec would look like this:

        "parameters": [
          {
            "name": "query",
            "in": "query",
            "required": true,
            "schema": {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              }
            }
          }
        ],

Unfortunately, it appears that it does not work, and I get the following error:

the trait `ApiExtractor<'_>` is not implemented for `poem_openapi::param::Query<HashMap<std::string::String, std::string::String>>`

A workaround to get the application to work is to use poem::web::Query instead. However, then it does not show up in the documentation. Thanks!

hverlin avatar Sep 13 '22 19:09 hverlin

This is not currently supported, I will add it later.

sunli829 avatar Sep 15 '22 02:09 sunli829

Would be great to have this...

TheCataliasTNT2k avatar Dec 19 '22 22:12 TheCataliasTNT2k