Skip to content

Specifying default value for request parameter #413

Answered by juhaku
jmhrpr asked this question in Q&A
Discussion options

You must be logged in to vote

The Parameter OpenAPI Type does not itself have default attribute. Instead one should either give an example which is used as default value by Swagger UI or maybe by giving default value to the schema used for Parameter. Now it might get tricky if the schema used for the Parameter is a String or any other literal type because there is no direct way to give a default value for them.

What could be done, is something like this seen below. We create a custom schema type with custom default value and then inline it as parameter type.

#[derive(ToSchema)]
#[schema(default = "default value")]
struct MyParmater(String)

#[utoipa::path(
  params(
    ("my_parameter = inline(MyParameter))
  )
)]

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jmhrpr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants