Add file size validation to document upload endpoint#584
Add file size validation to document upload endpoint#584ankit-mehta07 wants to merge 5 commits intoProjectTech4DevAI:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughWalkthroughAdds organization and project foreign keys to Job (DB + model + CRUD + service callers) via migrations. Introduces async document file-size validation with configurable MAX_DOCUMENT_UPLOAD_SIZE_MB (default 512MB), integrates it into upload route, and adds tests for oversize/empty/file-ok cases. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Route as /documents/upload
participant Validator as validate_document_file
participant Storage as CloudStorage
participant DB as Database
Client->>Route: POST file + metadata
Route->>Validator: await validate_document_file(file)
alt size > MAX
Validator-->>Route: raise 413
Route-->>Client: 413 Payload Too Large
else size == 0
Validator-->>Route: raise 422
Route-->>Client: 422 Unprocessable Entity
else valid size
Validator-->>Route: return file_size
Route->>Storage: upload file
Storage-->>Route: upload success
Route->>DB: create document record
DB-->>Route: record created
Route-->>Client: 200 OK (document created)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
backend/app/services/response/jobs.py (1)
1-6:⚠️ Potential issue | 🟡 MinorAdd a type hint for
task_instance.
task_instanceis untyped on Line 61, which breaks the project’s type-hint requirement.As per coding guidelines, "Always add type hints to all function parameters and return values in Python code".✅ Suggested fix
+from typing import Any @@ def execute_job( request_data: dict, project_id: int, organization_id: int, job_id: str, task_id: str, - task_instance, + task_instance: Any, ) -> None:Also applies to: 55-62
🤖 Fix all issues with AI agents
In `@backend/app/alembic/versions/043_add_project_org_to_job_table.py`:
- Around line 22-41: The migration adds non-nullable columns organization_id and
project_id to the job table using op.add_column without a server_default, which
will fail if rows already exist; update the migration to perform a safe
two-phase change: either (A) add organization_id and project_id with a sensible
server_default (or temporary default value) so existing rows get backfilled,
commit, then remove the server_default and alter nullable to False, or (B) add
both columns as nullable (nullable=True) via op.add_column, run a data backfill
step to populate them, then run a follow-up ALTER to set nullable=False; adjust
the op.add_column calls for "organization_id" and "project_id" accordingly and
include a follow-up migration step to remove defaults or flip nullable once
backfill is done.
In `@backend/app/alembic/versions/044_optimize_conversation_query.py`:
- Around line 18-34: The migration functions upgrade and downgrade lack explicit
return type annotations; update their signatures (functions named upgrade and
downgrade in this migration) to include return type hints (i.e., -> None) so
they comply with the project's mandatory type-hints guideline, leaving the
function bodies unchanged and keeping the existing op.create_index/op.drop_index
calls intact.
- Around line 11-15: The migration functions upgrade() and downgrade() lack
return type annotations; update their definitions to include explicit return
types by changing them to "def upgrade() -> None:" and "def downgrade() ->
None:" so both functions are annotated as returning None (keep bodies unchanged
and only adjust the function signatures for upgrade and downgrade).
In `@backend/app/api/docs/documents/upload.md`:
- Around line 7-11: The docs claim a 50MB max but the code default constant
MAX_DOCUMENT_UPLOAD_SIZE_MB is 512; update the documentation in the upload.md
text to reflect the actual default (change "Maximum file size: 50MB" to "Maximum
file size: 512MB (configurable via MAX_DOCUMENT_UPLOAD_SIZE_MB environment
variable)") and ensure any related lines about rejection behavior remain
unchanged; reference the MAX_DOCUMENT_UPLOAD_SIZE_MB symbol so readers know the
source of truth.
🧹 Nitpick comments (2)
backend/app/models/job.py (1)
92-94: Consider addingback_populatesfor bidirectional navigation.The relationships lack
back_populates, meaning you cannot navigate fromOrganizationorProjectto their associated jobs. If bidirectional access is needed (e.g.,organization.jobs), you'll need to add corresponding relationship fields to those models.♻️ Example with back_populates
# Relationships - organization: Optional["Organization"] = Relationship() - project: Optional["Project"] = Relationship() + organization: Optional["Organization"] = Relationship(back_populates="jobs") + project: Optional["Project"] = Relationship(back_populates="jobs")Then add to
OrganizationandProjectmodels:jobs: list["Job"] = Relationship(back_populates="organization", cascade_delete=True)backend/app/crud/jobs.py (1)
15-31: Consider adding a log statement for job creation.Per the coding guidelines, log messages should be prefixed with the function name. Adding a log entry here would improve observability for job creation events.
📝 Proposed logging addition
self.session.add(new_job) self.session.commit() self.session.refresh(new_job) + logger.info( + f"[create] Job created | job_id={new_job.id}, job_type={job_type}, " + f"project_id={project_id}, organization_id={organization_id}" + ) return new_job
backend/app/alembic/versions/044_optimize_conversation_query.py
Outdated
Show resolved
Hide resolved
Prajna1999
left a comment
There was a problem hiding this comment.
Is this not a duplicate PR?
Summary
Result
Improves reliability and prevents oversized uploads.
Summary by CodeRabbit
New Features
Documentation
Tests
Chores
Chores