Repository navigation
refactor(api-v1): read a state's request into a typed struct that writes its own schema (request-parser, States) - #235
Merged
Conversation
…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]>
Merged
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, withtext(false, false)and a localGROUPSlist). 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 namePatchedStateRequest::REQUIRED.values.rs(new):Text<MIN, MAX>,NullableText<MIN, MAX>andProse. Each reads a value as DRF'sCharFielddoes, and (with theopenapifeature) writes its schema,minLengthandmaxLengthfrom the same two numbers.Grouptakes its list fromStateGroup::ALL, so the localGROUPScopy is gone.Creations(openapi.rs): the creation's schema,StateRequest, is made of the change's withPatchedStateRequest::REQUIRED, the list the parser checks. The creation and the change cannot differ in a key, a bound, a type or anull. 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 StatePatchhands the core its own patch type.choice_ofreturns what a choice parses to;choice_fieldandchoice_byare 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)
#[schema(as = ..)]no longer exists, so a schema is named after its Rust type: the parser's struct isPatchedStateRequestand the creation is a marker,StateRequest, whichCreationsfills.oneOf [T, null], whichoasdiffreads 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).Absent<T>does not satisfyComposeSchema, so presence staysOption<_>.The wire is unchanged
tools/oracle-v1/schema/openapi.thrust.jsonregenerates byte-identical (201,194 bytes), andcheck-openapi.shreports the same 103 findings, none unaccepted.openapifeature, 579 without.scripts/oracle-v1.sh native-memory controlsis PASS (gate 162 cases, 12 failures all accepted, 0 unaccepted, 0 stale; corpus 112 exchanges, 15 accepted, 0 unaccepted, 0 stale), andscripts/contract.sh native-memory mainis 71 of 71.depsjob, 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
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), anda_long_namefails.descriptionwithoutnullable = false: the document claimsoneOf [string, null], which the route refuses; both States conformance tests fail.REQUIREDshortened to["name"]: the document's list follows the parser's; the core still refuses a state with no colour, and 7 tests fail.NullableTextinverted: 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.rsare 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
values.rsis small, but I did not measure it.docsjob (mdbook-mermaidis not installed on this machine; the edits add no page or link).typekey).Next
Labels, stacked on this branch: the same pattern over
LabelCreateUpdateRequestand its change, with a relation check (parent) that stays in the route as the second stage.Refs: #58
🤖 Generated with Claude Code