Skip to content

refactor(api-v1): write a request struct, its reader and its schema from one line a field - #261

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

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

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

The four request structs of /api/v1 are now written by one macro, request!, from one name: Type line a field. It follows the request-parser series (#228, #235, #242, #247, #250), which made every request body a typed struct that the hand parser fills and the document derives from. Each struct still said two things by hand that had to agree: the schema attribute on each field, and the reader that each field is read by. Part of #58, which is closed; this PR does not reopen it.

What changed

  • request! (fields.rs) replaces read_into!. From name: Type lines it writes the struct (every field an Option; Debug, Default and, with the openapi feature, ToSchema), #[schema(inline, nullable = false)] on every field, and read_fields, which reads each field from the key of its name by the reader of its type, in the order the fields are written (the order the errors are filed in). A field whose name is not its key (type, a Rust keyword) says type_ as "type", and the macro writes the schema's rename and the key it reads from out of that one word.
  • Field (fields.rs) is the trait that gives a type its reader and what it refuses with (String, or a whole Value for a list whose items are wrong one by one). bool and f64 implement it with bool_field and float_field. The 13 value types of values.rs, Group, KeyList and PriorityValue move their read out of an inherent impl into impl Field for, the body unchanged.
  • The four structs (PatchedStateRequest, PatchedLabelCreateUpdateRequest, PatchedProjectUpdateRequest, PatchedIssueRequest) are request! calls: 60 fields that said #[cfg_attr(feature = "openapi", schema(inline, nullable = false))] by hand no longer do. The Work Items struct lists its fields in the order they were read, which was not the order it declared them; the document sorts its properties, so the order shows nowhere else.
  • Not written by the macro, because it differs by resource and does not follow from a field line: REQUIRED and CHANGE_ONLY, the stages after the reading (check_rows, validate), and the From into the core's patch.
  • status.md G-25 item 6 says what the macro writes and what it does not; the two sentences that described the attribute as repeated by hand are gone.

The wire is unchanged

  • tools/oracle-v1/schema/openapi.thrust.json regenerates byte-identical; scripts/check-openapi.sh exits 0 (32 operations, 23 schemas) with its controls.
  • thrust-api-v1 runs the same 679 tests with the openapi feature and 653 without, all passing, no leak. The doctest of the macro is ignore, as the one of read_into! was.
  • Each of the four structs reads its fields in the same order as on main (a script compares the read_into! list of origin/main with the request! fields), so the errors are filed in the same order.
  • scripts/oracle-v1.sh native-memory controls is PASS (gate: 25 operations, 162 cases, 12 failures all accepted, 0 unaccepted, 0 stale; corpus: 112 exchanges, 15 accepted, 0 unaccepted, 0 stale; the controls pass) and scripts/contract.sh native-memory main is 71 of 71.
  • All 22 steps of the deps job (my local list was stale again: it lacked check-ci-index.sh and actionlint; I compared it with ci.yml mechanically this time, and its one remaining difference is CI's download of actionlint, which cannot run here), cargo fmt --check, clippy for the workspace and for thrust-api-v1 with the feature, clippy for the Worker's wasm32 target, cargo metadata --locked.

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

  1. nullable = false taken out of the macro: 9 conformance tests fail (the four resources, creation and change) and the committed document goes stale, in all four at once. The same edit before was 60 edits.
  2. The rename taken out: 2 conformance tests fail and the document says type_.
  3. The key read from the field's name and not from as "type": 13 Work Items tests fail (a_type_by_its_model_name and the rows that name a type), the document unchanged. The reading and the schema are tied by different tests.

Not checked

  • The Worker bundle size (the feature is off there; Field is a trait with a few impls).
  • The book build of the docs job (mdbook-mermaid is not installed on this machine); the edit adds no page or link.
  • The whole CI run.

Refs: #58

🤖 Generated with Claude Code

…rom one line a field

A request struct was written in two places that had to agree: the struct, with `#[cfg_attr(feature = "openapi", schema(inline,
nullable = false))]` on each of its fields (60 of them, in the four resources), and the `read_into!` list that named the reader of
each field. `request!` takes one `name: Type` line a field and writes both.

- `fields.rs`: the `Field` trait (a type's reader, and what it refuses with), implemented for `bool` and `f64`, and `request!` in
  place of `read_into!`. It writes the struct (every field an `Option`, `Debug`, `Default` and, with the feature, `ToSchema`), the
  schema attribute on every field, the `rename` of a key that is not the field's name (from the same `as "type"` the reader takes
  the key from), and `read_fields`, which reads each field from the key of its name by the reader of its type, in the order the
  fields are written, which is the order the errors are filed in.
- `values.rs`, `states.rs`, `work_items.rs`: the reader of each type (13 values, `Group`, `KeyList`, `PriorityValue`) moves out of
  its inherent impl into `impl Field for`, body unchanged.
- `states.rs`, `labels.rs`, `projects.rs`, `work_items.rs`: the four request structs are `request!` calls. The Work Items struct
  lists its fields in the order they were read, which was not the order it declared them; the document sorts its properties, so
  the order shows nowhere else.
- Not written by the macro, because it differs by resource: `REQUIRED` and `CHANGE_ONLY`, the stages after the reading, and the
  conversion into the core's patch.

The wire is unchanged: the regenerated `openapi.thrust.json` is byte-identical, `thrust-api-v1` runs the same 679 tests with the
feature and 653 without, and the order of the fields read matches `origin/main` for all four structs.

Controls, each one edit to the macro, reverted: without `nullable = false` 9 conformance tests fail and the document goes stale,
in all four resources at once; without the `rename` two conformance tests fail and the document says `type_`; reading the key
`type_` and not `type` fails 13 Work Items tests, with the document unchanged. `status.md` G-25 item 6 says what the macro writes and
what it does not.

Refs: #58
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@aRustyDev
aRustyDev merged commit 4cb7046 into main Nov 17, 2026
40 checks passed
@aRustyDev
aRustyDev deleted the refactor/api-v1-request-macro branch November 17, 2026 04:11
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