-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how do I set a default value for a parameter? #70
Comments
Yes you can. Just add something like this. pub async fn get_customers(
db: &State<Database>,
limit: i64,
page: Option<i64>,
) -> Result<Json<Vec<Customer>>, Json<MessageResponse>> {
println!("Page before: {:?}", page);
let page: i64 = page.unwrap_or(1);
println!("Page after: {:?}", page);
} For more info: https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or This is how You can also do: If you have more questions, just ask. (if not, you can close the issue) |
Perfect! Thank you so much. |
Hmm... not sure this is (easily) possible at this point. Because this is right in between I think you would have to create a new struct in that case. You could try: fn default_page_query_param() -> PageQueryParami64 {
PageQueryParami64(1)
}
#[derive(Deserialize, Serialize, JsonSchema, Debug)]
#[serde(default = "default_page_query_param")]
struct PageQueryParami64(i64);
impl Default for PageQueryParami64 {
fn default() -> Self{
default_page_query_param()
}
}
pub async fn get_customers(
db: &State<Database>,
limit: i64,
page: PageQueryParami64,
) -> Result<Json<Vec<Customer>>, Json<MessageResponse>> {
println!("Page before: {:?}", page);
let page: i64 = page.0;
println!("Page after: {:?}", page);
} Source: GREsau/schemars#6 and https://serde.rs/field-attrs.html There are also "example values" (not the same as default) might might also fill them in, not sure. But this is already a bit more involved. Let me know if you got stuck somewhere while trying this. |
No further response, closing issue. |
I am quite new to rust and found that default parameter is not allowed in rust.
but I could not find a way to set a default value for a parameter in the swagger documentation page.
How can I set the default value for like
page = 1
in such codes?The text was updated successfully, but these errors were encountered: