Back to Poem

README

poem-openapi/README.md

2.0.08.5 KB
Original Source
<h1 align="center">Poem OpenAPI</h1> <p align="center">Fast and Type-Safe OpenAPI implementation for Poem.</p> <div align="center"> <!-- Crates version --> <a href="https://crates.io/crates/poem-openapi"> </a> <!-- Downloads --> <a href="https://crates.io/crates/poem-openapi"> </a> <!-- docs.rs docs --> <a href="https://docs.rs/poem-openapi"> </a> <a href="https://github.com/rust-secure-code/safety-dance/"> </a> <a> </a> <a href="https://discord.gg/qWWNxwasb7"> </a> </div>

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:

FeatureDescription
caminoIntegrate with the camino crate.
chronoIntegrate with the chrono crate.
timeIntegrate with the time crate.
humantimeIntegrate with the humantime crate
openapi-explorerAdd OpenAPI Explorer support
swagger-uiAdd swagger UI support
rapidocAdd RapiDoc UI support
redocAdd Redoc UI support
scalarAdd Scalar UI support
stoplight-elementsAdd Stoplight Elements UI support
emailSupport for email address string
hostnameSupport for hostname string
ulidIntegrate with the ulid crate
uuidIntegrate with the uuid crate
urlIntegrate with the url crate
geoIntegrate with the geo-types crate
bsonIntegrate with the bson crate
rust_decimalIntegrate with the rust_decimal crate
prost-wkt-typesIntegrate with the prost-wkt-types crate
static-filesSupport for static file response
websocketSupport for websocket
sonic-rsUses sonic-rs instead of serde_json. Pls, checkout sonic-rs requirements to properly enable sonic-rs capabilities

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::{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("0.0.0.0:3000"))
        .run(app)
        .await
}

This feature needs to be opted-in. It can be done by adding the feature in Cargo.toml file

toml
[dependencies]
poem = "3"
poem-openapi = { version = "5", features = ["swagger-ui"]}
tokio = { version = "1", features = ["full"] }

Run example

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

shell
> cargo run --example hello_world

> curl http://localhost:3000/api/hello
hello!

> curl http://localhost:3000/api/hello?name=sunli
hello, sunli!        

MSRV

The minimum supported Rust version for this crate is 1.83.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.