Skip to content

refactor(api-v1): read a state's request into a typed struct that writes its own schema (request-parser, States) - #235

Merged
aRustyDev merged 1 commit into
mainfrom
refactor/api-v1-states-request
Nov 16, 2026
Merged

aRustyDev merged 1 commit into
mainfrom
refactor/api-v1-states-request

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

The States' request is now read into a typed struct, and the document's schema is derived from that struct. This is the go/no-go spike of the request-parser series (step 0 is #228): it shows that a bound can be written once, in a type, and serve the parser and the document alike. Part of #58, which is closed; this PR does not reopen it.

Why

The States' request was described twice: by a hand-written schema in openapi.rs (two structs, about 50 lines) and by the parser (state_patch, with text(false, false) and a local GROUPS list). Only the conformance test of #228 tied them. A bound that is written in a type cannot differ.

What changed

  • PatchedStateRequest (states.rs) is the struct the parser reads into, and the document's schema is derived from it. It is the pin's name for a body whose keys may all be left out: a change reads into it as it is, and a creation reads into it and must also name PatchedStateRequest::REQUIRED.
  • values.rs (new): Text<MIN, MAX>, NullableText<MIN, MAX> and Prose. Each reads a value as DRF's CharField does, and (with the openapi feature) writes its schema, minLength and maxLength from the same two numbers. Group takes its list from StateGroup::ALL, so the local GROUPS copy is gone.
  • Creations (openapi.rs): the creation's schema, StateRequest, is made of the change's with PatchedStateRequest::REQUIRED, the list the parser checks. The creation and the change cannot differ in a key, a bound, a type or a null. The two hand-written structs are removed.
  • fields::read_into! reads a struct from a body with the key as the field's own identifier, so the two cannot be spelled apart; From<PatchedStateRequest> for StatePatch hands the core its own patch type.
  • choice_of returns what a choice parses to; choice_field and choice_by are one line each over it (this also removes the unreachable fallback branch I left in test(api-v1): hold each request schema to its parser with bodies the schema generates (request-parser step 0) #228).

What utoipa 6 imposed (found by building it)

  • A struct-level #[schema(as = ..)] no longer exists, so a schema is named after its Rust type: the parser's struct is PatchedStateRequest and the creation is a marker, StateRequest, which Creations fills.
  • A nullable value of a type of ours is wrapped in oneOf [T, null], which oasdiff reads as a different schema. So null-ness is part of the type (NullableText), and each field is #[schema(inline, nullable = false)]. The attribute is mechanical, and forgetting it is caught (control 2).
  • A generic wrapper such as Absent<T> does not satisfy ComposeSchema, so presence stays Option<_>.

The wire is unchanged

  • tools/oracle-v1/schema/openapi.thrust.json regenerates byte-identical (201,194 bytes), and check-openapi.sh reports the same 103 findings, none unaccepted.
  • No test was added or removed: 605 with the openapi feature, 579 without.
  • On this tree: scripts/oracle-v1.sh native-memory controls is PASS (gate 162 cases, 12 failures all accepted, 0 unaccepted, 0 stale; corpus 112 exchanges, 15 accepted, 0 unaccepted, 0 stale), and scripts/contract.sh native-memory main is 71 of 71.
  • All 20 steps of the deps job, run locally on the rebased commit; clippy with and without the feature; the Worker's wasm32 clippy.

Controls (AGENTS.md rule 8), each one edit made and reverted

  1. name: Text<1, 256>: the route and both schemas move together, so the committed document goes stale (the signal that it must be updated on purpose), and a_long_name fails.
  2. description without nullable = false: the document claims oneOf [string, null], which the route refuses; both States conformance tests fail.
  3. REQUIRED shortened to ["name"]: the document's list follows the parser's; the core still refuses a state with no colour, and 7 tests fail.
  4. The blank flag of NullableText inverted: the document stays byte-identical and the conformance test catches it. So the test keeps covering the one gap typing leaves, between a type's reader and its schema.

The document-side breaks of request_conformance.rs are relative to the schema's current bound and required list now: they hard-coded 255 and 256, and a bound that changes on purpose, in one type, would have failed them.

Not checked

  • The Worker bundle size. The feature is off there and values.rs is small, but I did not measure it.
  • The book build of the docs job (mdbook-mermaid is not installed on this machine; the edits add no page or link).
  • The whole CI run.
  • Anything about the other resources: Work Items will need more kinds of value (lists of uuids, dates, a renamed type key).

Next

Labels, stacked on this branch: the same pattern over LabelCreateUpdateRequest and its change, with a relation check (parent) that stays in the route as the second stage.

Refs: #58

🤖 Generated with Claude Code

…tes its own schema

The States' request was described twice: by a hand-written schema in openapi.rs (two structs, about 50 lines) and by the parser
(`state_patch`, with `text(false, false)` and a local `GROUPS` list). Nothing but the conformance test tied them.

`PatchedStateRequest` (states.rs) is the struct the parser now reads into, and the document's schema is derived from it. Each field's
type says what it takes: `Text<1, 255>` and `NullableText<0, 255>` (values.rs) read a value as DRF's `CharField` does and write
`minLength`/`maxLength` from the same two numbers, `Prose` is a description, `Group` takes its list from `StateGroup::ALL` (the local
`GROUPS` copy is gone). A bound is written once. The creation's schema, `StateRequest`, is made of the change's by `Creations`
(openapi.rs) with `PatchedStateRequest::REQUIRED`, the list the parser checks, so a creation and a change cannot differ in a key, a
bound, a type or a `null`. `From<PatchedStateRequest> for StatePatch` hands the core its own patch type.

What utoipa 6 imposed, found by building it: a struct-level `#[schema(as = ..)]` no longer exists, so a schema is named after its Rust
type (the parser's struct is the pin's `PatchedStateRequest` and the creation is a marker, `StateRequest`, which `Creations`
fills); a nullable value of a type of ours is wrapped in `oneOf [T, null]`, so null-ness is part of the type (`NullableText`) and
each field is `#[schema(inline, nullable = false)]`; a generic wrapper such as `Absent<T>` does not satisfy `ComposeSchema`.
`fields::read_into!` reads a struct from a body with the key as the field's own identifier, so the two cannot be spelled apart.
`choice_of` returns what a choice parses to; `choice_field` and `choice_by` are now one line each over it.

Wire proof: `tools/oracle-v1/schema/openapi.thrust.json` regenerates BYTE-IDENTICAL (201,194 bytes), `check-openapi.sh` reports the same
103 findings, none unaccepted, no test was added or removed (605 with the feature, 579 without), and on this tree `oracle-v1.sh
native-memory controls` is PASS (gate 162 cases, 12 failures, all accepted; corpus 112 exchanges, 15 accepted, 0 unaccepted) and
`contract.sh native-memory main` is 71 of 71.

Controls, each one edit made and reverted: `name: Text<1, 256>` moves the route and BOTH schemas together (the committed document goes
stale, which is the signal, and `a_long_name` fails); `description` without `nullable = false` writes a `oneOf [string, null]` that the
route refuses (both States conformance tests fail); `REQUIRED` shortened to `["name"]` moves the document's list with the parser's and
the core still refuses a state with no colour (7 tests fail); the blank flag of `NullableText` inverted leaves the document
byte-identical and the conformance test catches it, so it still covers the gap between a type's reader and its schema.

The document-side breaks of request_conformance.rs are relative to the schema's current bound and required list now (they hard-coded
255 and 256, and a bound that changes on purpose, in one type, would have failed them).

Refs: #58
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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