Stop accepting category_id on the trades API

Closed
JA
21d ago
+169-58

Closes jaysbeekay/sure#619. Upstream issue: we-promise/sure#4707.

Fork pre-flight for we-promise/sure#4734 (draft): same branch, same commit. The branch up/trades-category-id is cut from upstream main at e658686d5.

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 upstream 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.