Skip to content

FIX: Remove envelope response pattern and add Pydantic response models#76

Draft
YektaY wants to merge 3 commits intomainfrom
yektay/api-changes-remove-envelope
Draft

FIX: Remove envelope response pattern and add Pydantic response models#76
YektaY wants to merge 3 commits intomainfrom
yektay/api-changes-remove-envelope

Conversation

@YektaY
Copy link
Copy Markdown
Collaborator

@YektaY YektaY commented Apr 22, 2026

Description

Note: This PR builds off of #75 and should not be reviewed until that has be reviewed and merged

  • Drop the {errorCode, errorMessage, payload} envelope from every REST response. Endpoints now return their DTO (or primitive) directly.
  • Add response_model= to every handler in app/api/v1/ so Swagger and /openapi.json render accurate, per-endpoint response schemas instead of {}.
  • Add missing response DTOs for endpoints that previously returned ad-hoc dicts
  • Replace raise APIException(code, msg, status) with raise HTTPException(status_code=code, detail=msg) across handlers and app/dependencies.py. Error bodies now follow FastAPI's default {"detail": "..."} shape.
  • Delete app/api/responses.py (the success_response / error_response helpers and APIException class), ApiResultResponse from app/schemas/common.py, and the matching exception handler in app/main.py.
  • Update tests

Motivation

The envelope pattern was tracked as a bug in #32 and react-squirrel#77. Two problems with the old design:

  1. OpenAPI/Swagger docs were useless for responses. Because every handler was typed -> dict, FastAPI couldn't generate accurate response examples — /docs showed generic {} for every endpoint. With response_model= attached, Swagger now shows the actual field shape for every response, and any consumer regenerating types from /openapi.json gets correct TypeScript interfaces.
  2. Redundant wrapping/unwrapping. HTTP already has status codes for success/failure. The envelope forced both sides to hand-serialize (success_response(x) on the backend) and hand-deserialize (response.data.payload on the frontend) for no added signal. Removing it simplifies both ends and eliminates a class of bugs where handlers forgot to wrap.

The frontend work to drop the apiClient.ts unwrap layer is react-squirrel#77 and needs to deploy with with this change.

Closes #32

Response shape diff (example)

BeforeGET /v1/snapshots:

{
  "errorCode": 0,
  "errorMessage": null,
  "payload": [{ "id": "...", "title": "...", "pvCount": 42 }]
}

AfterGET /v1/snapshots:

[{ "id": "...", "title": "...", "pvCount": 42 }]

Pre-merge checklist

  • Code works interactively
  • Code contains descriptive docstrings
  • New/changed functions and methods are covered in the test suite where possible
  • Test suite passes locally
  • Test suite passes on GitHub Actions

@YektaY YektaY changed the title Yektay/api changes remove envelope FIX: Remove envelope response pattern and add Pydantic response models Apr 22, 2026
@YektaY YektaY self-assigned this Apr 22, 2026
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.

[BUG] - API Endpoints Don't Return Response Models

1 participant