2 releases

new 0.1.1 Mar 8, 2025
0.1.0 Feb 27, 2025

#1279 in Parser implementations

Download history 142/week @ 2025-02-26

142 downloads per month

MIT/Apache

9KB

Implement poem FromRequest to deserialize struct from query string.

Example

use poem_queryext::QueryExt;
use poem_openapi::{payload::PlainText, OpenApi};
use serde::Deserialize;

struct Api;

#[OpenApi]
impl Api {
  //test url: /test?name=cx  
  //test url: /test?name=cx&age=18&hobby[0]=music&hobby[1]=game  
  #[oai(path = "/test", method = "get")]
  async fn test(&self, query: QueryExt<'_, QueryObj>) -> PlainText<String> {
       let obj = query.0;
       PlainText(format!(
           "name:{},age:{},hobby:{}",
           obj.name,
           obj.age.unwrap_or_default(),
           obj.hobby.unwrap_or_default().join(",")
       ))
   }
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct QueryObj{
    name:String,
    age:Option<i8>,//Non mandatory fields  use Option<T>
    hobby:Option<Vec<String>>//Non mandatory fields  use Option<T>
}


lib.rs:

Implement poem FromRequest to deserialize struct from query string.

Example

use poem_queryext::QueryExt;
use poem_openapi::{payload::PlainText, OpenApi};
use serde::Deserialize;

struct Api;

#[OpenApi]
impl Api {
  //test url: /test?name=cx  
  //test url: /test?name=cx&age=18&hobby[0]=music&hobby[1]=game  
  #[oai(path = "/test", method = "get")]
  async fn test(&self, query: QueryExt<'_, QueryObj>) -> PlainText<String> {
       let obj = query.0;
       PlainText(format!(
           "name:{},age:{},hobby:{}",
           obj.name,
           obj.age.unwrap_or_default(),
           obj.hobby.unwrap_or_default().join(",")
       ))
   }
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct QueryObj{
    name:String,
    age:Option<i8>,//Non mandatory fields  use Option<T>
    hobby:Option<Vec<String>>//Non mandatory fields  use Option<T>
}

Dependencies

~14–25MB
~398K SLoC