Stop accepting category_id on the trades API
Closes #4707.
Fork pre-flight: jaysbeekay/sure#624, on the same branch at the same commit. Its CI, Gatekeeper and cubic history is there: the full review approved it with no findings.
@1steve78, you offered to take #4707 and had read the triage plan, so a review from you would be very welcome.
Problem
POST and PUT /api/v1/trades return 500 whenever the request carries a usable category_id, the family's own included. 20260114002048_remove_category_from_trades dropped trades.category_id, but the controller still permitted the field and wrote it:
- create, through
build_trade_create_options; - update, through
build_entry_params_for_update'sentryable_attributes.
The write raised, and the broad rescue turned it into a 500.
What changes
Api::V1::TradesController:category_idis removed fromtrade_paramsandtrade_update_params, from the create builder and from the update's entry attributes. A sentcategory_idis now ignored like any other unpermitted parameter.- The 422 for another family's category goes with it. Nothing is stored either way, so there is nothing to refuse.
spec/requests/api/v1/trades_spec.rbanddocs/api/openapi.yaml(regenerated) no longer offer the field on create or update.config/brakeman.ignore: the existing mass-assignment entry fortrade_params(aboutaccount_id/transfer_account_id) is re-keyed to the shorter permit list. Brakeman fingerprints include the code, so the old entry went obsolete and the warning reappeared. Nothing new is suppressed.
Left out on purpose:
- Restoring trade categories is a product decision.
Trade'sbelongs_to :categorystays; it can go in a follow-up (see below).
Blast radius
- Writers: only the two API permit lists, the create builder and the update builder. The web
TradesControllerandTrade::CreateFormtake no category. - Readers of
trade.category, each left as is. With no column, the association readsnil.
| Reader | Effect, before and after |
|---|---|
app/views/api/v1/trades/_trade.json.jbuilder:29 | Renders "category": null; the new tests assert the key is present and null |
ReportsController (trade.category in the category breakdown) | Counts trades as uncategorized, as before |
TradesController#index includes(:category) | Preloads nothing, as before |
- Clients: API clients that send
category_idon a trade get a 500 today, so none can depend on that result. After this change they get 201 or 200 with the rest of the request saved.
Tests
test/controllers/api/v1/trades_controller_test.rb. The old rejects an inaccessible category without persisting a trade test, which pinned the 422, is replaced by:
- create ignores a category_id and saves the trade: an own, a foreign and an unknown
category_ideach return 201, save one entry and trade with the request's qty and price, and render"category": null. - update ignores a category_id and saves the other fields: the same three each return 200 and save the notes sent with them.
- an update carrying only a category_id changes nothing: 200, and the entry's attributes are unchanged.
Failed first, on main:
- Create with the family's own category saved nothing (the 500 rolled it back).
- Both update tests got a 500.
Mutations, each caught by a named test:
| Mutation | Caught by |
|---|---|
| Restore the create write | the create test |
| Restore the update write | both update tests |
| Refuse only a foreign category, as before | the create test's foreign case |
Re-adding :category_id to a permit list on its own changes nothing: both lists are read field by field, so the writes are what the mutations target.
Validation
bin/rails test: 14,315 runs, 92,605 assertions, 0 failures, 0 errors, 37 skipstest/controllers/api/v1/trades_controller_test.rb: 59 runs, 0 failuresrake rswag:specs:swaggerize: 450 examples, 0 failures;openapi.yamlchanges only by the two removedcategory_idfields- RuboCop: no offenses on the changed Ruby files. Brakeman: 0 warnings, no obsolete ignore entries.
- No UI, migration or JS change.
Follow-up
belongs_to :category on Trade, the jbuilder's category block, the reports reader and index's includes(:category) all read a column that no longer exists. Removing them changes the API's JSON (category disappears from a trade) and the reports code, so it belongs in its own change if the maintainers want it.
Review round, 2026-11-06 (head 0cd28fa29)
- 903560543:
updaterunslock_saved_attributes!,mark_user_modified!andsync_account_later, in the same order, only when the entry or its trade has saved changes. A category-only request now changes nothing and triggers none of them. Tests:- The no-op test compares every Entry and Trade attribute, and asserts no SyncJob,
user_modifiedfalse and no locked attributes. It failed before: "0 jobs expected, but 1 were enqueued". - A positive test with an entry-only change (notes) and a trade-only change (activity label).
- Four gate mutations (always true, always false, each half only) are each caught.
- The web
TradesController#updateis unchanged.
- The no-op test compares every Entry and Trade attribute, and asserts no SyncJob,
- 0cd28fa29: every update test asserts
:ok. - Full suite on 903560543:
14316 runs, 0 failures, 0 errors, 37 skips. Trades controller tests on 0cd28fa29 (test-only change): 60 runs, 0 failures. RuboCop: no offenses. Brakeman: 0 warnings.
Review round, 2026-11-11 (head 6e78ed252)
- Scope: the gate covers every update that changes nothing, not only a category-only one. Re-sending a trade's current values no longer marks the entry user-modified or enqueues a sync.
- 6e78ed252:
updateand its follow-up writes run in oneEntry.transaction, asApi::V1::TransactionsController#updatedoes. If marking, locking or the sync fails, the edit rolls back, so a retry still finds the change and completes it. The new test fails without the wrap: the edit stays saved without its follow-ups. - The unused
let(:category)is gone from the trades request spec. build_trade_create_optionscan now only return the labelTrade::CreateFormalready sets, so it andtrade_from_created_modelcan shrink. That's left for a follow-up, to keep this change to the bug.- Full suite: 14,836 runs, 0 failures. RuboCop clean, Brakeman 0 warnings.
Summary by cubic
Stops accepting category_id on POST and PUT /api/v1/trades. The column was dropped but the controller still permitted and wrote the field, so any usable value, including the family's own, became a 500. A sent category_id is now ignored like any other unknown parameter.
Trades API updates now run their follow-up side effects (lock_saved_attributes!, mark_user_modified!, sync_account_later) only when the entry or trade has saved changes, and the update plus those follow-ups commit together so a failed follow-up rolls back the edit.
Behavior
- The 422 for another family's category is gone, since nothing is stored either way.
- Trade JSON keeps rendering
"category": null; reports still count those trades as uncategorized. - Docs, request specs, and update tests no longer offer the field; the Brakeman ignore entry for
trade_paramsis re-keyed.
Summary by CodeRabbit
- API Changes
- Trade creation and update requests no longer accept category IDs; supplied category IDs are ignored.
- Other supported fields can still be created or updated when a category ID is supplied. Category-only updates leave trade entry attributes unchanged.
- API documentation reflects the updated request fields.
- Bug Fixes
- Trade updates mark entries as modified and synchronize account data when supported trade or entry fields change.
- Failed updates roll back changes and return validation errors.