Is the Optional extractor no longer compatible with Common extractors? #3136
-
SummaryI previously wrote the following code according to the documentation of version 0.7.9 use axum::{
extract::{Path, Query},
routing::get,
Router,
};
use uuid::Uuid;
use serde::Deserialize;
let app = Router::new().route("/users/:id/things", get(get_user_things));
#[derive(Deserialize)]
struct Pagination {
page: usize,
per_page: usize,
}
impl Default for Pagination {
fn default() -> Self {
Self { page: 1, per_page: 30 }
}
}
async fn get_user_things(
Path(user_id): Path<Uuid>,
pagination: Option<Query<Pagination>>,
) {
let Query(pagination) = pagination.unwrap_or_default();
// ...
} However, I found that this syntax seems to no longer be supported in version 0.8. How should I write the code to achieve the functionality of the above code? axum version0.8.1 |
Beta Was this translation helpful? Give feedback.
Answered by
Turbo87
Jan 2, 2025
Replies: 1 comment 2 replies
-
#[derive(Deserialize)]
struct Pagination {
#[serde(default = "1")]
page: usize,
#[serde(default = "30")]
per_page: usize,
}
async fn get_user_things(
Path(user_id): Path<Uuid>,
Query(pagination): Query<Pagination>,
) {
// ...
} something like this is roughly what you want. alternatively, you can use |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
baby195lxl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
something like this is roughly what you want. alternatively, you can use
Option<usize>
inside the struct and remove the#[serde(default)]
annotations.