Trait utoipa::Modify

source ·
pub trait Modify {
    // Required method
    fn modify(&self, openapi: &mut OpenApi);
}
Expand description

Trait that allows OpenApi modification at runtime.

Implement this trait if you wish to modify the OpenApi at runtime before it is being consumed (Before utoipa::OpenApi::openapi() function returns). This is trait can be used to add or change already generated OpenApi spec to alter the generated specification by user defined condition. For example you can add definitions that should be loaded from some configuration at runtime what may not be available during compile time.

See more about OpenApi derive at derive documentation.

§Examples

Add custom JWT SecuritySchema to OpenApi.

#[derive(OpenApi)]
#[openapi(modifiers(&SecurityAddon))]
struct ApiDoc;

struct SecurityAddon;

impl Modify for SecurityAddon {
    fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
         openapi.components = Some(
             utoipa::openapi::ComponentsBuilder::new()
                 .security_scheme(
                     "api_jwt_token",
                     SecurityScheme::Http(
                         HttpBuilder::new()
                             .scheme(HttpAuthScheme::Bearer)
                             .bearer_format("JWT")
                             .build(),
                     ),
                 )
                 .build(),
         )
     }
}

Add OpenAPI Server Object to alter the target server url. This can be used to give context path for api operations.

#[derive(OpenApi)]
#[openapi(modifiers(&ServerAddon))]
struct ApiDoc;

struct ServerAddon;

impl Modify for ServerAddon {
    fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
        openapi.servers = Some(vec![Server::new("/api")])
    }
}

Required Methods§

source

fn modify(&self, openapi: &mut OpenApi)

Implementors§