OpenAPI · TypeSpec → Axum

Spec in.
Fast, correct Rust out.

crabspec turns an OpenAPI 3.0/3.1 document or a TypeSpec design into an idiomatic Axum server. You write one plain async fn per operation. It writes the routing, parsing, validation and typed responses.

$ crabspec generate openapi.yaml -o src/api --module

main.tspTypeSpec
model Pet {
  @visibility(Lifecycle.Read) id: uuid;
  @minLength(1) name: string;
  owner?: Owner;
}

@route("/pets/{petId}") op read(
  @path petId: uuid
): Pet | NotFound;
crabspec generate ↓
src/lib.rsyou write this part
impl PetsApi for PetStore {
  async fn read(&self, req: PetsReadRequest)
    -> Result<PetsReadResponse, Error> {
    // req.pet_id is already a validated uuid
    match self.db.find(req.pet_id).await? {
      Some(pet) => Ok(PetsReadResponse::Ok(pet)),
      None => Ok(PetsReadResponse::NotFound(..)),
    }
  }
}

Save CPUs. Sam needs them for GPUs.

On the same spec, OpenAPI Generator's rust-axum server burns 2.6× the CPU per request. crabspec's generated server runs level with hand-written Axum, and still validates every request.

2.6×less CPU per request

Level with hand-written Axum

Petstore, the same in-memory logic in all three servers. CPU time per 100,000 requests: shorter is better.

OpenAPI Generatorrust-axum 7.28.0
4.8 s CPU
~124k req/s · p99 1.5 ms
crabspecgenerated, fully validated
1.84 s CPU
~207k req/s · p99 0.86 ms
Hand-written Axumthe baseline
1.88 s CPU
~208k req/s · p99 0.84 ms

oha with 64 connections for 8 s per endpoint, averaged over GET /pets, GET /pets/{id} and POST /pets, on Apple Silicon with the load generator on the same machine. All requests returned 2xx; expect a few percent of run-to-run noise. crabspec doesn't hop to a blocking thread pool twice per request or run an HTML sanitizer over every string. Reproduce it with bench/setup.sh and bench/run-bench.sh in the repo.

Why another generator?

We ran real specs through OpenAPI Generator's rust-axum: a TypeSpec design, OpenAI's 406-operation API, and a small spec of known edge cases. Every crabspec result below has a test in the repo.

Inputrust-axum 7.28.0crabspec
OpenAI's API spec (406 ops, OAS 3.1)535k lines, 62 compile errors185k lines, compiles
Versioned TypeSpec API (2026-06-01)Invalid Cargo.tomlCompiles
Recursive types (Dog.bestFriend: Dog)Infinite-size type errorOption<Box<Dog>>, only where needed
POST /pets without the read-only idRejected: missing field idAccepted; PetInput has no id
A Cat posted as a PetSilently becomes {"kind":"Pet"}, indoor droppedPet::Cat(Cat), round-trips exactly
Integer enum size: 2Rejected: expects the string "2"2
"Tom <3" as a nameRejected as "XSS"It's a name
Handler signatureasync_trait, plus &Method, &Host, &CookieJar on every callasync fn list(&self, request: PetsListRequest)

What you get

The generated code depends only on axum, serde and serde_json, is formatted, and is documented from your spec's descriptions.

Types that mean what the spec says

  • Required vs optional and nullable vs not are four distinct shapes.
  • allOf + discriminator becomes enum Pet { Cat(Cat), Dog(Dog) }.
  • oneOf/anyOf become tagged or untagged enums.
  • readOnly/writeOnly get separate …Input types.
  • Merge-patch gets Option<Option<T>>.

Requests checked before your code runs

  • Path, query, header and cookie params parsed into typed fields.
  • Lengths, ranges, item counts, sized ints like uint8.
  • pattern with exact ECMAScript semantics, compiled once.
  • date-time, date, time and uuid formats.
  • 400/415/422 JSON errors with paths like body.owner.name.

Security from the spec, closed by default

  • Bearer, basic, API keys, OAuth2 and OIDC become a Security trait.
  • No defaults: a secured server won't compile without the check.
  • The caller arrives typed as request.auth.
  • 401 with WWW-Authenticate, 403 for missing scopes.
  • Authentication runs before the body is read.

Start small

  • Every operation defaults to 501, so a 70-operation API compiles before you write a handler.
  • TypeSpec Interface_operation ids become interface.operation().

Typed responses

  • One enum per operation: Ok, Created { body, location }, NotFound(ApiError).
  • Response headers are fields.
  • No async_trait, no boxed futures.

Safe to regenerate

  • Files you've taken over by hand are never overwritten.
  • crabspec generate --check fails CI when generated code is stale.
  • router() returns a plain axum::Router: your tower layers attach as usual.

Quick start

crabspec is on GitHub now; a crates.io release is coming soon.

01

Install

cargo install --git \
  https://github.com/Wolg/crabspec crabspec
02

Generate

# from TypeSpec
npx tsp compile .
crabspec generate \
  tsp-output/@typespec/openapi3/openapi.yaml \
  -o src/api --module
03

Serve

let app = router(Arc::new(PetStore::new()));
axum::serve(listener, app).await?;

Status: v0.1, experimental

Expect breaking changes. A spec that breaks crabspec is the best bug report.

Not there yet

  • Client generation.
  • Multipart and other non-JSON bodies arrive as raw Bytes.
  • Dates and UUIDs are validated Strings, not chrono/uuid types; email and uri aren't checked.
  • Constraints on a request body's root schema aren't enforced.

Tested against

  • OpenAI's 406-operation API spec.
  • Real-world TypeSpec projects, compiled to OAS 3.0 and 3.1.
  • Fixture specs that must stay clippy-clean with -D warnings on every change.
  • HTTP conformance tests for validation and security.