Skip to main content

Module openapi

Module openapi 

Source
Expand description

OpenAPI 3.1 document generated from the registered routes

Every route you register shows up in the document with its method, path and path parameters. Attach details with Operation through App::doc (or RouteGroup::doc), then publish the document with App::openapi:

use ullun::openapi::{schema, OpenApiInfo, Operation};
use ullun::prelude::*;

App::new()
    .get_with_params("/users/{id}", |p: Params| async move {
        Ok(Response::json(serde_json::json!({ "id": p.get("id")? })))
    })
    .doc(
        "GET",
        "/users/{id}",
        Operation::new("Fetch a user")
            .tag("users")
            .path_param("id", schema::integer(), "User id")
            .response_json(200, "The user", schema::object([("id", schema::integer())], ["id"]))
            .error_response(404, "No such user"),
    )
    .openapi(OpenApiInfo::new("Users API", "1.0.0").swagger_ui("/docs"))
    .run("127.0.0.1:3000")
    .await
    .unwrap();

The document is built when the server starts, so routes added after the openapi call are included too. Static file routes are left out. swagger_ui serves a page that loads Swagger UI from unpkg (pinned version with subresource integrity hashes); leave it off if you do not want a third-party script on your server.

Modules§

schema
Small helpers for JSON Schema values

Structs§

OpenApiInfo
Document metadata and where to publish it
Operation
Description of one operation (method + path)