smalldocs.api_openapi

The reference of an HTTP API, from its OpenAPI document: a page for each tag, a page of schemas, and the names for links.

A section of api with language: openapi takes exactly one input:

api:
  - language: openapi
    fastapi: api.app:app                  # a FastAPI app, as module:attribute, imported in the project's environment
    setup: api.routers:register_routers   # with fastapi: a function that adds the routes, for an app that adds them at startup
    out: reference
  - language: openapi
    command: cargo run --quiet --bin openapi   # a command that prints the document, run in root
    out: rust-http
  - language: openapi
    file: openapi.yaml                    # a JSON or YAML document, from the site's folder
    out: http

smalldocs reads OpenAPI 3.0 and 3.1. It parses the document with Python’s json, or PyYAML for YAML. It never imports FastAPI: a short program, PROGRAM, runs in the project’s environment and prints the document.

section_name

source

section_name(s: Settings) -> str

Name a section in a message, by its out.

Parameter Type Default Description
s Settings required The settings
returns str How messages name the section

kill_group

source

kill_group(proc: Popen) -> None

Kill the process and every process it started in its session, such as the Python under uv run and the app’s workers.

Parameter Type Default Description
proc Popen required A process that leads its own session

killed_on_stop

source

killed_on_stop(proc: Popen) -> Iterator[None]

Kill the process’s session when SIGTERM or SIGHUP would end the build, then end it as the signal asks.

Parameter Type Default Description
proc Popen required A process that leads its own session, which a signal to the build does not reach
returns Iterator[None] The block in which a stop kills the process first

stderr_tail

source

stderr_tail(text: str) -> str

Give the end of standard error for a message, short enough to read.

Parameter Type Default Description
text str required What a command printed on its standard error
returns str Its last 12 lines, each cut to 500 characters

run_tool

source

run_tool(s: Settings, words: list[str], env: dict[str, str], what: str) -> tuple[str, str]

Run a command in the root, with no shell, and stop the build when it fails, with the end of its standard error.

Parameter Type Default Description
s Settings required The settings
words list[str] required The command and its arguments
env dict[str, str] required More environment variables
what str required How the message names the command
returns tuple[str, str] What it printed on its standard output, and the end of its standard error

document_text

source

document_text(s: Settings) -> tuple[str, str, str]

Read the document from its input: the file, the output of the command, or the output of the FastAPI program.

Parameter Type Default Description
s Settings required The settings
returns tuple[str, str, str] The document’s text, how messages name where it came from, and the end of the command’s standard error

parse

source

parse(s: Settings, text: str, what: str, said: str = '') -> dict

Parse the document as JSON, or else as YAML, and refuse what is not OpenAPI 3.0 or 3.1.

Parameter Type Default Description
s Settings required The settings
text str required The document’s text
what str required Where it came from, for messages
said str '' The end of the command’s standard error, which may say why it gave no document
returns dict The document

size

source

size(s: Settings, data: object) -> int

Refuse a document that holds itself, or that YAML’s aliases make larger than LIMIT values, before a page repeats it.

Parameter Type Default Description
s Settings required The settings
data object required The document, as parsed
returns int How many values it holds, each alias of YAML counted as often as it is used

json_text

source

json_text(value: object) -> str

Write a value as JSON, as the document gives it.

Parameter Type Default Description
value object required A value from the document, such as a default
returns str It as JSON on one line

natural_id

source

natural_id(method: str, path: str) -> str

Make an operation’s id from its method and its path: no braces, a - for each run of other signs, in lower case.

Parameter Type Default Description
method str required The method, such as get
path str required The path, such as /api/players/{player_id}
returns str The id, such as get-api-players-player_id

file_name

source

file_name(tag: str) -> str

Name a tag’s page by the rule of operation ids.

Parameter Type Default Description
tag str required A tag’s name, such as Head to Head
returns str Its page’s name, such as head-to-head, or tag when it has no letter or digit

class Operation

source

Operation(method: str, path: str, node: dict, item: dict, id: str = '', page: str = '')

One method on one path, and where its entry goes.

Parameter Type Default Description
method str required The method, in lower case, such as get
path str required The path, such as /api/players/{player_id}
node dict required The operation’s object in the document
item dict required The path item that holds it, for the parameters of the path
id str '' Its id on the page, unique in the section
page str '' Its page, such as players.qmd

class Group

source

