Skip to content

Fix KeyError in admin_create_coupon when optional fields are omitted#3

Merged
iamfarooqh merged 1 commit intoAISecurityConsortium:mainfrom
sudhamshk:fix/coupon-creation-keyerror
Apr 7, 2026
Merged

Fix KeyError in admin_create_coupon when optional fields are omitted#3
iamfarooqh merged 1 commit intoAISecurityConsortium:mainfrom
sudhamshk:fix/coupon-creation-keyerror

Conversation

@sudhamshk
Copy link
Copy Markdown

Summary

  • Fixed inconsistent dictionary access in admin_create_coupon endpoint (app/api/admin.py)
  • Three optional fields (maximum_discount, valid_from, valid_until) used body["key"] for value
    access after checking with body.get(), raising KeyError when the field is absent
  • Changed all three to use body.get() consistently

Bug 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

  • Create a coupon without maximum_discount — should default to None instead of raising KeyError
  • Create a coupon without valid_from/valid_until — should default to current time instead of
    raising KeyError
  • Create a coupon with all fields present — should work as before

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.
Copy link
Copy Markdown
Member

@iamfarooqh iamfarooqh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved the changes

@iamfarooqh iamfarooqh merged commit 717bef6 into AISecurityConsortium:main Apr 7, 2026
4 checks passed
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.

3 participants