OpenAPI
One of Cot’s powerful features is its ability to automatically generate OpenAPI documentation for your web API. This allows you to create interactive API documentation with minimal configuration, making your APIs more accessible and easier to understand for users and developers.
Overview
OpenAPI (formerly known as Swagger) is a specification for machine-readable interface files for describing, producing, consuming, and visualizing RESTful web services. Cot provides built-in support for:
- Automatic OpenAPI specification generation based on your route definitions
- Integration with Swagger UI for interactive API documentation
- Type-safe API development with proper schema generation
This chapter will guide you through setting up OpenAPI in your Cot project and show you how to leverage automatic specification generation to create well-documented APIs.
Prerequisites
To use OpenAPI features in Cot, you need to enable the openapi
and swagger-ui
features in your project’s Cargo.toml
:
[]
= { = "...", = ["openapi", "swagger-ui"] }
= "0.8" # Required for JSON Schema generation
The schemars
crate is necessary for creating JSON Schema definitions for your request and response types.
Setting Up Your API
Define Your Data Types
First, define your request and response data types with serde
for serialization and schemars
for schema generation:
use ;
use ;
Note the use of #[derive(JsonSchema)]
which comes from the schemars
crate. This attribute generates schema information that Cot uses to build the OpenAPI specification.
Create API Handlers
Next, create your API handlers using Cot’s extractors:
use ;
async
Use API Method Routers
Instead of using regular method routers, use the OpenAPI-enabled versions that automatically generate API documentation:
use ;
use ;
The key differences from standard routes are:
- Using
with_api_handler
instead ofwith_handler
- Using
api_post
instead ofpost
Register the Swagger UI App
To expose the interactive documentation UI, register the SwaggerUi
app in your project:
use ;
use ;
use ;
;
Don’t forget to include the StaticFilesMiddleware
as it’s required for the Swagger UI to serve its CSS and JavaScript files!
Complete Example
Here’s a complete example of a simple API with OpenAPI documentation:
use ;
use ;
use ;
use ;
use ;
use ;
use ;
use ;
use ;
use ;
async
;
;
After running this example, you can:
- Navigate to
http://localhost:8000/swagger/
to see the interactive API documentation - Test your API directly from the browser using the Swagger UI, or
- Make requests programmatically:
Advanced Features
Using Path Parameters
Path parameters are automatically detected and included in the OpenAPI specification:
use ;
async
// Register the route
URL Query Parameters
Query parameters are also supported and properly documented:
use ;
async
// Register the route
Excluding Routes from OpenAPI Documentation
Sometimes you might want to exclude certain routes from your API documentation. You can do this by using NoApi
:
use ;
// This handler will be in the API docs
// This handler will work but won't appear in the docs
You can also exclude specific parameters from the OpenAPI docs:
async
Multiple HTTP Methods
The ApiMethodRouter
allows you to define multiple HTTP methods for a single route and include them all in the OpenAPI documentation:
use ;
Each method will be properly documented in the OpenAPI specification.
Implement your own OpenAPI extractor
In order for your parameter or response type to generate OpenAPI specification, you need to implement the ApiOperationPart
trait. You can study their implementations to understand how to design your own:
Json<T>
adds a request or response body to the operationPath<T>
adds path parametersUrlQuery<T>
adds query parameters
The key is to modify the Operation
object appropriately for your extractor, adding parameters, request bodies, or other OpenAPI elements as needed.
Conclusion
Cot’s OpenAPI integration provides a powerful way to automatically generate comprehensive API documentation while maintaining type safety. By leveraging the schema generation capabilities, you can create well-documented APIs with minimal overhead, making your services more accessible and easier to use.
With just a few additions to your code, you get interactive documentation that stays in sync with your implementation, eliminating the common problem of outdated API docs. This feature is particularly valuable for teams working on APIs that are consumed by external developers or multiple internal teams.