-
SummaryI've been trying to add a query parameter that can be either of the two provided types (in my case - u64 or String) #[derive(PartialEq,Eq,serde::Serialize,serde::Deserialize,Debug)]
#[serde(untagged)]
pub enum IdOrName {
Id(u64),Name(String),
}This approach works for normal JSON, however, when trying to test this with query parameters, the value is always deserialized as a string. For example, the following code #[derive(PartialEq, Eq, serde::Deserialize, Debug)]
struct TestQuery {
foo: IdOrName,
bar: u64,
}
#[test]
fn test_query_query_de() {
let uri: Uri = "http://example.com/path?foo=1&bar=222".parse().unwrap();
let result: Query<TestQuery> = Query::try_from_uri(&uri).unwrap();
assert_eq!(
result.0,
TestQuery {
foo: IdOrName::Id(1),
bar: 222,
}
);
}yields the following test failure assertion Is there a good way to get around this? Or do I have to implement manual deserialization for the enum? axum version0.8.4 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
You have to implement |
Beta Was this translation helpful? Give feedback.
You have to implement
Deserializemanually for this. You'll probably want to callString::deserializefirst, then attempt to parse that into an integer and if the parsing fails, return the string variant.