Fix KeyError in admin_create_coupon when optional fields are omitted#3
Merged
iamfarooqh merged 1 commit intoAISecurityConsortium:mainfrom Apr 7, 2026
Conversation
The coupon creation endpoint checks for optional fields using body.get() but then accesses them with body["key"], which raises KeyError when the field is not present in the request body. Affected fields: maximum_discount, valid_from, valid_until. Changed all three to use body.get() consistently for both the conditional check and the value access.
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.
Summary
admin_create_couponendpoint (app/api/admin.py)maximum_discount,valid_from,valid_until) usedbody["key"]for valueaccess after checking with
body.get(), raisingKeyErrorwhen the field is absentbody.get()consistentlyBug Details
Line 444:
maximum_discount=float(body["maximum_discount"])→float(body.get("maximum_discount"))Line 448:
body["valid_from"].replace(...)→body.get("valid_from", "").replace(...)Line 449:
body["valid_until"].replace(...)→body.get("valid_until", "").replace(...)Test plan
maximum_discount— should default toNoneinstead of raisingKeyErrorvalid_from/valid_until— should default to current time instead ofraising
KeyError