Crate poem_openapi[−][src]
Expand description
OpenAPI support for Poem.
Poem-openapi
allows you to easily implement APIs that comply with the
OpenAPIv3
specification. It uses procedural macros to generate a lots of
boilerplate code, so that you only need to focus on the more
important business implementations.
Features
- Type safety If your codes can be compiled, then it is fully compliant
with the
OpenAPI v3
specification. - Rustfmt friendly Do not create any DSL that does not conform to Rust’s syntax specifications.
- IDE friendly Any code generated by the procedural macro will not be used directly.
- Minimal overhead All generated code is necessary, and there is almost no overhead.
Crate features
To avoid compiling unused dependencies, Poem gates certain features, some of which are disabled by default:
Feature | Description | Default enabled |
---|---|---|
chrono | Integrate with the chrono crate. | :x: |
swagger-ui | Add swagger UI support | :heavy_check_mark: |
rapidoc | Add RapiDoc support | :heavy_check_mark: |
Example
use poem::{listener::TcpListener, Route};
use poem_openapi::{param::Query, payload::PlainText, OpenApi, OpenApiService};
struct Api;
#[OpenApi]
impl Api {
#[oai(path = "/hello", method = "get")]
async fn index(&self, name: Query<Option<String>>) -> PlainText<String> {
match name.0 {
Some(name) => PlainText(format!("hello, {}!", name)),
None => PlainText("hello!".to_string()),
}
}
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let api_service =
OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000/api");
let ui = api_service.swagger_ui();
let app = Route::new().nest("/api", api_service).nest("/", ui);
poem::Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await
}
Run example
Open http://localhost:3000/ui
in your browser, you will see the Swagger UI
that contains these API definitions.
> cargo run --example hello_world
> curl http://localhost:3000
hello!
> curl http://localhost:3000\?name\=sunli
hello, sunli!
Modules
Some certificate types for security scheme.
Parameter types for the API operation.
Commonly used payload types.
Commonly used data types.
Structs
API for the combine
method.
Options for the parameter extractor.
A license information for the exposed API.
An OpenAPI service for Poem.
An object representing a Server.
Enums
API extractor types.
This type represents errors that occur when parsing the HTTP request.
Traits
Represents a OpenAPI extractor.
Represents a OpenAPI responses object.
Represents a OAuth scopes.
Represents a OpenAPI object.
Represents a OpenAPI tags.
Attribute Macros
Define a OpenAPI.
Derive Macros
Define a OpenAPI request.
Define a OpenAPI response.
Define a OpenAPI enum
Define a OpenAPI payload.
Define a OAuth scopes.
Define a OpenAPI object
Define a OpenAPI discriminator object.
Define a OpenAPI Security Scheme.
Define a OpenAPI Tags.