Skip to content

fix: Prevent ParseObject::remove from saving wrong values when removing multiple values - #531

Open
zigzagdev wants to merge 2 commits into
parse-community:masterfrom
zigzagdev:fix/remove-operation-multiple-values
Open

zigzagdev wants to merge 2 commits into
parse-community:masterfrom
zigzagdev:fix/remove-operation-multiple-values

Conversation

@zigzagdev

@zigzagdev zigzagdev commented Nov 27, 2026 •

Copy link
Copy Markdown

New Pull Request Checklist

Issue Description

When ParseObject::remove() is called with two or more values, RemoveOperation::_apply() returns the wrong array: values that should be removed are kept, and values that should be kept are duplicated.

    (new RemoveOperation([1, 2]))->_apply([1, 2, 3], null, 'k');
    // expected: [3]
    // actual:   [1, 2, 3, 3]

When remove follows set on the same key, the two operations are merged into a SetOperation whose value is the result of _apply(), so the broken value is saved to the server as is.

Closes: N/A

Approach

The nested loop in _apply() appended an existing value every time it did not match one of the values to remove.
This PR changes it to drop an existing value if it matches any of the values to remove.
The check is done in a new private method, isRemoved().

  • Scalars and other values: compared with ===
  • ParseObjects: compared by objectId, if not dirty

Added tests like

  • Removing multiple values
  • Removing every duplicate of a value
  • Removing an empty array (the array is left unchanged)
  • Removing ParseObjects by objectId
  • The value after merging with a SetOperation (the value sent to the server)

TODOs before merging

  • Add tests
$ php -d display_errors=0 -d log_errors=0 ./vendor/bin/phpunit --bootstrap /tmp/unit-bootstrap.php --no-coverage --testdox tests/Parse/RemoveOperationTest.php
PHPUnit 10.1.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.5.9
Configuration: /Users/masa/WebstormProjects/parse-php-sdk/phpunit.xml


Deprecated: Constant E_STRICT is deprecated since 8.4, the error level was removed in /Users/masa/WebstormProjects/parse-php-sdk/vendor/phpunit/phpunit/src/Runner/ErrorHandler.php on line 52

Deprecated: Constant E_STRICT is deprecated since 8.4, the error level was removed in /Users/masa/WebstormProjects/parse-php-sdk/vendor/phpunit/phpunit/src/Runner/ErrorHandler.php on line 52

Deprecated: Constant E_STRICT is deprecated since 8.4, the error level was removed in /Users/masa/WebstormProjects/parse-php-sdk/vendor/phpunit/phpunit/src/Runner/ErrorHandler.php on line 52
D........                                                           9 / 9 (100%)

Time: 00:00.019, Memory: 8.00 MB

Remove Operation (Parse\Test\RemoveOperation)
 ✔ Missing array
 ✔ Merge previous
 ✔ Invalid merge
 ✔ Empty apply
 ✔ Apply removes multiple values
 ✔ Apply removes all duplicates
 ✔ Apply with nothing to remove
 ✔ Apply removes parse objects by object id
 ✔ Merge with set removes multiple values

OK, but there are issues!
Tests: 9, Assertions: 15, Deprecations: 1.
  • Add changes to documentation (guides, repository pages, in-code descriptions)
  • Add changelog entry

Summary by CodeRabbit

  • Bug Fixes
    • Removing items now consistently excludes matching values, including duplicates, while preserving unrelated values.
    • Removal requests with no targets leave existing values unchanged.
    • Object values are matched consistently by their identifier, and removal operations combined with a prior set correctly exclude the requested items.

RemoveOperation::_apply looped over every value to remove for each
existing value and appended the existing value once per non-matching
target. With two or more values to remove, matched values were kept and
unmatched values were duplicated, e.g. removing [1, 2] from [1, 2, 3]
returned [1, 2, 3, 3]. Removing an empty list also cleared the array.

The broken result is not only local: when a remove follows a set on the
same key, the operations merge into a SetOperation built from _apply, so
setArray('arr', [1, 2, 3]) followed by remove('arr', [1, 2]) saved
[1, 2, 3, 3] to the server.

Keep an existing value unless it matches any of the values to remove.
The match rules (strict equality, ParseObjects by objectId when not
dirty) are unchanged.
Add cases for RemoveOperation::_apply removing two or more values,
removing every duplicate, removing nothing, and removing ParseObjects
by objectId. Also check that merging with a previous SetOperation
produces the correct value, since that value is sent to the server.

These cases fail without the fix in RemoveOperation::_apply.
@parse-github-assistant

Copy link
Copy Markdown

🚀 Thanks for opening this pull request! We appreciate your effort in improving the project. Please let us know once your pull request is ready for review.

Tip

  • Keep pull requests small. Large PRs will be rejected. Break complex features into smaller, incremental PRs.
  • Use Test Driven Development. Write failing tests before implementing functionality. Ensure tests pass.
  • Group code into logical blocks. Add a short comment before each block to explain its purpose.
  • We offer conceptual guidance. Coding is up to you. PRs must be merge-ready for human review.
  • Our review focuses on concept, not quality. PRs with code issues will be rejected. Use an AI agent.
  • Human review time is precious. Avoid review ping-pong. Inspect and test your AI-generated code.

