110 releases (stable)

Uses new Rust 2021

new 1.2.47 Jan 26, 2022
1.2.19 Dec 31, 2021
1.0.33 Nov 30, 2021
0.9.0 Oct 8, 2021

#62 in Network programming

Download history 124/week @ 2021-10-07 190/week @ 2021-10-14 460/week @ 2021-10-21 471/week @ 2021-10-28 289/week @ 2021-11-04 580/week @ 2021-11-11 480/week @ 2021-11-18 382/week @ 2021-11-25 477/week @ 2021-12-02 360/week @ 2021-12-09 274/week @ 2021-12-16 241/week @ 2021-12-23 270/week @ 2021-12-30 373/week @ 2022-01-06 394/week @ 2022-01-13 445/week @ 2022-01-20

1,539 downloads per month

MIT/Apache

3.5MB
19K SLoC

Poem OpenAPI

Fast and Type-Safe OpenAPI implementation 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
chrono Integrate with the chrono crate.
swagger-ui Add swagger UI support
rapidoc Add RapiDoc UI support
redoc Add Redoc UI support
email Support for email address string
hostname Support for hostname string
uuid Integrate with the uuid crate
url Integrate with the url crate

Safety

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

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!        

MSRV

The minimum supported Rust version for this crate is 1.56.1.

Contributing

🎈 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.

Dependencies

~17MB
~391K SLoC