Skip to content

Commit

Permalink
deserialize undefined to none
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Feb 7, 2024
1 parent 5b2a791 commit 039219b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/ain-ocean/src/api_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use axum::{
extract::FromRequestParts,
http::{request::Parts, StatusCode},
};
use serde::{de::DeserializeOwned, Deserialize};
use serde::{
de::{DeserializeOwned, Deserializer},
Deserialize,
};
use serde_with::{serde_as, DisplayFromStr};

use crate::error::ApiError;
Expand All @@ -18,9 +21,25 @@ pub struct PaginationQuery {
#[serde_as(as = "DisplayFromStr")]
#[serde(default = "default_pagination_size")]
pub size: usize,
#[serde(deserialize_with = "undefined_to_none")]
pub next: Option<String>,
}

fn undefined_to_none<'de, D>(d: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let v: Option<String> = Deserialize::deserialize(d)?;
match v {
Some(v) => if v.as_str() == "undefined" {
Ok(None)
} else {
Ok(Some(v))
},
None => Ok(None)
}
}

pub struct Query<T>(pub T);

#[async_trait]
Expand Down

0 comments on commit 039219b

Please sign in to comment.