31 releases (unstable)

1.0.0-pre-release.9 Sep 19, 2024
1.0.0-pre-release.4 Aug 6, 2024
1.0.0-pre-release.2 Jun 29, 2024
0.4.1 Sep 25, 2024
0.1.7 Mar 26, 2024

#104 in Web programming

Download history 949/week @ 2024-08-21 956/week @ 2024-08-28 1080/week @ 2024-09-04 1188/week @ 2024-09-11 1220/week @ 2024-09-18 1187/week @ 2024-09-25 1018/week @ 2024-10-02 878/week @ 2024-10-09 593/week @ 2024-10-16 765/week @ 2024-10-23 926/week @ 2024-10-30 859/week @ 2024-11-06 906/week @ 2024-11-13 940/week @ 2024-11-20 955/week @ 2024-11-27 770/week @ 2024-12-04

3,759 downloads per month
Used in 3 crates

MIT/Apache

330KB
5.5K SLoC

Apistos   Documentation Latest Version Build Status Deps Status

An OpenAPI documentation tool exposing OAS 3.0 models as well as an actix-web wrapper similar to paperclip.

Apistos is composed of these crates:

Check out our example project.

What does Apistos means

Apistos (pronounced /a.p.i.stos/) is a word play between Héphaïstos (Ἥφαιστος, grec god of blacksmiths, carpenters, craftsmen, metallurgy ... which can also be considered by some as the god of technology) and API (pronounced /a.p.i/ in French).

Apistos

Installation

[dependencies]
schemars = "1.0.0-alpha.14"
apistos = "1.0.0-pre-release.9"

Usage example

Wrap your regular actix-web app using apistos types.

Most of these types are drop-in types for actix-web one's.

use std::fmt::Display;
use actix_web::{App, HttpServer, ResponseError};
use actix_web::http::StatusCode;
use actix_web::middleware::Logger;
use actix_web::web::Json;
use apistos::actix::CreatedJson;
use apistos::api_operation;
use apistos::ApiComponent;
use apistos::ApiErrorComponent;
use apistos::app::OpenApiWrapper;
use apistos::spec::Spec;
use apistos::web::{post, resource, scope};
use apistos_models::info::Info;
use core::fmt::Formatter;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::net::Ipv4Addr;

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, ApiComponent)]
pub struct Test {
  pub test: String
}

#[derive(Serialize, Deserialize, Debug, Clone, ApiErrorComponent)]
#[openapi_error(
  status(code = 403),
  status(code = 404),
  status(code = 405, description = "Invalid input"),
  status(code = 409)
)]
pub enum ErrorResponse {
  MethodNotAllowed(String),
  NotFound(String),
  Conflict(String),
  Unauthorized(String),
}

impl Display for ErrorResponse {
  fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
    todo!()
  }
}

impl ResponseError for ErrorResponse {
  fn status_code(&self) -> StatusCode {
    todo!()
  }
}

#[api_operation(
  tag = "pet",
  summary = "Add a new pet to the store",
  description = r###"Add a new pet to the store
    Plop"###,
  error_code = 405
)]
pub(crate) async fn test(
  body: Json<Test>,
) -> Result<CreatedJson<Test>, ErrorResponse> {
  Ok(CreatedJson(body.0))
}

#[actix_web::main]
async fn main() -> Result<(), impl Error> {
  HttpServer::new(move || {
    let spec = Spec {
      info: Info {
        title: "An API".to_string(),
        version: "1.0.0".to_string(),
        ..Default::default()
      },
      ..Default::default()
    };

    App::new()
        .document(spec)
        .wrap(Logger::default())
        .service(scope("/test")
            .service(
              resource("")
                  .route(post().to(test))
            )
        )
        .build("/openapi.json")
  })
      .bind((Ipv4Addr::UNSPECIFIED, 8080))?
      .run()
      .await
}

For a complete example, see the sample petstore.

Feature flags

name description extra dependencies
query (default) Enables documenting actix_web::web::Query
actix (default) Enables documenting types from actix
lab_query Enables documenting actix_web_lab::extract::Query actix-web-lab
garde Enables input validation through garde garde
actix-web-grants Enables support for actix-web-grants actix-web-grants
rapidoc Enables RapiDoc to expose the generated openapi file
redoc Enables Redoc to expose the generated openapi file
swagger-ui Enables Swagger UI to expose the generated openapi file
qs_query Enables documenting types from serde_qs serde_qs
chrono Enables documenting types from chrono chrono
ipnetwork Enables documenting types from ipnetwork. Supported via apistos::ipnetwork mod. ipnetwork
multipart Enables documenting types from actix-multipart. Tempfile is supported though apistos::multipart::Tempfile struct. actix-multipart
rust_decimal Enables documenting types from rust_decimal rust_decimal
uuid Enables documenting types from uuid uuid
url Enables documenting types from url url
extras Enables chrono, multipart, rust_decimal, uuid and url features All from previous features

What's next

  • Handle schema for errors using ApiErrorComponent derive macro

Alternatives

Crate Key differences
paperclip Paperclip is similar to this project but generates Swagger v2 documentation. Paperclip also provides a tool to generate rust code from a Swagger v2 document.
utoipa Utoipa-actix integration rely on actix web macros for routing definition. At first, we planned on relying on utoipa for OAS types and schema derivation but for now utoipa doesn't support generic struct the way we intended to.
okapi Pretty similar, based on schemars as well (and maintained by the founder of schemars) but not integrated with actix.

Articles

  • announcement article can be found on medium. It acts as a tutorial for Apistos.

About us

apistos is provided by Netwo.

We use this crate for our internal needs and therefore are committed to its maintenance, however we cannot provide any additional guaranty. Use it at your own risks.

While we won't invest in any feature we don't need, we are open to accept any pull request you might propose.

We are a France based full-remote company operating in the telecom industry. If you are interested in learning more, feel free to visit our career page.

Dependencies

~21–35MB
~604K SLoC