You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Name: Bug report
About: Create a report to help us improve
Title: "chore: refactor in Squeeze API endpoints"
Labels: "modification, enhancement
# Describe the modification
The Squeeze API endpoints defined in the `squeeze` router have several issues and areas for improvement:
1. Incorrect `response_model` Usage:
The endpoints use `response_model=success_response`, but `success_response` is a function (or utility) that returns a response dictionary rather than a Pydantic model. This might lead to unexpected behavior in automatic API documentation and response validation.
2. Query Parameter Dependency for Filters:
The `filter` parameter in the GET endpoints (`get_all_squeeze` and `get_squeeze`) is defined as `filter: FilterSqueeze = None`. If `FilterSqueeze` is a Pydantic model intended to parse query parameters, it should be provided as a dependency (e.g., using `Depends()`) so that FastAPI can correctly extract and validate query parameters.
3. Inconsistent Error Handling:
In `create_squeeze`, if the user is not found, the endpoint returns a success response with a 404 status code rather than raising an HTTPException. This may not integrate well with global error handlers.
4. Wildcard Imports:
The code imports models using `from api.v1.models import *`. This can lead to namespace pollution and makes it less clear which models are in use. Explicit imports are preferred for maintainability and clarity.
# To Reproduce
Steps to reproduce the behavior:
1. Call the POST endpoint to create a squeeze page with an email that does not exist in the database and observe that it returns a success response with a 404 status code instead of raising an HTTPException.
2. Examine the generated API documentation and notice that the `response_model` does not reflect a proper Pydantic model schema.
3. Use the GET endpoints with query parameters for filtering and observe that the `filter` parameter may not be correctly parsed if sent as query parameters.
# Expected behavior- The endpoints should specify a proper Pydantic response model in the `response_model` parameter.
- The `filter` parameter should be injected as a dependency using `Depends()` so that query parameters are correctly parsed and validated.
- Error cases (such as a missing user in `create_squeeze`) should raise HTTPException to allow for consistent error handling.
- Imports should be explicit to improve clarity and maintainability.
# Proposed Fixes1. Fix `response_model` Usage
Instead of using `success_response` as the `response_model`, define a Pydantic schema (for example, `SuccessResponseSchema`) that represents the success response structure. Then update the endpoint decorators accordingly.
Before:
```python@squeeze.post("", response_model=success_response, status_code=201)
defcreate_squeeze(...):
...return success_response(status.HTTP_201_CREATED, SUCCESS, new_squeeze.to_dict())
Improve Error Handling in create_squeeze
Instead of returning a success response for a 404 error, raise an HTTPException.
Before:
user=user_service.fetch_by_email(db, data.email)
ifnotuser:
returnsuccess_response(status.HTTP_404_NOT_FOUND, "User not found!")
After:
user=user_service.fetch_by_email(db, data.email)
ifnotuser:
raiseHTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found!")
Avoid Wildcard Imports
Replace the wildcard import with explicit model imports.
Before:
fromapi.v1.modelsimport
After:
fromapi.v1.models.userimportUserfromapi.v1.models.squeezeimportSqueeze# Example, adjust as needed.
Additional context
Addressing these issues will improve response validation, API documentation clarity, and error handling consistency. This will ultimately lead to a more maintainable and robust API implementation for squeeze page management.
The text was updated successfully, but these errors were encountered:
After:
If
FilterSqueeze
is a Pydantic model, inject it viaDepends()
to ensure proper validation.Before:
After:
create_squeeze
Instead of returning a success response for a 404 error, raise an HTTPException.
Before:
After:
Replace the wildcard import with explicit model imports.
Before:
After:
Additional context
Addressing these issues will improve response validation, API documentation clarity, and error handling consistency. This will ultimately lead to a more maintainable and robust API implementation for squeeze page management.
The text was updated successfully, but these errors were encountered: