Skip to content

refactor(api-v1): read a work item's request into a typed struct that writes its own schema (request-parser, Work Items) - #250

Merged
aRustyDev merged 1 commit into
mainfrom
refactor/api-v1-work-items-request
Nov 17, 2026
Merged

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

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

The Work Items' request is now read into a typed struct, and the document's schema is derived from that struct: the last of the four resources of the request-parser series, after the States (#235), the Labels (#242) and the Projects (#247). With this, no request body of /api/v1 is described by a hand-written copy. Part of #58, which is closed; this PR does not reopen it.

What changed

The same pattern as #235, #242 and #247, for IssueRequest and PatchedIssueRequest:

  • PatchedIssueRequest (work_items.rs) is the struct the parser reads into, and the document's schema is derived from it. IssueRequest is the creation's marker, filled by Creations with PatchedIssueRequest::REQUIRED. The two hand-written structs in openapi.rs (about 100 lines), request_sequence_id and the local PRIORITIES list are removed: PriorityValue takes its choices from Priority::ALL and writes the pin's allOf of the enum by reference, as before.
  • New kinds of value: KeyList (the people and the labels; an item that is wrong is filed by its place, {"0": [message]}), NullableDate, NullableDateTime, NullableWhole<MIN, MAX> (the point) and SentText<MIN, MAX> (the name as sent and as kept: the activity quotes both). fields::read now takes a reader that fails with a message or with a whole answer (Refusal), so one function serves both.
  • A key that is not the field's name: type is a Rust keyword, so the field is type_ and the macro says type_ as "type" beside the schema's rename = "type".
  • Three stages, as the Projects': read (every key, no database), check_rows (the people, the labels, the parent, the state, and the keys that name a type or an estimate that no project has), then one refuse_if_any, then validate() (the dates, the description as HTML, the assignees' roles). The fields the route reads and does not keep (sequence_id, sort_order, archived_at, is_draft) are typed fields with an allow(dead_code) and the reason, as is_triage was in the States.

Two numbers that were written in more than one place

As the months were in #247, both found by changing a bound in the struct alone:

  • MAX_WORK_ITEM_POINT (thrust-contracts): the struct, the memory adapter's two checks (work_items_point_valid) and the document. The Postgres column's CHECK (migration 0015) says the same number in SQL, which no constant reaches; the constant's doc comment says so.
  • MAX_NAME_CHARS (thrust-core): validate_name, its refusal text (name must be 1..=255 characters) and the struct.

Changing either constant once moves all of its places, and no conformance test fails; only the wire test that hard-codes the old number does, as a wire test should. These are the only changes outside thrust-api-v1: two constants and the lines that read them.

A property that had no test, now pinned

a_key_that_names_no_row_is_reported_beside_the_other_fields_that_are_wrong (48 cases): six kinds of row refusal (parent, state, created_by, type, an assignee, a label) by four wrong fields, on a creation and on a change. With the first stage refusing before the rows are checked, exactly these 48 fail and the 92 other work item tests pass.

