paperclip icon indicating copy to clipboard operation
paperclip copied to clipboard

HashMap as string type in QsQuery

Open omid opened this issue 4 years ago • 5 comments

I have a HashMap which will be deserialized with QsQuery. So the input is a string. But based on paperclip, the type is Object.

Is there any way to override this? Or implement TypedData for my HashMap?

omid avatar Nov 23 '20 09:11 omid

I did this with introducing a new struct.

It was similar to:

#[derive(Deserialize, Apiv2Schema)]
pub struct MainType {
    pub something: Option<HashMap<String, String>>,
}

Now it's like:

#[derive(Deserialize)]
pub struct MyNewType {
    #[serde(flatten)]
    pub inner: HashMap<String, String>
}

impl TypedData for MyNewType {
    fn data_type() -> DataType {
        DataType::String
    }
}

#[derive(Deserialize, Apiv2Schema)]
pub struct MainType {
    pub something: Option<MyNewType>,
}

Would be cool if you know a better/simpler/standard way and mention it here.

omid avatar Nov 23 '20 10:11 omid

So I think there is no better way :D Going to close it :)

omid avatar Dec 05 '20 20:12 omid

Let's keep it open so that we can track that there's an issue with this usage.

wafflespeanut avatar Dec 06 '20 06:12 wafflespeanut

Hi @omid, I did some work yesterday to improve support for QsQuery with some specific handling which I think will make an improvement for your usage. I'm finding it quite difficult with qs to figure out if this works generally as qs doesn't really fit in with openapi very well. It would be good to hear if this improves things for you. https://github.com/wafflespeanut/paperclip/pull/242/files

platy avatar Dec 08 '20 08:12 platy

Hey @platy, thanks. I couldn't make it work with:

#[derive(Deserialize, Apiv2Schema)]
pub struct QueryParams {
    pub limit: i32,
    pub filter: Option<HashMap<String, Vec<String>>>,
}

nor this:

#[derive(Deserialize, Apiv2Schema)]
pub struct QueryParams {
    pub limit: i32,
    pub filter: Option<HashMap<String, String>>,
}

It doesn't show up any error, but also this field is not included in the OpenAPI output. The limit field does exist, but the filter doesn't!

omid avatar Dec 08 '20 14:12 omid