Docs.rs
  • poem-openapi-2.0.23
    • poem-openapi 2.0.23
    • Docs.rs crate page
    • MIT/Apache-2.0
    • Links
    • Homepage
    • Repository
    • crates.io
    • Source
    • Owners
    • sunli829
    • Dependencies
      • base64 ^0.13.0 normal
      • bson ^2.0.0 normal
      • bytes ^1.1.0 normal
      • chrono ^0.4.19 normal
      • derive_more ^0.99.16 normal
      • email_address ^0.2.1 normal
      • futures-util ^0.3.17 normal
      • hostname-validator ^1.1.0 normal
      • humantime ^2.1.0 normal
      • mime ^0.3.16 normal
      • num-traits ^0.2.14 normal
      • poem ^1.3.52 normal
      • poem-openapi-derive ^2.0.23 normal
      • quick-xml ^0.26.0 normal
      • regex ^1.5.5 normal
      • rust_decimal ^1.22.0 normal
      • serde ^1.0.130 normal
      • serde_json ^1.0.68 normal
      • serde_urlencoded ^0.7.1 normal
      • serde_yaml ^0.9.0 normal
      • thiserror ^1.0.30 normal
      • time ^0.3.9 normal
      • tokio ^1.17.0 normal
      • url ^2.2.2 normal
      • uuid ^1.1.0 normal
      • poem ^1.3.52 dev
      • tokio ^1.17.0 dev
    • Versions
    • 100% of the crate is documented
  • Go to latest version
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation
logo

Crate poem_openapi

logo

Crate poem_openapi

  • Version 2.0.23
  • All Items
  • Modules
  • Macros
  • Structs
  • Enums
  • Traits
  • Attribute Macros
  • Derive Macros

Crates

  • poem_openapi
?
Change settings

Crate poem_openapi

source ·
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.

Table of contents

  • Features
  • Quickstart
  • Crate features

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.

Quickstart

Cargo.toml

[package]
name = "helloworld"
version = "0.1.0"
edition = "2021"

[dependencies]
poem = "1.2"
poem-openapi = { version = "1.2", features = ["swagger-ui"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

main.rs

ⓘ
use poem::{listener::TcpListener, Route, Server};
use poem_openapi::{payload::PlainText, OpenApi, OpenApiService};

struct Api;

#[OpenApi]
impl Api {
    /// Hello world
    #[oai(path = "/", method = "get")]
    async fn index(&self) -> PlainText<&'static str> {
        PlainText("Hello World")
    }
}

let api_service =
    OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000");
let ui = api_service.swagger_ui();
let app = Route::new().nest("/", api_service).nest("/docs", ui);

Server::new(TcpListener::bind("127.0.0.1:3000"))
    .run(app)
    .await;

Check it

Open your browser at http://127.0.0.1:3000.

You will see the plaintext response as:

Hello World

Interactive API docs

Now go to http://127.0.0.1:3000/docs.

You will see the automatic interactive API documentation (provided by Swagger UI):

swagger-ui

Crate features

To avoid compiling unused dependencies, Poem gates certain features, some of which are disabled by default:

FeatureDescription
chronoIntegrate with the chrono crate
timeIntegrate with the time crate.
humantimeIntegrate with the humantime crate
swagger-uiAdd swagger UI support
rapidocAdd RapiDoc UI support
redocAdd Redoc UI support
emailSupport for email address string
hostnameSupport for hostname string
uuidIntegrate with the uuid crate
urlIntegrate with the url crate
bsonIntegrate with the bson crate
rust_decimalIntegrate with the rust_decimal crate
static-filesSupport for static file response

Modules

auth
Some certificate types for security scheme.
error
Some common error types.
macros
Macros to help with building custom payload types.
param
Parameter types for the API operation.
payload
Commonly used payload types.
types
Commonly used data types.

Macros

impl_apirequest_for_payload
This macro implements ApiExtractor for your type, with additional bounds if you want to.

Structs

ContactObject
A contact information for the exposed API.
ExternalDocumentObject
An object representing a external document.
ExtraHeader
An extra header
ExtractParamOptions
Options for the parameter extractor.
LicenseObject
A license information for the exposed API.
OpenApiService
An OpenAPI service for Poem.
OperationId
A operation id that can be obtained from the response
ServerObject
An object representing a Server.

Enums

ApiExtractorType
API extractor types.

Traits

ApiExtractor
Represents a OpenAPI extractor.
ApiResponse
Represents a OpenAPI responses object.
OAuthScopes
Represents a OAuth scopes.
OpenApi
Represents a OpenAPI object.
ResponseContent
Represents a OpenAPI response content object.
Tags
Represents a OpenAPI tags.
Validator
Represents a validator for validate the input value.
Webhook
Represents a webhook object.

Attribute Macros

OpenApi
Define a OpenAPI.
Webhook
Define a OpenApi webhooks.

Derive Macros

ApiRequest
Define a OpenAPI request.
ApiResponse
Define a OpenAPI response.
Enum
Define a OpenAPI enum
Multipart
Define a OpenAPI payload.
NewType
Define a new type.
OAuthScopes
Define a OAuth scopes.
Object
Define a OpenAPI object
ResponseContent
Define a OpenAPI response content.
SecurityScheme
Define a OpenAPI Security Scheme.
Tags
Define a OpenAPI Tags.
Union
Define a OpenAPI discriminator object.

Results

poem_openapi::Validator
Represents a validator for validate the input value.
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.