utoipa::openapi

Struct OpenApi

Source
#[non_exhaustive]
pub struct OpenApi { pub openapi: OpenApiVersion, pub info: Info, pub servers: Option<Vec<Server>>, pub paths: Paths, pub components: Option<Components>, pub security: Option<Vec<SecurityRequirement>>, pub tags: Option<Vec<Tag>>, pub external_docs: Option<ExternalDocs>, pub schema: String, pub extensions: Option<Extensions>, }
Expand description

Root object of the OpenAPI document.

You can use OpenApi::new function to construct a new OpenApi instance and then use the fields with mutable access to modify them. This is quite tedious if you are not simply just changing one thing thus you can also use the OpenApiBuilder::new to use builder to construct a new OpenApi object.

See more details at https://spec.openapis.org/oas/latest.html#openapi-object.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§openapi: OpenApiVersion

OpenAPI document version.

§info: Info

Provides metadata about the API.

See more details at https://spec.openapis.org/oas/latest.html#info-object.

§servers: Option<Vec<Server>>

Optional list of servers that provides the connectivity information to target servers.

This is implicitly one server with url set to /.

See more details at https://spec.openapis.org/oas/latest.html#server-object.

§paths: Paths

Available paths and operations for the API.

See more details at https://spec.openapis.org/oas/latest.html#paths-object.

§components: Option<Components>

Holds various reusable schemas for the OpenAPI document.

Few of these elements are security schemas and object schemas.

See more details at https://spec.openapis.org/oas/latest.html#components-object.

§security: Option<Vec<SecurityRequirement>>

Declaration of global security mechanisms that can be used across the API. The individual operations can override the declarations. You can use SecurityRequirement::default() if you wish to make security optional by adding it to the list of securities.

See more details at https://spec.openapis.org/oas/latest.html#security-requirement-object.

§tags: Option<Vec<Tag>>

Optional list of tags can be used to add additional documentation to matching tags of operations.

See more details at https://spec.openapis.org/oas/latest.html#tag-object.

§external_docs: Option<ExternalDocs>

Optional global additional documentation reference.

See more details at https://spec.openapis.org/oas/latest.html#external-documentation-object.

§schema: String

Schema keyword can be used to override default $schema dialect which is by default “https://spec.openapis.org/oas/3.1/dialect/base”.

All the references and individual files could use their own schema dialect.

§extensions: Option<Extensions>

Optional extensions “x-something”.

Implementations§

Source§

impl OpenApi

Source

pub fn builder() -> OpenApiBuilder

Construct a new OpenApiBuilder.

This is effectively same as calling OpenApiBuilder::new

Source§

impl OpenApi

Source

pub fn new<P: Into<Paths>>(info: Info, paths: P) -> Self

Construct a new OpenApi object.

Function accepts two arguments one which is Info metadata of the API; two which is Paths containing operations for the API.

§Examples
let openapi = OpenApi::new(Info::new("pet api", "0.1.0"), Paths::new());
Source

pub fn to_json(&self) -> Result<String, Error>

Converts this OpenApi to JSON String. This method essentially calls serde_json::to_string method.

Source

pub fn to_pretty_json(&self) -> Result<String, Error>

Converts this OpenApi to pretty JSON String. This method essentially calls serde_json::to_string_pretty method.

Source

pub fn to_yaml(&self) -> Result<String, Error>

Available on crate feature yaml only.

Converts this OpenApi to YAML String. This method essentially calls serde_yaml::to_string method.

Source

pub fn merge_from(self, other: OpenApi) -> OpenApi

Merge other OpenApi moving self and returning combined OpenApi.

In functionality wise this is exactly same as calling OpenApi::merge but but provides leaner API for chaining method calls.

Source

pub fn merge(&mut self, other: OpenApi)

Merge other OpenApi consuming it and resuming it’s content.

Merge function will take all self nonexistent servers, paths, schemas, responses, security_schemes, security_requirements and tags from other OpenApi.

This function performs a shallow comparison for paths, schemas, responses and security schemes which means that only name and path is used for comparison. When match occurs the whole item will be ignored from merged results. Only items not found will be appended to self.

For servers, tags and security_requirements the whole item will be used for comparison. Items not found from self will be appended to self.

Note! info, openapi, external_docs and schema will not be merged.

Source

pub fn nest<P: Into<String>, O: Into<OpenApi>>(self, path: P, other: O) -> Self

Nest other OpenApi to this OpenApi.

Nesting performs custom OpenApi::merge where other OpenApi paths are prepended with given path and then appended to paths of this OpenApi instance. Rest of the other OpenApi instance is merged to this OpenApi with OpenApi::merge_from method.

If multiple APIs are being nested with same path only the last one will be retained.

Method accepts two arguments, first is the path to prepend .e.g. /user. Second argument is the OpenApi to prepend paths for.

§Examples

Merge user_api to api nesting user_api paths under /api/v1/user

 let api = OpenApiBuilder::new()
     .paths(
         PathsBuilder::new().path(
             "/api/v1/status",
             PathItem::new(
                 HttpMethod::Get,
                 OperationBuilder::new()
                     .description(Some("Get status"))
                     .build(),
             ),
         ),
     )
     .build();
 let user_api = OpenApiBuilder::new()
    .paths(
        PathsBuilder::new().path(
            "/",
            PathItem::new(HttpMethod::Post, OperationBuilder::new().build()),
        )
    )
    .build();
 let nested = api.nest("/api/v1/user", user_api);

Trait Implementations§

Source§

impl Clone for OpenApi

Source§

fn clone(&self) -> OpenApi

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for OpenApi

Source§

fn default() -> OpenApi

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for OpenApi

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<OpenApi> for OpenApiBuilder

Source§

fn from(value: OpenApi) -> Self

Converts to this type from the input type.
Source§

impl From<OpenApiBuilder> for OpenApi

Source§

fn from(value: OpenApiBuilder) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for OpenApi

Source§

fn eq(&self, other: &OpenApi) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for OpenApi

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for OpenApi

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,