Trait utoipa::OpenApi

source ·
pub trait OpenApi {
    fn openapi() -> OpenApi;
}
Expand description

Trait for implementing OpenAPI specification in Rust.

This trait is derivable and can be used with #[derive] attribute. The derived implementation will use Cargo provided environment variables to implement the default information. For a details of #[derive(ToSchema)] refer to derive documentation.

Examples

Below is derived example of OpenApi.

use utoipa::OpenApi;
#[derive(OpenApi)]
#[openapi()]
struct OpenApiDoc;

This manual OpenApi trait implementation is approximately equal to the above derived one except the derive implementation will by default use the Cargo environment variables to set defaults for application name, version, application description, license, author name & email.

 struct OpenApiDoc;

 impl utoipa::OpenApi for OpenApiDoc {
     fn openapi() -> utoipa::openapi::OpenApi {
         use utoipa::{ToSchema, Path};
         utoipa::openapi::OpenApiBuilder::new()
             .info(utoipa::openapi::InfoBuilder::new()
                 .title("application name")
                 .version("version")
                 .description(Some("application description"))
                 .license(Some(utoipa::openapi::License::new("MIT")))
                 .contact(
                     Some(utoipa::openapi::ContactBuilder::new()
                         .name(Some("author name"))
                         .email(Some("author email")).build()),
             ).build())
             .paths(utoipa::openapi::path::Paths::new())
             .components(Some(utoipa::openapi::Components::new()))
             .build()
     }
 }

Required Methods

Implementors