Stop accepting category_id on the trades API

Merged
JA
24d ago
+169-58

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's entryable_attributes.

The write raised, and the broad rescue turned it into a 500.

What changes

  • Api::V1::TradesController: category_id is removed from trade_params and trade_update_params, from the create builder and from the update's entry attributes. A sent category_id is 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.rb and docs/api/openapi.yaml (regenerated) no longer offer the field on create or update.
  • config/brakeman.ignore: the existing mass-assignment entry for trade_params (about account_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's belongs_to :category stays; 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 TradesController and Trade::CreateForm take no category.
  • Readers of trade.category, each left as is. With no column, the association reads nil.
ReaderEffect, before and after
app/views/api/v1/trades/_trade.json.jbuilder:29Renders "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_id on 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_id each 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:

MutationCaught by
Restore the create writethe create test
Restore the update writeboth update tests
Refuse only a foreign category, as beforethe 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 skips
  • test/controllers/api/v1/trades_controller_test.rb: 59 runs, 0 failures
  • rake rswag:specs:swaggerize: 450 examples, 0 failures; openapi.yaml changes only by the two removed category_id fields
  • 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: update runs lock_saved_attributes!, mark_user_modified! and sync_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_modified false 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#update is unchanged.
  • 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: update and its follow-up writes run in one Entry.transaction, as Api::V1::TransactionsController#update does. 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_options can now only return the label Trade::CreateForm already sets, so it and trade_from_created_model can 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_params is 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.