Documentation

Route request and response shape

Graphvein describes what a route accepts and returns: path, query, header and cookie parameters, the request body, responses and security. Every part says how Graphvein knows it, and a part Graphvein did not read is shown as "not read", never as "none". The model is shared by every framework; the rules are rows SM-1..SM-29 of the route shape claim contract, and the decision is recorded in ADR 0081 ("Shape model").

What is stored

A route node may carry the attribute shape (model version 1). A route without it has every section not read. The attribute holds:

Schemas use a small JSON-Schema-like language: object (fields with a presence each: required, optional or unknown), array, string, number, integer, boolean, null, enum of literals, a reference to a model, and unknown with a reason.

The server serves the checked shape on the endpoint detail (shape) and a compact summary on the endpoint list; see serve.

Model types

When a section's item was read from a named model (a Pydantic class and its repository bases, a Go struct and the structs it embeds, a Rust serde type, a TypeScript interface or type alias, a module-level zod or Joi schema), the route also lists the model's declaration in the attribute model_uses and has a uses_model edge to the declaration node, so change impact reaches the route when the model changes ("through request body (fact) model UserIn"). Only declarations the extractor itself resolved, and whose node is emitted, are recorded; each use keeps the item's section and provenance kind. The rules are rows MT-1..MT-19 of the route model types claim contract. An extractor marks a schema node it read from a declaration with the declaration's identity (Schema.Decls, never stored in the shape) and calls routeshape.SetModelUses with the shape it stored.

Following calls

Parameters and bodies are often read in functions the handler calls. The shape extractors follow the handler's values only through proven call targets (one stored definition), up to a depth limit set by index.shape_depth in graphvein.yaml or graphvein index --shape-depth (default 6, from 1 to 10). A chain stops for one of these reasons, which is stored with it:

Stop Meaning
ended the value is not passed on
depth_cap the next call is proven but deeper than the limit: "depth cap reached (N)", raise the setting to follow it
cycle the call returns to a function already on the chain
joined the call reaches a function already read through another chain
budget 256 functions were already read for this route
resolver_error reading a function failed (recorded; other chains continue)
unresolved, ambiguous, external the target is not proven: no definition, several, or a library
transformed, opaque_type the value is changed before it is passed on, or its type is any, unknown or generic

Adding a framework

A framework's shape extractor (for example FastAPI, Express, gin, Axum, Next.js) uses the shared package internal/extract/routeshape and adds its own claim table. It never changes the model; if a form needs a claim the model cannot express, report it as a contract change.

  1. Claim table. Write the framework's SH-<family><n> table first (public, under docs/extract/): one row per code form read, each naming the section, the provenance kind and the model rows it relies on, plus a default row for every form not read. Every hazard of the framework (sequence parameters that are bodies, dependencies reading headers, generics that are not checked, ...) has a row or a refusal. Keep a Go table 1:1 with it and test that.
  2. Build the shape. Start from routeshape.New(depthLimit) (every section not_read), set only the sections the extractor reads, and give every item a routeshape.Provenance whose Rule is the framework's row. Mark a section partial with a reason whenever a chain stopped before the end; use provably_none and complete only with evidence of kind fact or mapped. A name read in code is Access: ReadByHandler (or ReadByDependency with the chain), presence unknown, no schema.
  3. Follow calls. Implement routeshape.Resolver for the language: Visit(ctx, frame) reads frame.Function, records the reads of frame.Value (with frame.Chain as their provenance chain), and returns the calls through which the value leaves the function. Return a call with an empty Stop only for a proven target; otherwise return it with StopUnresolved, StopAmbiguous, StopExternal, StopTransformed or StopOpaqueType. Call routeshape.Follow(ctx, start, depthLimit, resolver) and store routeshape.Records(section, result) in shape.Follow.Chains. Do the following at link time over cached syntax, so changing the depth needs no re-parse; if parse-time data is needed, cache enough for depth 10 (routeshape.MaxDepthLimit).
  4. Store. routeshape.ToAttribute(shape) checks every model rule and returns the value for attrs[routeshape.AttributeKey]. If it refuses, store no shape and record a diagnostic; never store a shape that breaks a rule.
  5. Test. A fixture with a golden output for every row, including the default and every hazard; the framework's oracle where one exists (the framework's own OpenAPI output, a compiler, a schema library), with any disagreement treated as a precision stop. Run the extractor over the corpora and report routeshape.Stats (Table()): how deep chains go and how many hit depth_cap, so the default depth is set from data.
  6. Version. Shape extraction changes link output: bump the extractor version as for any extraction change.

Express and Next.js Pages Router API

Rows SH-E1..SH-E36 of the Express shape claim contract. The extractor runs after the Express and Next.js linkers (routes.LinkShapes) and stores a shape on every Express route and every Pages Router API route.

Cached syntax. The TypeScript extractor records, per file, the parameter-use syntax of its functions and its top-level interfaces and type aliases (facts.ShapeSyntax, extractor version 54): for each function, its parameters (names, destructured keys, type annotations), the type annotation of the const that binds it, its declared return type and the uses of its parameters: property paths read (directly, through const aliases or by destructuring), method chains called on them (res.status(201).json({...})), calls they are passed to, type assertions, assignments and throw statements. Nothing in it is resolved; names are resolved and calls followed at link time, so a new index.shape_depth needs no re-parse.

