Repository navigation
refactor(api-v1): write a request struct, its reader and its schema from one line a field - #261
Merged
Merged
Conversation
…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]>
Merged
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 four request structs of
/api/v1are now written by one macro,request!, from onename: Typeline 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) replacesread_into!. Fromname: Typelines it writes the struct (every field anOption;Debug,Defaultand, with theopenapifeature,ToSchema),#[schema(inline, nullable = false)]on every field, andread_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) saystype_ as "type", and the macro writes the schema'srenameand 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 wholeValuefor a list whose items are wrong one by one).boolandf64implement it withbool_fieldandfloat_field. The 13 value types ofvalues.rs,Group,KeyListandPriorityValuemove theirreadout of an inherent impl intoimpl Field for, the body unchanged.PatchedStateRequest,PatchedLabelCreateUpdateRequest,PatchedProjectUpdateRequest,PatchedIssueRequest) arerequest!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.REQUIREDandCHANGE_ONLY, the stages after the reading (check_rows,validate), and theFrominto the core's patch.status.mdG-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.jsonregenerates byte-identical;scripts/check-openapi.shexits 0 (32 operations, 23 schemas) with its controls.thrust-api-v1runs the same 679 tests with theopenapifeature and 653 without, all passing, no leak. The doctest of the macro isignore, as the one ofread_into!was.main(a script compares theread_into!list oforigin/mainwith therequest!fields), so the errors are filed in the same order.scripts/oracle-v1.sh native-memory controlsis 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) andscripts/contract.sh native-memory mainis 71 of 71.depsjob (my local list was stale again: it lackedcheck-ci-index.shand actionlint; I compared it withci.ymlmechanically 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 forthrust-api-v1with 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
nullable = falsetaken 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.renametaken out: 2 conformance tests fail and the document saystype_.as "type": 13 Work Items tests fail (a_type_by_its_model_nameand the rows that name atype), the document unchanged. The reading and the schema are tied by different tests.Not checked
Fieldis a trait with a few impls).docsjob (mdbook-mermaidis not installed on this machine); the edit adds no page or link.Refs: #58
🤖 Generated with Claude Code