Note

Please respond to review comments from AI agents just like you would to comments from a human reviewer. Let the reviewer resolve their own comments, unless they have reviewed and accepted your commit, or agreed with your explanation for why the feedback was incorrect.

Caution

Pull requests must be written using an AI agent with human supervision. Pull requests written entirely by a human will likely be rejected, because of lower code quality, higher review effort and the higher risk of introducing bugs. Please note that AI review comments on this pull request alone do not satisfy this requirement. Our CI and AI review are safeguards, not development tools. If many issues are flagged, rethink your development approach. Invest more effort in planning and design rather than using review cycles to fix low-quality code.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

RemoveOperation::_apply now retains each unmatched old value once, including when the removal list is empty. Tests cover value filtering, ParseObject matching by object ID, and merging with SetOperation.

Changes: Remove Operation Filtering

Layer / File(s) Summary
Filter values and verify removal behavior
src/Parse/Internal/RemoveOperation.php, tests/Parse/RemoveOperationTest.php
_apply checks each old value against the removal objects and retains it once when no match exists. Tests cover removal of matching values, preservation with an empty removal list, ParseObject matching by object ID, and merging with SetOperation.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: grahamcampbell

Merge Risk: Minimal · up to 2eb00

This change fixes the remove operation so that removing multiple values no longer duplicates or keeps the wrong values. No actionable merge-blocking risk was found. The documentation and changelog TODOs remain open.

Pre-merge checks: Passed 6

Check name Status Explanation
Title check Passed The title begins with the required prefix "fix:" and the first letter after the prefix is capitalized ("Prevent"). It clearly describes the main change: correcting RemoveOperation so that ParseObject:…
Description check Passed The description follows the repository template. It includes the checklist, the Issue Description, the Approach, and the TODOs. It explains the bug with a reproduction example, describes the fix (new …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Security Check Passed The security check found no problematic pattern introduced by this PR. The reviewed range changes two files: src/Parse/Internal/RemoveOperation.php (+27/-15) and tests/Parse/RemoveOperationTest.php. T…
Engage In Review Feedback Passed The check fails only when a user ignores or resolves review feedback without first engaging in discussion and then either implementing the feedback in a commit or convincing the reviewer to retract it…
Full details: Title check

Explanation

The title begins with the required prefix "fix:" and the first letter after the prefix is capitalized ("Prevent"). It clearly describes the main change: correcting RemoveOperation so that ParseObject::remove no longer saves wrong values when removing multiple values.

Full details: Description check

Explanation

The description follows the repository template. It includes the checklist, the Issue Description, the Approach, and the TODOs. It explains the bug with a reproduction example, describes the fix (new private isRemoved() method, strict comparison for scalars, objectId comparison for non-dirty ParseObjects), and lists the tests added. It also includes a local test run showing 9 tests passing. The Closes line is set to N/A rather than an issue number, and the documentation and changelog TODOs remain unchecked. These are non-critical gaps, so the description is mostly complete.

Full details: Security Check

Explanation

The security check found no problematic pattern introduced by this PR. The reviewed range changes two files: src/Parse/Internal/RemoveOperation.php (+27/-15) and tests/Parse/RemoveOperationTest.php. The source change moves the matching logic out of a nested loop into a private isRemoved() helper. An existing value is kept only when it matches none of the removal targets. The helper adds no I/O, no deserialization, no dynamic code execution, no auth or permission logic, and no new external input handling. The ParseObject match still uses the same rule as before: both values are ParseObjects, the existing object is not dirty, and the object IDs compare equal. The loose == comparison on object IDs was already present before this PR. The diff keeps it unchanged, so the PR does not introduce or widen it. The behavior change is a data-correctness fix: duplicated values and an empty removal list no longer corrupt the stored array. That is not a security vulnerability under the stated policy. The test file adds only unit tests with no sensitive data or unsafe patterns.

Full details: Engage In Review Feedback

Explanation

The check fails only when a user ignores or resolves review feedback without first engaging in discussion and then either implementing the feedback in a commit or convincing the reviewer to retract it. The supplied review evidence contains no feedback to engage with: - The posted CodeRabbit review comments section states that no review threads were returned. - The current review produced zero actionable findings. No feedback item was ignored or resolved. The failure condition therefore does not apply, and the check passes. This verdict relies only on the supplied finding metadata. It does not depend on the code changes in the diff. The verdict covers only the review data supplied for this run. It does not establish that earlier review threads were absent. The supplied data states that no threads were returned, so no earlier thread can be assessed here.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f4df641f-2c93-432c-9c96-c03118938318
Commits

Reviewing files that changed from the base of the PR and between 4b52492 and 2eb00c9.

Files selected for processing: 2

  • src/Parse/Internal/RemoveOperation.php
  • tests/Parse/RemoveOperationTest.php

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant