2 releases
new 0.1.1 | Mar 8, 2025 |
---|---|
0.1.0 | Feb 27, 2025 |
#1279 in Parser implementations
142 downloads per month
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