Group(title: str, description: str, ops: list[Operation], page: str = '')

The operations of one tag, which share one page.

Parameter Type Default Description
title str required The tag’s name, or default for operations with no tag
description str required The tag’s description, from the document’s tags
ops list[Operation] required Its operations, in the document’s order
page str '' Its page, such as players.qmd

class Document

source

Document(s: Settings, data: dict)

An OpenAPI document, with its $refs followed as they are read.

Parameter Type Default Description
s Settings required The settings
data dict required The document

Document.fail

source

Document.fail(message: str) -> ValueError

Make the error that stops the build.

Parameter Type Default Description
message str required What is wrong
returns ValueError The error, naming the section

Document.pointer

source

Document.pointer(ref: str) -> object

Follow a JSON pointer inside the document; a $ref to another file stops the build.

Parameter Type Default Description
ref str required A $ref, such as #/components/parameters/limit
returns object What it points at

Document.named

source

Document.named(node: object) -> str | None

Tell whether a node is a link to a named schema.

Parameter Type Default Description
node object required Part of the document
returns str | None The name of the schema when it is a $ref to a named schema; None otherwise

Document.deref

source

Document.deref(node: object, chain: tuple[str, ...] = ()) -> tuple[object, tuple[str, ...]]

Follow each $ref that is not a named schema; one already on the chain is a loop, which stops the build.

Parameter Type Default Description
node object required Part of the document, which may be a $ref
chain tuple[str, …] () The $refs followed to reach it
returns tuple[object, tuple[str, …]] What it stands for, and the chain with its own $refs; a link to a named schema stays as it is

Document.type_of

source

Document.type_of(node: object, chain: tuple[str, ...] = (), depth: int = 0) -> str

Write a schema’s type: string (date-time), array of Player, string or null.

Parameter Type Default Description
node object required A schema
chain tuple[str, …] () The $refs followed to reach it
depth int 0 How deep it is
returns str Its type in a short form, as Markdown: plain text, with a link for each named schema

Document.base_type

source

Document.base_type(node: dict, chain: tuple[str, ...], depth: int) -> str

Write the type of a schema from its own keys: const, enum, anyOf, oneOf, allOf or type.

Parameter Type Default Description
node dict required A schema that is not a $ref
chain tuple[str, …] required The $refs followed to reach it
depth int required How deep it is
returns str Its type, as Markdown

Document.one_type

source

Document.one_type(kind: str, node: dict, chain: tuple[str, ...], depth: int) -> str

Write one JSON type of a schema: an array of its items, a map of its values, or a type with its format.

Parameter Type Default Description
kind str required One JSON type, such as array
node dict required The schema
chain tuple[str, …] required The $refs followed to reach it
depth int required How deep it is
returns str The type, as Markdown

Document.fields

source

Document.fields(node: object, chain: tuple[str, ...] = (), prefix: str = '', depth: int = 1) -> list[tuple[str, dict, bool, tuple[str, ...]]]

List the fields of an object, and the fields of an inline object in a field, as stats.runs, to a depth of three.

Parameter Type Default Description
node object required An object schema
chain tuple[str, …] () The $refs followed to reach it
prefix str '' The dotted name of the field that holds it, such as stats.
depth int 1 How deep the fields are; three at most
returns list[tuple[str, dict, bool, tuple[str, …]]] Each field: its dotted name, its schema, whether it is required, and its chain

Document.description

source

Document.description(node: object, chain: tuple[str, ...] = ()) -> str

Read a description.

Parameter Type Default Description
node object required A schema, a parameter or a response
chain tuple[str, …] () The $refs followed to reach it
returns str Its description, or that of what its $ref stands for; empty when there is none

Document.field_table

source

Document.field_table(node: object, chain: tuple[str, ...] = ()) -> str

Describe an object by its fields.

Parameter Type Default Description
node object required An object schema
chain tuple[str, …] () The $refs followed to reach it
returns str A Markdown table of its fields: type, default, whether it is required, and description

Document.operations

source

Document.operations() -> list[Operation]

List the operations of the document’s paths.

Parameter Type Default Description
returns list[Operation] Each operation, in the document’s order

Document.grouped

source

Document.grouped() -> list[Group]

Put each operation in the group of its first tag, or in default when it has none, and name each group’s page.

Parameter Type Default Description
returns list[Group] One group for each tag that an operation has first, in the order of tags, then of first use

