Skip to content

Instrument record PATCH silently discards arrays supplied for existing array fields #1639

Open

Description

@joshunrau

PATCH /v1/instrument-records/:id silently discards any array the request supplies for a field that already holds an array. The mergeWith customizer in updateById returns the existing (destination) value whenever the source value is an array, so the stored array never changes. The request still returns 200 with the record, so the client is told the edit succeeded when it was not applied. This affects record-array fields and any other array-valued data. An array for a field that is currently absent is written, because the customizer then returns undefined and lodash falls back to its default merge.

Where

apps/api/src/instrument-records/instrument-records.service.ts:381-386

const updatedData = mergeWith(instrumentRecord.data, data, (updatedValue: unknown, sourceValue: unknown) => {
  if (Array.isArray(sourceValue)) {
    return updatedValue; // the existing value, not the submitted one
  }
  return undefined;
});

Reproduce

Using the same customizer with lodash-es 4.18.1:

  • existing { meds: [{ name: 'a' }, { name: 'b' }], x: 1 }, update { meds: [{ name: 'c' }], x: 2 } → { meds: [{ name: 'a' }, { name: 'b' }], x: 2 }
  • Expected: { meds: [{ name: 'c' }], x: 2 }

Through the API: PATCH a record whose data has answers: [1, 2, 3] with { "answers": [9] }. The response is 200 and the stored value is still [1, 2, 3].

Tests

apps/api/src/instrument-records/__tests__/instrument-records.service.spec.ts, test should ignore an array the update supplies for an existing array field (line 1150, with a "Pins a reported defect" comment). When fixing, invert it: expect data: { answers: [9] } and rename it (for example, "should replace an existing array field with the array the update supplies"). The neighbouring test should merge an array update element by element into array data covers records whose root data is an array. The customizer is not called for the root, so that behaviour is unaffected.

Suggested fix

Return sourceValue instead of updatedValue from the customizer, so a submitted array replaces the stored one rather than being merged index by index or dropped.

Found while raising unit test coverage in #1632.

Activity

  1. added
    BugType: existing behavior is wrong
    Difficulty: LowIsolated change, about 2 hours or less
    Area: APIapps/api (NestJS backend, Prisma models, CASL permissions)
    Good First IssueWell-scoped, low-risk and self-contained; a good entry point for a new contributor
    on Oct 5, 2026
  2. joshunrau commented on Oct 26, 2026

    @joshunrau
    CollaboratorAuthor

    Still reproduces on main (d86740e). The customizer is now at instrument-records.service.ts:455-460, unchanged since f29adf1. The pinned test is at __tests__/instrument-records.service.test.ts:1121. efa9070 renamed the api .spec.ts unit tests to .test.ts.

    Scope

    The bug is wider than existing arrays. lodash falls back to its default merge only when the customizer returns undefined (baseMergeDeep), so a submitted array is dropped whenever the stored field is present, whatever it holds. Checked with lodash-es 4.18.1, the version vendor/lodash-es@4.x pins:

    • { answers: null } patched with { answers: [9] } stays { answers: null }.
    • The customizer is skipped for the root, but not for arrays inside root-array elements: [{ tags: [1, 2] }] patched with [{ tags: [7] }] keeps [1, 2].

    return sourceValue fixes both. Deleting the customizer does not: the default merge goes index by index, so [1, 2, 3] patched with [9] gives [9, 2, 3] (the same trap as #932). Root-array data itself is still merged that way after the fix, so a PATCH cannot remove an element from it. That is a separate change.

    Related

    #1723 revives both sides before the merge. A revived Set or Date is neither an array nor a plain object, so lodash assigns it wholesale and the two fixes are independent. #1723 and #1633 also change updateById and the same describe('updateById') block, so they are easiest to land as one PR with one fix(api) commit each.

    The route has no caller in apps/web. Its only in-repo client is odc-cli instrument-records update, whose help at cli/odc-cli:571 says the patch is "merged recursively". Change it to say arrays are replaced, in the same commit. cli/AGENTS.md asks for this because nothing else checks that file.

    Tests

    Triage note written with Claude Code. Code references point at main @ d86740e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: APIapps/api (NestJS backend, Prisma models, CASL permissions)BugType: existing behavior is wrongDifficulty: LowIsolated change, about 2 hours or lessGood First IssueWell-scoped, low-risk and self-contained; a good entry point for a new contributorPriority: MediumReal value; schedule normally

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions