Example: Openapi ​
Demonstrates openapi.zig using the canonical HTTPX API.
Complete Example ​
zig
//! Generate an OpenAPI 3.1 document for a registered router.
//!
//! Run with: `zig build run-openapi`
//!
//! Demonstrates building a router with documented routes and producing
//! the OpenAPI JSON spec to stdout. The same data drives the built-in
//! /openapi.json, /docs (Swagger UI), and /redoc endpoints when
//! `enableDocs` is left on.
const std = @import("std");
const httpx = @import("httpx");
fn listHandler(_: *httpx.Context) anyerror!httpx.Response {
return .{ .status = 200, .body = "[]", .contentType = "application/json" };
}
fn getHandler(ctx: *httpx.Context) anyerror!httpx.Response {
const id = ctx.param("id") orelse "0";
return .{
.status = 200,
.body = id,
.contentType = "text/plain",
};
}
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var router = httpx.Router.init(allocator);
defer router.deinit();
try router.add("/widgets", listHandler, .{.meta = .{
.summary = "List all widgets",
.description = "Returns the full set of widgets.",
}});
try router.add("/widgets/{id}", getHandler, .{.meta = .{
.summary = "Fetch a single widget",
.description = "Looks up a widget by its opaque id.",
}});
const spec = try httpx.openapi.generate(&router, .{
.title = "Widgets API",
.version = "1.0.0",
.description = "Demonstration OpenAPI document generated at build time.",
});
defer allocator.free(spec);
// Examples are stdlib-friendly: dump the generated JSON to the debug
// stream rather than plumbing an Io through stdout.
std.debug.print("{s}\n", .{spec});
}How to Run ​
bash
zig build run-openapi