Document.source

source

Document.source(node: dict) -> str

Link an entry to the file and line that x-smalldocs-source gives.

Parameter Type Default Description
node dict required An operation or a schema
returns str Its source link, from x-smalldocs-source; empty when it has none, or its file is outside the root

Document.parameters

source

Document.parameters(op: Operation) -> list[tuple[dict, tuple[str, ...]]]

List an operation’s parameters; one of the operation replaces the path’s with the same name and place.

Parameter Type Default Description
op Operation required An operation
returns list[tuple[dict, tuple[str, …]]] Each parameter, with the chain of $refs to it: the path’s, then the operation’s own

Document.param_schema

source

Document.param_schema(p: dict) -> object

Find a parameter’s schema.

Parameter Type Default Description
p dict required A parameter
returns object Its schema, or that of its first media type

Document.request

source

Document.request(op: Operation) -> str

Write the request as the signature of the operation.

Parameter Type Default Description
op Operation required An operation
returns str The HTTP request in a code block: the path, the query with its defaults, the headers and the cookies

Document.params_table

source

Document.params_table(op: Operation) -> str

Describe an operation’s parameters.

Parameter Type Default Description
op Operation required An operation
returns str A Markdown table of its parameters: place, type, default, whether it is required, and description

Document.body

source

Document.body(op: Operation) -> list[str]

Describe an operation’s request body.

Parameter Type Default Description
op Operation required An operation
returns list[str] The paragraphs of its request body: a line of its media types and types, its description, and the table of an inline object’s fields

Document.responses

source

Document.responses(op: Operation) -> str

Describe an operation’s responses.

Parameter Type Default Description
op Operation required An operation
returns str A Markdown table of its responses: one row for each status and media type

Document.operation_entry

source

Document.operation_entry(op: Operation) -> str

Write one operation’s entry.

Parameter Type Default Description
op Operation required An operation
returns str Its entry: a heading with an id, a source link, the request, the summary and description, and the tables

Document.schema_entry

source

Document.schema_entry(name: str) -> str

Write one schema’s entry.

Parameter Type Default Description
name str required A schema’s name
returns str Its entry: a heading with an id, a source link, its description, and its type or the table of its fields

wrap

source

wrap(text: str) -> str

Keep array of (string or null) apart from array of string, or null.

Parameter Type Default Description
text str required A type
returns str It in parentheses when it joins types with “or” or “and”

load

source

load(s: Settings) -> Document

Read the document from its input, and refuse one with no operations.

Parameter Type Default Description
s Settings required The settings
returns Document The document, read

paragraphs

source

paragraphs(text: str) -> tuple[str, str]

Split a description into its summary and the rest.

Parameter Type Default Description
text str required A description, in Markdown
returns tuple[str, str] Its first paragraph on one line, for the front matter, and the rest

readable

source

readable(s: Settings, work: object) -> object

Run a step that reads the document, and turn a value of the wrong kind, such as a list where OpenAPI has a mapping, into an error that names the section.

Parameter Type Default Description
s Settings required The settings
work object required A function of the settings that reads the document, write_pages or gaps
returns object What it gives

build

source

build(s: Settings) -> list[Path]

Write a page for each tag, a page of schemas, the folder’s index, and the names for links.

Parameter Type Default Description
s Settings required The settings
returns list[Path] The pages written

write_pages

source

write_pages(s: Settings) -> list[Path]

Write the pages of the document.

Parameter Type Default Description
s Settings required The settings
returns list[Path] The pages written

response_has_schema

source

response_has_schema(doc: Document, r: object) -> bool

Tell whether a response gives the shape of its body.

Parameter Type Default Description
doc Document required The document
r object required A response
returns bool Whether some media type of it has a schema that says something: not {}, nor an object with no fields

check

source

check(s: Settings, shown: bool = False) -> int

Print how much of each tag and of the schemas is documented, then every gap, the parameters with no description by name, and the total.

Parameter Type Default Description
s Settings required The settings
shown bool False Hold only what the pages show; the pages show every operation and schema, so this changes nothing
returns int The exit code: 0 when nothing is missing, 1 otherwise

gaps

source

gaps(s: Settings) -> int

Hold the document to the convention, and print what it finds.

Parameter Type Default Description
s Settings required The settings
returns int The exit code: 0 when nothing is missing, 1 otherwise