-
Hi, how can I have optional parameters in my query? I have found similar questions with a possible solution related to query strings, but at the moment I wonder if I can keep the path syntax I am most familiar with. Related: #622 and #683. #[utoipa::path(
get,
path = "/all_users/{start_id}/{continuation_token}",
params(
("start_id", description = "Optional: Minimum ID of users to look for."),
("continuation_token", description = "Optional: Pass when requesting continuation pages."),
),
tag = TAG_USER,
responses(
(status = 200, description = "description.", body = [Vec<User>])
)
)]
pub async fn list_all_users(
Path((start_id, continuation_token)): Path<(Option<i32>, Option<String>)>,
DatabaseConnection(mut conn): DatabaseConnection,
) -> Result<Json<Vec<User>>, FetchUserDataError> {
todo!()
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The docs gives some examples of You are probably mixing up Query parameters are the ones that are defined after the question mark ( |
Beta Was this translation helpful? Give feedback.
-
I have a similar issue where i got this code: #[derive(Deserialize, IntoParams)]
pub struct GetFileVersionRequestQueryParams {
/// download the file/set the content disposition header to attachment
pub d: Option<bool>,
/// request setting the caching headers for max-age in seconds
pub c: Option<u64>,
}
#[utoipa::path(
get,
path = "/api/file_versions/content/get/{file_id}/{version}/{app_id}/{app_path}",
params(
("file_id" = Uuid, Path, description = "The ID of the file to retrieve content for"),
("version" = Option<u32>, Path, description = "The version of the file to retrieve, if applicable"),
("app_id" = Option<Uuid>, Path),
("app_path" = Option<String>, Path),
GetFileVersionRequestQueryParams
),
responses(
(status = 200, description = "Gets a single files data/content from the server", body = Vec<u8> ),
(status = 206, description = "Partial content returned due to range request", body = Vec<u8>),
(status = 404, description = "File not found", body = ApiResponse<EmptyApiResponse>),
(status = 500, description = "Internal server error retrieving file content", body = ApiResponse<EmptyApiResponse>)
)
)]
pub async fn get_file_version_content(
external_user: IntrospectedUser,
State(ServerState { db, .. }): State<ServerState>,
Extension(timing): Extension<axum_server_timing::ServerTimingExtension>,
Path((file_id, version, app_id, app_path)): Path<(
Uuid,
Option<u32>,
Option<Uuid>,
Option<String>,
)>, Now when I request:
the response is:
or if the version is omited:
|
Beta Was this translation helpful? Give feedback.
The docs gives some examples of
params
format https://docs.rs/utoipa/latest/utoipa/attr.path.html#parameter-formats.You are probably mixing up
path
params andquery
params. Path params that are described in the path e.g./user/{id}/{type}
must be present. Path params are always required and cannot be non required. That means that in order to call the path you must give those parameters otherwise the endpoint does not match. You could call the endpoint with/user/null/null
which will result inNone
for both values. But calling it with/user
does only give the default behavior of the http framework you are using which is either 404, 415, or 500 most likely.Query parameters are the ones th…