ApiOperationPart

Trait ApiOperationPart 

Source
pub trait ApiOperationPart {
    // Provided method
    fn modify_api_operation(
        operation: &mut Operation,
        route_context: &RouteContext<'_>,
        schema_generator: &mut SchemaGenerator,
    ) { ... }
}
Available on crate feature openapi only.
Expand description

A trait that can be implemented for types that should be taken into account when generating OpenAPI paths.

When implementing this trait for a type, you can modify the Operation object to add information about the type to the OpenAPI spec. The default implementation of ApiOperationPart::modify_api_operation does nothing to indicate that the type has no effect on the OpenAPI spec.

§Example

use cot::aide::openapi::{Operation, MediaType, ReferenceOr, RequestBody};
use cot::openapi::{ApiOperationPart, RouteContext};
use cot::request::Request;
use cot::request::extractors::FromRequest;
use indexmap::IndexMap;
use cot::schemars::SchemaGenerator;
use serde::de::DeserializeOwned;

pub struct Json<D>(pub D);

impl<D: DeserializeOwned> FromRequest for Json<D> {
    async fn from_request(head: &cot::request::RequestHead, body: cot::Body) -> cot::Result<Self> {
        // parse the request body as JSON
    }
}

impl<D: schemars::JsonSchema> ApiOperationPart for Json<D> {
    fn modify_api_operation(
        operation: &mut Operation,
        _route_context: &RouteContext<'_>,
        schema_generator: &mut SchemaGenerator,
    ) {
        operation.request_body = Some(ReferenceOr::Item(RequestBody {
            content: IndexMap::from([(
                "application/json".to_owned(),
                MediaType {
                    schema: Some(aide::openapi::SchemaObject {
                        json_schema: D::json_schema(schema_generator),
                        external_docs: None,
                        example: None,
                    }),
                    ..Default::default()
                },
            )]),
            ..Default::default()
        }));
    }
}

Provided Methods§

Source

fn modify_api_operation( operation: &mut Operation, route_context: &RouteContext<'_>, schema_generator: &mut SchemaGenerator, )

Modify the OpenAPI operation object.

This function is called by the framework when generating the OpenAPI spec for a route. You can use this function to add custom information to the operation object.

The default implementation does nothing.

§Examples
use aide::openapi::Operation;
use cot::openapi::{ApiOperationPart, RouteContext};
use schemars::SchemaGenerator;

struct MyExtractor<T>(T);

impl<T> ApiOperationPart for MyExtractor<T> {
    fn modify_api_operation(
        operation: &mut Operation,
        _route_context: &RouteContext<'_>,
        _schema_generator: &mut SchemaGenerator,
    ) {
        // Add custom OpenAPI information to the operation
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§