Skip to content

fix(api): validate time entry create rules at schema level - #894

Open
puritanne wants to merge 1 commit into
DRYTRIX:developfrom
epoch8:fix/time-entry-create-schema
Open

puritanne wants to merge 1 commit into
DRYTRIX:developfrom
epoch8:fix/time-entry-create-schema

Conversation

@puritanne

Copy link
Copy Markdown

Description

POST /api/v1/time-entries rejects every task_id, even when project_id is sent.

Steps to reproduce:

  1. POST /api/v1/time-entries with {"project_id": 1, "task_id": 2, "start_time": "2026-11-03T09:00:00", "end_time": "2026-11-03T10:30:00"} (task 2 belongs to project 1).
  2. Response: 400 {"task_id": ["task_id can only be set when project_id is provided"]}.

Expected: 201 with task_id set. Today an API client has to create the entry without a task and then PATCH the task in.

Cause: TimeEntryCreateSchema checks its cross-field rules in field-level @validates hooks that read kwargs["data"]. Marshmallow 4 (4.4.0 in requirements.txt) passes only data_key to those hooks, so the other fields are never visible. Besides task_id, this means:

  • "project_id": null together with a client_id (and "client_id": null with a project_id) is rejected;
  • end_time <= start_time is never caught by the schema (the service still catches it).

Fix: the same rules in one @validates_schema hook, the way TimerStartSchema already does it. Error messages and field keys are unchanged. If one time is naive and the other aware, the schema skips the comparison and leaves it to the service, as before.

Environment: TimeTracker 5.21.0 (Docker), marshmallow 4.4.0, PostgreSQL 16.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would change existing behavior)
  • Documentation update
  • Refactor (no functional change)

Checklist

  • My code follows the project's style guidelines (Black, flake8).
  • I have added/updated tests for my changes.
  • All tests pass locally (e.g. pytest).
  • I have updated the documentation if needed.
  • For user-facing changes, I have added an entry to the Unreleased section of CHANGELOG.md.

Tests: tests/test_routes/test_api_v1_time_entry_create_validation.py, 14 cases over the API and the schema. 10 of them fail without the fix. With the fix, they pass together with tests/test_api_v1.py and tests/test_routes/test_api_v1_time_entries_refactored.py (62 passed). We have run this fix in production on our fork since 5.21.0.

Related issues

None filed; the PR describes the bug.

TimeEntryCreateSchema checked cross-field rules in field-level @validates
hooks that read kwargs["data"]. Marshmallow 4 passes only data_key to those
hooks, so the other fields were never visible:
- POST /api/v1/time-entries rejected every task_id, even with project_id;
- "project_id": null with a client_id (and the reverse) was rejected;
- end_time <= start_time was never caught by the schema.

Move the rules into one @validates_schema hook, as in TimerStartSchema.
Messages and field keys are unchanged. A naive start with an aware end is
left to the service, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FEehhsUMojSDD4gn58PMHK

This branch has not been deployed

No deployments
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.

1 participant