diff --git a/backend/api_v2/api_deployment_views.py b/backend/api_v2/api_deployment_views.py index 580c7494f..656a33ae7 100644 --- a/backend/api_v2/api_deployment_views.py +++ b/backend/api_v2/api_deployment_views.py @@ -4,7 +4,7 @@ from api_v2.constants import ApiExecution from api_v2.deployment_helper import DeploymentHelper -from api_v2.exceptions import InvalidAPIRequest, NoActiveAPIKeyError +from api_v2.exceptions import NoActiveAPIKeyError from api_v2.models import APIDeployment from api_v2.postman_collection.dto import PostmanCollection from api_v2.serializers import ( @@ -47,16 +47,14 @@ def initialize_request( def post( self, request: Request, org_name: str, api_name: str, api: APIDeployment ) -> Response: - file_objs = request.FILES.getlist(ApiExecution.FILES_FORM_DATA) serializer = ExecutionRequestSerializer(data=request.data) serializer.is_valid(raise_exception=True) + file_objs = serializer.validated_data.get(ApiExecution.FILES_FORM_DATA) timeout = serializer.validated_data.get(ApiExecution.TIMEOUT_FORM_DATA) include_metadata = serializer.validated_data.get(ApiExecution.INCLUDE_METADATA) include_metrics = serializer.validated_data.get(ApiExecution.INCLUDE_METRICS) use_file_history = serializer.validated_data.get(ApiExecution.USE_FILE_HISTORY) tag_names = serializer.validated_data.get(ApiExecution.TAGS) - if not file_objs or len(file_objs) == 0: - raise InvalidAPIRequest("File shouldn't be empty") response = DeploymentHelper.execute_workflow( organization_name=org_name, api=api, diff --git a/backend/api_v2/serializers.py b/backend/api_v2/serializers.py index 24bba28cc..611f4079f 100644 --- a/backend/api_v2/serializers.py +++ b/backend/api_v2/serializers.py @@ -9,8 +9,10 @@ from rest_framework.serializers import ( BooleanField, CharField, + FileField, IntegerField, JSONField, + ListField, ModelSerializer, Serializer, ValidationError, @@ -115,12 +117,31 @@ class ExecutionRequestSerializer(TagParamsSerializer): e.g:'tag1,tag2-name,tag3_name' """ + MAX_FILES_ALLOWED = 32 + timeout = IntegerField( min_value=-1, max_value=ApiExecution.MAXIMUM_TIMEOUT_IN_SEC, default=-1 ) include_metadata = BooleanField(default=False) include_metrics = BooleanField(default=False) use_file_history = BooleanField(default=False) + files = ListField( + child=FileField(), + required=True, + allow_empty=False, + error_messages={ + "required": "At least one file must be provided.", + "empty": "The file list cannot be empty.", + }, + ) + + def validate_files(self, value): + """Validate the files field.""" + if len(value) == 0: + raise ValidationError("The file list cannot be empty.") + if len(value) > self.MAX_FILES_ALLOWED: + raise ValidationError(f"Maximum '{self.MAX_FILES_ALLOWED}' files allowed.") + return value class ExecutionQuerySerializer(Serializer):