What is read.

Hazards. A mounted express-openapi-validator and the body parsers are noted in the sections' reasons (the committed spec, shown as mapped by its own rows, decides the shape there). A Pages Router API route is one route for every method, so the reads of all req.method branches are merged. Path parameters a Pages handler reads through req.query stay path parameters.

Next.js App Router route handlers and server actions

Rows SH-N1..SH-N29 of the Next.js route shape claim contract. The same linker as the Express rows (routes.LinkShapes) stores a shape on every App Router route (framework: nextjs) and every Next.js server_action node; the Pages Router API keeps the Express rows.

Cached syntax. The TypeScript shape payload (above) also records, from extractor version 57: the Fetch API body readers as parameter paths (await request.json() is the path json(), formData(), text(), arrayBuffer(), blob(); request.clone() is the request), new URL(request.url) as the path url.URL(), zero-argument calls of headers() and cookies() as values of their own, calls of notFound, redirect, permanentRedirect, forbidden and unauthorized, and the return statements of a function (the returned expression, the initializer of a returned const and the arguments of the returned call or construction). Returns are kept for functions named like an HTTP method, functions of a 'use server' file or with the directive, and functions with other recorded uses; in any other function only returns that build a response (Response.json, new Response, NextResponse.*) are kept. Names are resolved at link time (headers must be the import of next/headers, Response the global, NextResponse the export of next/server).

What is read.

Security. Next.js route handlers: see "Security beyond FastAPI" below.

Not read. Joi and other validators, streamed responses, NextResponse.next() and rewrite(), other route segment config (revalidate, runtime, dynamic 'auto' or 'force-dynamic'; the handler sees the request as sent), redirects and notFound() in server actions, several typed action arguments.

The oracle scripts/oracle/nextjs_shape builds the fixture testdata/fixtures/nextjs-shape with Next.js 14.1.0, 15.5.4 and 16.2.10, serves it on 127.0.0.1 and drives every claimed status, content type and empty body, the requests a fact body must refuse and accept, the server actions through their action ids, and compares the zod schemas (toJSONSchema) and the TypeScript checker's view of every name read and every model.

Axum

The Axum shape extractor reads Axum routes (axum 0.3 to 0.8). Its rows are SH-X1..SH-X29 of the Axum route shape claim contract.

The oracle scripts/oracle/axum_shape builds the fixtures testdata/fixtures/route-shape-axum (axum 0.8.8) and testdata/fixtures/route-shape-axum03 (axum 0.3.4) with the real crates (the fixture testdata/fixtures/route-shape-axum-versions covers the version reading and is golden-only), serves them on 127.0.0.1 and compares every claimed status, content type, required and optional field and parameter, type, enum and closed object with real requests and serde_json output.

FastAPI

The FastAPI and Starlette shape extractor reads rows SH-F1..SH-F39 of the FastAPI route shape claim contract. It follows FastAPI 0.161.1's own rules, checked against FastAPI's app.openapi() and a TestClient run on the fixtures testdata/fixtures/route-shape-fastapi, testdata/fixtures/route-shape-fastapi-handlers, testdata/fixtures/route-shape-fastapi-unpack and testdata/fixtures/route-shape-fastapi-assign (scripts/oracle/fastapi_shape.sh). Rules that depend on the FastAPI version (return annotations validated from 0.89.0, form models from 0.113.0) apply only when the nearest pyproject.toml requiring fastapi proves the version with a lower bound.

gin, chi and net/http

The Go shape extractor reads gin v1, chi v5 and net/http routes. Its rows are SH-G1..SH-G38 of the Go route shape claim contract.

The oracle scripts/oracle/go_shape compiles the fixture testdata/fixtures/go-shape with the real encoding/json, gin and chi and compares the claimed field sets, types, presences, statuses and binds with real marshaling and real responses on 127.0.0.1.

Security beyond FastAPI

Rows SH-S1..SH-S24 of the route security claim contract fill the security section of Express, Axum, gin, chi and Next.js App Router routes from a closed list of auth libraries whose credential form is fixed or read from literal options. Custom auth (a repository middleware, an axum from_fn, gin-jwt, a session the library names from its configuration) states no scheme: it stays as written in the route's middleware attributes, and a section with no source stays not_read.

The oracle scripts/oracle/route_security serves the fixture testdata/fixtures/route-shape-security with the real libraries (Express 4.21.1 and the npm versions its package.json pins, gin v1.12.0, chi v5.5.0, jwtauth v5.4.0, axum 0.8.8, axum-extra 0.10.3, tower-http 0.6.8) on 127.0.0.1 and checks that a request with each claimed requirement's credentials reaches the handler and that one without them (or missing one scheme of a requirement) does not.

Committed specs

A route documented by a committed OpenAPI 3.0, 3.1 or 3.2 or Swagger 2.0 operation (one inferred documents_route edge of the OpenAPI import, exact matches only) also shows what that operation says: rows SH-O1..SH-O19 of the committed spec claim contract. The items are added at link time, after every framework stored its own shape, and no syntax is cached.

Report a problem with this pageSource: docs/extract/route-shape.md