Poem OpenAPI

Fast and Type-Safe OpenAPI implementation for Poem.

Crates.io version Download docs.rs docs Unsafe Rust forbidden rustc 1.54+

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

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 | :heavycheckmark: |

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

Example

```rust use poem::{listener::TcpListener, Route}; use poem_openapi::{payload::PlainText, OpenApi, OpenApiService};

struct Api;

[OpenApi]

impl Api { #[oai(path = "/hello", method = "get")] async fn index( &self, #[oai(name = "name", in = "query")] name: Option, ) -> PlainText { match name { Some(name) => PlainText(format!("hello, {}!", name)), None => PlainText("hello!".to_string()), } } }

[tokio::main]

async fn main() -> Result<(), std::io::Error> { let listener = TcpListener::bind("127.0.0.1:3000"); let apiservice = OpenApiService::new(Api) .title("Hello World") .server("http://localhost:3000/api"); let ui = apiservice.swagger_ui("http://localhost:3000");

poem::Server::new(listener)
    .await?
    .run(Route::new().nest("/api", api_service).nest("/", ui))
    .await

} ```

Run example

Open http://localhost:3000/ui in your browser, you will see the Swagger UI that contains these API definitions.

```shell

cargo run --example hello_world

curl http://localhost:3000 hello!

curl http://localhost:3000\?name\=sunli hello, sunli!
```

MSRV

The minimum supported Rust version for this crate is 1.54.0.

Contributing

:balloon: Thanks for your help improving the project! We are so happy to have you!

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Poem by you, shall be licensed as Apache, without any additional terms or conditions.