在 actix-web 中用作查询参数时,如何将无效的枚举变体转换为 None
How do I turn invalid enum variants into None when used as query parameters in actix-web
使用 documentation 中为 actix_web::web::Query
提供的示例,当提供未知变体时,如何使 response_type
求助于 None
?
如果我有以下情况:
use actix_web::{web, App, HttpServer};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub enum ResponseType {
Token,
Code,
}
#[derive(Deserialize)]
pub struct AuthRequest {
id: u64,
response_type: Option<ResponseType>,
}
async fn index(web::Query(info): web::Query<AuthRequest>) -> String {
format!(
"Authorization request for client with id={} and type={:?}!",
info.id, info.response_type
)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
然后我访问 http://localhost:8080/?id=1&response_type=foo
,我得到了这个 400 响应:
Query deserialize error: unknown variant foo
, expected Token
or Code
当我希望它只接受 Enum 的值作为有效值时,如果没有提供值或提供无效值,我希望将其设置为 None
.
这个可以处理deserialize_with。
use actix_web::{web, App, HttpServer};
use serde::Deserialize;
use serde::de::{Deserializer};
#[derive(Debug, Deserialize)]
pub enum ResponseType {
Token,
Code,
}
fn from_response_type<'de, D>(deserializer: D) -> Result<Option<ResponseType>, D::Error>
where
D: Deserializer<'de>,
{
let res: Option<ResponseType> = Deserialize::deserialize(deserializer).unwrap_or(None);
Ok(res)
}
#[derive(Debug, Deserialize)]
pub struct AuthRequest {
id: u64,
#[serde(deserialize_with = "from_response_type")]
response_type: Option<ResponseType>,
}
async fn index(web::Query(info): web::Query<AuthRequest>) -> String {
format!(
"Authorization request for client with id={} and type={:?}!",
info.id, info.response_type
)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
任何无效值都被视为 None
。关键是
let res: Option<ResponseType> = Deserialize::deserialize(deserializer).unwrap_or(None);
使用 documentation 中为 actix_web::web::Query
提供的示例,当提供未知变体时,如何使 response_type
求助于 None
?
如果我有以下情况:
use actix_web::{web, App, HttpServer};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub enum ResponseType {
Token,
Code,
}
#[derive(Deserialize)]
pub struct AuthRequest {
id: u64,
response_type: Option<ResponseType>,
}
async fn index(web::Query(info): web::Query<AuthRequest>) -> String {
format!(
"Authorization request for client with id={} and type={:?}!",
info.id, info.response_type
)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
然后我访问 http://localhost:8080/?id=1&response_type=foo
,我得到了这个 400 响应:
Query deserialize error: unknown variant
foo
, expectedToken
orCode
当我希望它只接受 Enum 的值作为有效值时,如果没有提供值或提供无效值,我希望将其设置为 None
.
这个可以处理deserialize_with。
use actix_web::{web, App, HttpServer};
use serde::Deserialize;
use serde::de::{Deserializer};
#[derive(Debug, Deserialize)]
pub enum ResponseType {
Token,
Code,
}
fn from_response_type<'de, D>(deserializer: D) -> Result<Option<ResponseType>, D::Error>
where
D: Deserializer<'de>,
{
let res: Option<ResponseType> = Deserialize::deserialize(deserializer).unwrap_or(None);
Ok(res)
}
#[derive(Debug, Deserialize)]
pub struct AuthRequest {
id: u64,
#[serde(deserialize_with = "from_response_type")]
response_type: Option<ResponseType>,
}
async fn index(web::Query(info): web::Query<AuthRequest>) -> String {
format!(
"Authorization request for client with id={} and type={:?}!",
info.id, info.response_type
)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
任何无效值都被视为 None
。关键是
let res: Option<ResponseType> = Deserialize::deserialize(deserializer).unwrap_or(None);