{
  "filename": "main.py",
  "rule": "The discount percentage accepted by the checkout endpoint must be constrained to the inclusive range 0 to 20 before a total is calculated.",
  "fixed": "from decimal import Decimal\nfrom fastapi import FastAPI\nfrom pydantic import BaseModel, Field\n\napp = FastAPI()\n\nclass Checkout(BaseModel):\n    subtotal: Decimal = Field(gt=0)\n    discount_percent: Decimal = Field(default=0, ge=0, le=20)\n\n@app.post(\"/checkout\")\ndef checkout(body: Checkout):\n    total = body.subtotal * (1 - body.discount_percent / 100)\n    return {\"total\": str(total.quantize(Decimal(\"0.01\")))}\n",
  "broken": "from decimal import Decimal\nfrom fastapi import FastAPI\nfrom pydantic import BaseModel, Field\n\napp = FastAPI()\n\nclass Checkout(BaseModel):\n    subtotal: Decimal = Field(gt=0)\n    discount_percent: Decimal = Field(default=0, ge=0)\n\n@app.post(\"/checkout\")\ndef checkout(body: Checkout):\n    total = body.subtotal * (1 - body.discount_percent / 100)\n    return {\"total\": str(total.quantize(Decimal(\"0.01\")))}\n",
  "test": {
    "filename": "test_main.py",
    "source": "from fastapi.testclient import TestClient\nfrom main import app\n\nclient = TestClient(app)\n\ndef test_discount_boundary():\n    response = client.post(\"/checkout\", json={\"subtotal\": \"100\", \"discount_percent\": \"20\"})\n    assert response.status_code == 200\n    assert response.json()[\"total\"] == \"80.00\"\n    assert client.post(\"/checkout\", json={\"subtotal\": \"100\", \"discount_percent\": \"21\"}).status_code == 422\n    assert client.post(\"/checkout\", json={\"subtotal\": \"100\", \"discount_percent\": \"-1\"}).status_code == 422\n",
    "command": "python -m pytest -q"
  }
}