Skip to main content

Module openapi

Module openapi 

Source
Expand description

OpenAPI 文档 — 对齐 Swagger / OpenAPI 3.0.3

提供编程式 OpenAPI 规范构建器,支持生成 JSON/YAML 规范文件并提供 Swagger UI 渲染端点。无需 derive 宏,业务代码通过链式 API 注册端点。

§使用示例

use sz_rust_core::openapi::{OpenApiBuilder, HttpMethod};

let spec = OpenApiBuilder::new("SZ-Rust API", "1.0.0")
    .description("SZ-Rust 框架 API 文档")
    .path("/api/v1/users", HttpMethod::Get, |op| {
        op.summary("获取用户列表")
          .tag("用户")
          .response(200, "成功", "application/json")
    })
    .path("/api/v1/users/{id}", HttpMethod::Get, |op| {
        op.summary("获取用户详情")
          .tag("用户")
          .parameter("id", "path", "用户 ID", true, "integer")
          .response(200, "成功", "application/json")
          .response(404, "用户不存在", "application/json")
    })
    .build();

// spec 为 serde_json::Value,可直接序列化为 JSON
let json = serde_json::to_string_pretty(&spec).unwrap();

§Swagger UI 集成

通过 swagger_ui_html 获取 Swagger UI HTML 页面,挂载到 axum 路由:

use sz_rust_core::openapi::{OpenApiBuilder, swagger_ui_html};

let spec_json = serde_json::to_string(&builder.build()).unwrap();
let html = swagger_ui_html(&spec_json);

Structs§

OpenApiBuilder
OpenAPI 规范构建器
OperationBuilder
操作构建器 — 描述单个 API 端点的元数据

Enums§

HttpMethod
HTTP 方法枚举

Functions§

redoc_html
生成 Redoc HTML 页面(替代 Swagger UI 的轻量文档查看器)
swagger_ui_html
生成 Swagger UI HTML 页面