fix(rest/python): rebuild discounts.applied idempotently on recalculation - #154
Merged
damaz91 merged 1 commit intoJul 30, 2026
Conversation
…tion _recalculate_totals rebuilt checkout.totals from scratch on every create/update but appended to discounts.applied without resetting it. Because the persisted (and reloaded) checkout already carries the applied entries from the previous response, an update that omits the discounts field accumulated a duplicate applied entry on every recalculation. Reset discounts.applied before recomputing it, mirroring how totals is rebuilt. The server is the authority for applied discounts (discount.json marks applied as ucp_request:"omit"), so the list must be rebuilt idempotently. Add a regression test (test_discount_applied_is_not_duplicated_on_update) that creates a checkout with a discount code, then updates it without re-submitting discounts and asserts applied stays a single entry.
XiaolongZhang-TT
force-pushed
the
fix/python-discount-applied-dedup-on-update
branch
from
July 30, 2026 05:30
5753a97 to
ea664f4
Compare
XiaolongZhang-TT
marked this pull request as ready for review
July 30, 2026 05:31
Contributor
Author
|
Marking this ready for review. Quick context for reviewers:
Open to a different reset location if a reviewer prefers it. |
damaz91
approved these changes
Jul 30, 2026
nicholasjameshall
approved these changes
Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
_recalculate_totalsrebuiltcheckout.totalsfrom scratch on every create/update but appended todiscounts.appliedwithout resetting it. Because the persisted checkout (reloaded on each update) already carries theappliedentries from the previous response, an update that omits thediscountsfield accumulates a duplicateappliedentry on every recalculation.Repro: create a checkout with
discounts.codes = ["10OFF"](responseappliedhas 1 entry), thenPUT /checkout-sessions/{id}with a body that omitsdiscounts(e.g. a quantity / shipping change) → the responsediscounts.appliedlists the discount twice, and keeps growing with each such update.Root cause —
rest/python/server/services/checkout_service.py,_recalculate_totals:checkout.totalsis reset (checkout.totals = []) at the top of each recalculation, butdiscounts.appliedis never reset, so it accumulates once the reloaded checkout brings back the prior entries.Fix: reset
checkout.discounts.applied = Nonebefore recomputing — the same "server is the authority, rebuild from scratch" pattern already used fortotals. This matchesdiscount.json, whereappliedcarriesucp_request: "omit"(a server-authoritative output field), so it must be rebuilt idempotently rather than appended to.Category (Required)
Related Issues
None — this is a newly identified bug in the Python sample server (unrelated to #121 / #134).
Checklist
!for breaking changes).ruff check+ruff formatclean)test_discount_applied_is_not_duplicated_on_update)Screenshots / Logs (if applicable)
Before/after from the regression test —
discounts.appliedafter an update that omits thediscountsfield:Before (bug):
After (fix):
Full Python integration suite: