Skip to content

Port reflectapi-ex features and enhance ResponseError trait checks - #265

Merged
avkonst merged 9 commits into
mainfrom
andrey/core-wire-types
Nov 25, 2026
Merged

avkonst merged 9 commits into
mainfrom
andrey/core-wire-types

Conversation

@avkonst

@avkonst avkonst commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Summary

New wire types, an opt-in conventions module, a ResponseError trait replacing StatusCode, and stricter build checks. The guide gains an API design standard written as numbered rules, with lints that check a schema against it. Most of this was proven in a downstream extension crate first, and is ported here so services can share it.

Wire types

  • Bounded<T, MIN, MAX> and BoundedMax<T, MAX>: a string or collection with a checked length. A request with another length is refused with 400 before the handler runs, and every client checks the bounds before sending:
    • TypeScript: a branded type, made with bounded() or bounded.try().
    • Python: Pydantic min_length/max_length.
    • Rust client: reflectapi::Bounded itself.
    • OpenAPI: length keywords.
  • Raw<T> (feature json): a T kept as the JSON it arrived as, to store or forward unchanged, with peek to read a few fields. Under MessagePack it is converted at the edge.
  • Encoded<T> (feature json): a typed value carried as an opaque string, such as a page cursor: JSON, then base64url. Encoded, not secret.
  • Empty derives Copy and Hash.
  • Fixed-size arrays are checked in every client: Python, OpenAPI, and TypeScript's fixedSizeArray().

reflectapi::conventions (feature conventions)

  • pagination: PageRequest { cursor, limit }, ForwardPaginatedList, NonPaginatedList, all keeping their items under items. The cursor can be typed, PageRequest<Encoded<Cursor>>, while clients still see a string.
  • identity: Identity<I> and IdentityData<I, D>.
  • conflict: an if clause on writes, {"if": {"exists": "replace"}, …}.
    • A route takes Conditional<T, If> through aliases such as InsertOrElse, UpdateOrElse and RemoveOrElse.
    • Its actions come as one set from a catalogue of 23 in conflict::actions.
    • A …OrElseRequired twin of each alias serves the sets without a default.
    • Idempotent marks the sets safe to process twice.
    • conflict::matching adds conditions on existence or edition.
  • lints: one function per rule of the design standard, for Builder::validate, or all of them through lints::ALL. Each error names its rule in the new ValidationError::rule field and links to it. What they check:
    • Call names: form, kebab-case, two resource levels, a version as a suffix, no create/delete/upsert, no search-by-….
    • Bodies: requests, responses and errors are objects; enums with fields and error types are written in kind; fields and enum values in snake_case.
    • Headers: names in kebab-case, no x- prefix.
    • Type names: a type named as a call's request, response or error names that call; no Enum or Struct suffix; at most two modules; no models or errors module.

Breaking changes

  • A route's error implements ResponseError, and derives it with #[derive(reflectapi::ResponseError)]. It provides status() and headers(), with Output + Serialize as supertraits. The derive refuses, at compile time, statuses a route's error must not use.
    • #[derive(reflectapi::StatusCode)] becomes #[derive(reflectapi::ResponseError)], with the same attributes.
    • An impl StatusCode that overrides response_headers becomes an impl ResponseError with status and headers.
  • StatusCode is deprecated. An impl of it without response_headers still compiles, with a warning. It is removed in the next breaking release, and Box<E> then forwards ResponseError as Arc<E> does.
  • build() refuses schemas whose clients would lose data or fail to compile:
    • two untagged unit variants in one enum;
    • an untagged unit variant inside an Option;
    • a reference through a shadowed module name.

Fixes

  • TypeScript and Rust codegen fill a built-in type's parameters in one pass, so N is no longer found inside a type named Node.
  • A schema with a NonZeroU8…NonZeroU128 and no plain integer of its width no longer crashes OpenAPI generation.
  • The schema leaves out type parameters it doesn't use, along with the types only they reached.

Docs

  • API Design Standard (docs/src/design/standard.md): 156 rules, each a MUST, SHOULD or MAY statement with a stable id such as api-filter-bounds-together.
    • The id convention is written in "How to read it".
    • Examples, reasoning and "With reflectapi" notes are folded under each rule. Each rule a lint checks names its lint.
    • The docs build fails on a malformed rule, an id that breaks the convention, or a lint and rule that don't name each other.
  • Convention Types page: pagination and conflict-resolution examples as doctests, and the table of lints.
  • The Rust Forge talk: its transcript with all 14 slides.
  • Landing page: a section for the standard, with a preview of its "At a glance" graphic.
  • Site: mdBook 0.5 (the theme scripts and CSS follow its new ids). docs/serve.py serves the whole site locally with live reload. llms.txt points agents to the standard.
  • Proposals: the plans move to docs/proposals/.

Tests

  • New tests cover the conventions types, Raw and Bounded in both formats, ResponseError, the build checks, and each lint against a route that breaks its rule and an API that follows them all.
  • Snapshots and the demo clients are regenerated.
  • cargo test --workspace --all-features, clippy, the book's doctests and the docs build all pass.

Notes for review

  • About half the diff is snapshots, generated demo clients, the benchmark under bench/formats/ and the standard's text. The code to review is mostly bounded.rs, raw.rs, encoded.rs, transcode.rs, conventions/, builder/result.rs, builder/mod.rs and codegen/.
  • The docs.rs links to the conventions, ResponseError and Encoded will 404 until this is released.

@avkonst
avkonst requested a review from hardbyte November 25, 2026 10:56

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@avkonst
avkonst merged commit 546930f into main Nov 25, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant