Skip to content

Fix: Add FastAPI validation bounds and test suites for patron check-ins and yearly goals endpoints (#13150) - #13156

Merged
jimchamp merged 1 commit into
internetarchive:masterfrom
RsbhThakur:13150/fix/fastapi-checkins-validation
Jul 17, 2026
Merged

jimchamp merged 1 commit into
internetarchive:masterfrom
RsbhThakur:13150/fix/fastapi-checkins-validation

Conversation

@RsbhThakur

Copy link
Copy Markdown
Contributor

Closes #13150

This PR fixes missing date range bounds, malformed edition key crashes, and unvalidated path parameters in FastAPI patron check-ins (openlibrary/fastapi/checkins.py) and yearly reading goals (openlibrary/fastapi/yearly_reading_goals.py) endpoints, and adds two dedicated unit test suites.

Technical

  1. In openlibrary/fastapi/checkins.py:

    • Added Pydantic range bounds to CheckInRequest: year (gt=0, le=9999), month (ge=1, le=12), and day (ge=1, le=31).
    • Added regex pattern matching for edition OLIDs: edition_key: Field(None, pattern=r"(?i)^(?:/books/)?OL\d+M$").
    • Integrated is_valid_date and datetime.date(y, m, d) in @model_validator(mode="after") to reject invalid calendar dates (e.g., Feb 31st).
    • Added Annotated[int, Path(gt=0)] constraints to work_id and check_in_id path parameters.
    • Safely cast numeric edition_id as int before passing to BookshelvesEvents.
  2. In openlibrary/fastapi/yearly_reading_goals.py:

    • Added gt=0, le=9999 bounds to ReadingGoalItem.year, ReadingGoalForm.year, and the GET /reading-goal.json?year= query parameter.
  3. In [NEW] openlibrary/tests/fastapi/test_checkins.py and [NEW] openlibrary/tests/fastapi/test_yearly_reading_goals.py:

    • Added dedicated unit test suites asserting HTTP 200 OK for valid inputs and HTTP 422 Unprocessable Entity responses for invalid date ranges, bad days, malformed OLIDs, and non-positive path/query params.

Testing

  1. Automated Unit Tests:

    docker compose run --rm home pytest openlibrary/tests/fastapi/test_checkins.py openlibrary/tests/fastapi/test_yearly_reading_goals.py

    Output: 12 passed, 5 warnings in 6.03s

Screenshot

N/A (Backend FastAPI validation & test suite implementation).

Stakeholders

@jimchamp

@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Thanks for the PR, @RsbhThakur!

@jimchamp is assigned to this PR and currently has:

  • 16 open PR(s) of equal or higher priority to review first
PR triage checklist (maintainers / Richy)
  • PR description — not empty; explains what the change does and how to verify it
  • References an issue — PR body contains a #NNN reference
    • Linked issue is triaged — has a Priority: * label (not just Needs: Triage)
    • Linked issue is assigned — has at least one assignee
  • Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
  • CI passing — checks still in progress at time of this comment
  • Test cases present — if the change touches substantive logic, test coverage exists or is explained
  • Proof of testing — PR body includes a description of what was tested, a screenshot, or a video

Note

This comment was automatically generated by PAM, Open Library's Project AI Manager. PAM provides status visibility, performs basic project management functions, and gives actionable feedback so contributors aren't left waiting.

@jimchamp
jimchamp merged commit 5943b8d into internetarchive:master Jul 17, 2026
3 checks passed
@RsbhThakur
RsbhThakur deleted the 13150/fix/fastapi-checkins-validation branch July 17, 2026 16:16
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.

FastAPI: Patron check-ins endpoint allows invalid date insertion and crashes on invalid edition_key

3 participants