The wire is unchanged

  • tools/oracle-v1/schema/openapi.thrust.json regenerates byte-identical, so check-openapi.sh is unchanged (103 findings, none unaccepted).
  • 679 tests with the openapi feature and 653 without (the 48 new cases; none removed); thrust-contracts, thrust-core and thrust-adapters-memory with them: 2174 passed.
  • scripts/oracle-v1.sh native-memory controls is PASS on the tree replayed onto main at 57f06d4 (gate 162 cases, 12 failures all accepted, 0 unaccepted, 0 stale; corpus 112 exchanges, including the 36 for work items, 15 accepted, 0 unaccepted, 0 stale; the controls pass) and scripts/contract.sh native-memory main is 71 of 71 (nextest exit=0 passed=71 expected=71). main gained only CI scripts and the mutants harness (ci(gate): teach the planner and the gate about called workflows, and run the mutants in ci-scripts (stage 0.3) #245, fix(mutants): tell a row that did not run from one that survived #248) since the commit was built, and git range-diff shows it unchanged.
  • All 20 steps of the deps job, fmt, clippy for the four crates touched with and without the feature, and for the Worker's wasm32 target.

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

  1. point: NullableWhole<0, 13> in the struct alone: the conformance test fails ("the schema allows 13, the route refused: 500") and the committed document goes stale. The same edit made once to MAX_WORK_ITEM_POINT moves the struct, the adapter and the document together.
  2. The priority parser narrowed so that none is refused, the document unchanged: 5 tests fail (both conformance tests, each_priority_the_document_lists_is_taken_and_answered_and_no_other_is, order_by_follows_the_pins_allowlist, and the document-side control).
  3. priority without nullable = false: the document wraps the pin's allOf in a oneOf, and it is caught from both sides (the conformance tests and the stale document).
  4. The type key mis-spelt in the parser: a_type_by_its_model_name and my new test's type rows fail.
  5. The name's bound in the struct alone: a_long_name, the conformance test and the stale document.
  6. The stage order, above.

Not checked

  • The Worker bundle size (the feature is off there; values.rs grows by a few more small types).
  • The book build of the docs job (mdbook-mermaid is not installed on this machine; I rewrote G-25 item 6, and the edits add no page or link).
  • The whole CI run.

What is left of the series

Nothing is hand-written on the request side now. G-25 item 6 says what the conformance test cannot see: three refusals the schema cannot say (KNOWN), seven keys that name a row the world has none of (UNSAMPLED), and a field whose schema is any value (icon_prop), where a forgotten nullable = false is seen only by the stale document and oasdiff. The attribute #[schema(inline, nullable = false)] is now written on 60 fields (9 States, 7 Labels, 23 Projects, 21 Work Items); a request! macro that writes it would remove the chance to forget it, and would be its own PR.

Refs: #58

🤖 Generated with Claude Code

… writes its own schema

The last of the four, after the States', the Labels' and the Projects'. `PatchedIssueRequest` (work_items.rs) is the struct the parser
reads into and the document's schema is derived from it; `IssueRequest` is the creation's marker, filled by `Creations` with
`PatchedIssueRequest::REQUIRED`. The two hand-written structs in openapi.rs (about 100 lines) and `request_sequence_id` are gone, and
so is the local `PRIORITIES` list: `PriorityValue` takes its choices from `Priority::ALL`, and writes the pin's `allOf` of the enum by
reference, as before.

What the work items added to the pattern, in values.rs and fields.rs:

- Lists: `KeyList` (the people and the labels), whose items are refused by their place (`{"0": [message]}`). `fields::read` now takes a
  reader that fails with a message or with a whole answer (`Refusal`), so one reader serves both.
- Dates: `NullableDate` (keeps the text as it was sent, which the activity quotes), `NullableDateTime` (only whether it was given:
  `deleted_at` is read, checked and not stored), `NullableWhole<MIN, MAX>` (the point), `SentText<MIN, MAX>` (the name, as sent and as kept).
- A key that is not the field's name. `type` is a Rust keyword, so the field is `type_` and `read_into!` says `type_ as "type"`
  beside the schema's `rename = "type"`; a mis-spelt key fails the wire tests and the `type` rows of the new test.
- Three stages, as the Projects': `read` (every key, no database), `check_rows` (the people, the labels, the parent and the state, and
  the keys that name a type or an estimate that no project has), then one `refuse_if_any`, then `validate()` (the dates, the
  description as HTML, the assignees' roles). The fields the route reads and does not keep (`sequence_id`, `sort_order`,
  `archived_at`, `is_draft`) are typed fields with an `allow(dead_code)` and the reason, as `is_triage` was.

Two numbers that were written in more than one place are one now, as the months were:

- `MAX_WORK_ITEM_POINT` (thrust-contracts): the struct's `NullableWhole<0, { .. }>`, the memory adapter's two checks (`work_items_point_valid`)
  and the document. The Postgres column's `CHECK` (migration 0015) says the same number in SQL, which no constant reaches; the
  constant's doc comment says so.
- `MAX_NAME_CHARS` (thrust-core): `validate_name` and its refusal text ("name must be 1..=255 characters") and the struct's
  `SentText<1, { .. }>`. Changing it once moved all three and no conformance test failed; only `a_long_name`, which hard-codes 255 as a
  wire test should, did.

A property with no test is pinned: `a_key_that_names_no_row_is_reported_beside_the_other_fields_that_are_wrong` (48 cases: six kinds
of row refusal by four wrong fields, on a creation and on a change, a list refused by the place of its item). With the first stage
refusing before the rows are checked exactly these 48 fail and the 92 other work item tests pass.

Wire proof: tools/oracle-v1/schema/openapi.thrust.json regenerates BYTE-IDENTICAL; 679 tests with the feature and 653 without
(+48, none removed); contracts, core and the memory adapter 2174; oracle-v1 and contract.sh as in the PR.

Controls, each one edit made and reverted: `point` to `NullableWhole<0, 13>` (the struct alone) makes the conformance test fail with
"the schema allows 13, the route refused: 500" and the document go stale, and the same edit to the constant moves the struct, the adapter
and the document together; the priority parser narrowed so that `none` is refused, with the document unchanged, fails 5 tests (the
two conformance tests, `each_priority_the_document_lists_is_taken_and_answered_and_no_other_is`, `order_by_follows_the_pins_allowlist`
and the document-side control); `priority` without `nullable = false` writes a `oneOf` around the pin's `allOf` and is caught from both
sides (conformance and the stale document); the name's bound in the struct alone fails `a_long_name`, the conformance test and the
stale document.

Refs: #58
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@aRustyDev
aRustyDev merged commit a2ab8f5 into main Nov 17, 2026
39 checks passed
@aRustyDev
aRustyDev deleted the refactor/api-v1-work-items-request branch November 17, 2026 02:38
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