Repository navigation
chore: update community identifiers and enhance serializers for user and tool APIs - #7716
Conversation
| location="path", | ||
| required=True, | ||
| ), | ||
| ] |
There was a problem hiding this comment.
Here's a review of the code:
Issues
-
Missing import:
OpenApiTypesis used (type=OpenApiTypes.STR) but not imported. Add:from drf_spectacular.utils import OpenApiParameter, OpenApiTypes
-
Inconsistent language in descriptions: Some descriptions are Chinese (
"工作空间id"), others English ("Version Name"). Standardize for consistency. -
Redundant pagination parameters: For DRF pagination,
current_page/page_sizeare usually query parameters, but they're markedlocation="path". Verify this matches your URL patterns — likely should belocation="query". -
Inheritance redundancy:
ToolWorkflowVersionPageApiinherits fromToolWorkflowVersionListApiand re-declaresget_responsereturningToolWorkflowVersionPageResult— that's fine, butget_parametersduplication could be reduced by extracting base path params into a helper sinceworkspace_id/tool_idappear at multiple levels. -
Patching whitespace-only changes (single → double quotes) mixed with functional changes makes the diff noisier; consider separating formatting changes into their own commit.
Optimization Suggestions
-
DRY up repeated parameters: Extract common params:
def _path_param(name, desc): return OpenApiParameter(name=name, description=desc, type=OpenApiTypes.STR, location="path", required=True) BASE_PARAMS = [_path_param("workspace_id", "工作空间id"), _path_param("tool_id", "工具id")]
Then reuse across all API classes.
-
Serializer result classes can be more concise:
class ToolWorkflowVersionResult(ResultSerializer): def get_data(self): return ToolVersionModelSerializer()
If
ResultSerializersupports it, consider a factory or lambda-based approach to avoid three near-identical classes. -
Type hint for
OpenApiTypes.STRon IDs: If IDs are UUIDs, considerOpenApiTypes.UUIDfor better schema validation in Swagger docs.
Summary
The main blocking bug is the missing OpenApiTypes import — the code will raise NameError at runtime. Everything else is cleanliness/consistency.
e357c85 to
779757e
Compare
| location="path", | ||
| required=True, | ||
| ), | ||
| ] |
There was a problem hiding this comment.
Overall the diff is mostly schema additions, but there are several things to check:
-
Most important: pagination parameters are declared as
pathparametersname="current_page", type=OpenApiTypes.INT, location="path", required=True,
and the same for
page_size. Unless your URL is actually something like:/.../{current_page}/{page_size}/these should almost certainly be query parameters:
OpenApiParameter( name="current_page", description="当前页码", type=OpenApiTypes.INT, location="query", required=False, default=1, ), OpenApiParameter( name="page_size", description="每页大小", type=OpenApiTypes.INT, location="query", required=False, default=20, ),
As written, this will generate incorrect OpenAPI documentation and potentially incorrect client SDKs.
-
nameparameter has no explicitlocationOpenApiParameter( name="name", description="Version Name", type=OpenApiTypes.STR, required=False, ),
It probably defaults to query, but it is better to be explicit:
location="query",
Also consider making the description language consistent with the rest of the file, e.g.
"版本名称". -
Verify
ToolVersionModelSerializeris the correct serializer
The new classes are namedToolWorkflowVersion*, but they useToolVersionModelSerializer. IfToolVersionis the actual version model for workflow versions, this is fine. Otherwise, it may be a naming/model mismatch. -
Check whether
ToolWorkflowVersionOperateApireally always returns a single version
If this class is reused forDELETE, the response should usually be204 No Content, not a serialized version. If it is reused for create/update, you may also need request serializers. If the operations differ, consider splitting the schema classes or overridingget_response()/ request schema per operation. -
DefaultResultSerializerimport may become unused
Ensure it is still used elsewhere in the file. If not, remove it. -
Minor style/import issue
The import line is long:from common.result import DefaultResultSerializer, ResultSerializer, ResultPageSerializer
Consider wrapping:
from common.result import ( DefaultResultSerializer, ResultPageSerializer, ResultSerializer, )
-
Minor optimization
ToolWorkflowVersionListResultandToolWorkflowVersionPageResultduplicate the sameget_data()logic. This is acceptable, but if more result types are added, consider a small shared base class. -
Unrelated quote-style changes
The changes from'path'to"path"inToolWorkflowExportApiare harmless but add diff noise if the project has not standardized on double quotes.
The main correction is the pagination parameter location; the rest are consistency and maintainability improvements.
| }, | ||
| tags=[_("Home page")], # type: ignore | ||
| ) | ||
| @has_permissions( |
There was a problem hiding this comment.
Overall, this is a schema/documentation-only change and does not alter runtime behavior. I do not see a clear functional bug, but there are a few schema-correctness and maintainability points to verify.
Potential issues
-
Verify the return type of
get_response()- The new code assumes
TokensAggregationAPI.get_response()returns a serializer class orOpenApiResponse, because it is now nested under200. - If
get_response()already returns aresponsesmapping/dict, then{200: <dict>}is likely invalid or produces an incorrect schema. - If it returns a serializer class, this change is correct.
- The new code assumes
-
Make sure the 400 response body matches
ResultStringSerializer- DRF’s default
ValidationErrorresponse is usually{"field": ["error"]}or{"detail": "..."}, not a customresultenvelope. - If the project has a custom exception handler that wraps errors in
ResultStringSerializer, this is correct. - Otherwise, the documented 400 schema will not match the actual response.
- DRF’s default
-
The 400 description may be too specific
"Invalid or missing time range parameters"only covers time-range errors.- These endpoints may also return 400 for invalid pagination, filters, or other query parameters.
- Consider a more general description such as
"Invalid query parameters", or keep endpoint-specific wording only if the validation really is time-range-only.
-
Authentication/permission errors are not documented
- These views use
TokenAuthandhas_permissions. - If you are adding explicit error documentation, consider whether
401and/or403should also be documented. - This may already be handled globally by drf-spectacular, but it is worth confirming.
- These views use
-
Duplication of the same 400 response definition
- The same
OpenApiResponseblock is repeated six times. - This is not a bug, but it increases the chance of inconsistency later.
- Consider a shared constant/helper, for example:
COMMON_400_RESPONSE = OpenApiResponse( response=ResultStringSerializer, description=_("Invalid query parameters"), ) def common_responses(success_response): return { 200: success_response, 400: COMMON_400_RESPONSE, }
Then use:
responses=common_responses(TokensAggregationAPI.get_response())
- The same
-
Import style is acceptable but slightly redundant
from common import resultandfrom common.result import ResultStringSerializerare fine.- No action is required unless the project has stricter import grouping rules.
-
Pre-existing issue: operation ID with spaces
- In
ApplicationMonitoring, the existingoperation_id="Dialogue-related statistical trends"contains spaces. - OpenAPI operation IDs should preferably be stable, machine-friendly identifiers, e.g.
homepage_dialogue_statistical_trends. - This is not caused by the diff, but it is an existing schema irregularity.
- In
Recommendation
The change is reasonable if:
get_response()returns a serializer class orOpenApiResponse, not a response dict.- The actual 400 payload is indeed
ResultStringSerializer. - The 400 description is accurate for all validation failures on these endpoints.
Otherwise, adjust the schema to match the real exception-handling and response structure.
| }, | ||
| tags=[_("Home page")], # type: ignore | ||
| ) | ||
| @has_permissions( |
There was a problem hiding this comment.
Code Review
Issues
-
Repeated code (DRY violation): The same
OpenApiResponse(response=ResultStringSerializer, description=_("Invalid or missing time range parameters"))block is duplicated 6 times. Extract it once:INVALID_TIME_RANGE_RESPONSE = OpenApiResponse( response=ResultStringSerializer, description=_("Invalid or missing time range parameters"), )
Then use
responses={200: ..., 400: INVALID_TIME_RANGE_RESPONSE}. -
operation_idinconsistency:ApplicationMonitoringuses"Dialogue-related statistical trends"— an English human-readable string with spaces/uppercase as anoperation_id. This breaks the typical snake_case convention and may cause invalid OpenAPI operationId values. Use e.g.homepage_application_monitoring. -
Import style:
from common import resultplusfrom common.result import ResultStringSerializer— if onlyResultStringSerializeris needed andresultmodule import is unused elsewhere in this diff, prefer a single consistent import.
Suggestions
-
Verify accuracy of the 400 doc: Make sure these endpoints actually return 400 (not 200-wrapped error bodies) for invalid time ranges; otherwise the schema will mislead clients. If errors are returned via your
Resultwrapper with HTTP 200, document that instead. -
Consider reusable serializer-level responses: If most views follow the
{200: ..., 400: ...}pattern, consider defining a small helper or mixin to build the responses dict, reducing boilerplate further. -
Minor: keep
# type: ignorecomments justified; if tags lists are valid, they may be removable.
Overall: functionally correct, but refactor the duplication and fix the operation_id naming.
chore: update community identifiers and enhance serializers for user and tool APIs