From deb2c54be1e5f7b7c90a71dc6877d1decda9fc43 Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Fri, 16 Dec 2022 13:50:36 +0300 Subject: [PATCH 1/3] SDK: fix field serialization for multipart/form-data requests Django REST Framework ignores the Content-Type on request body parts, so it doesn't know that they are JSON-encoded. Instead, it just tries to decode each part as if it was an `str()`-encoded value. Change the encoding to match the decoding. The only type this matters for is `str`, because `json.dumps` and `str` produce different encodings for `str` values. Remove `none_type` from the list of encodable types since, to my knowledge, there's no way to encode a `None` value as a `multipart/form-data` part in a way that DRF will understand. --- cvat-sdk/gen/templates/openapi-generator/api_client.mustache | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cvat-sdk/gen/templates/openapi-generator/api_client.mustache b/cvat-sdk/gen/templates/openapi-generator/api_client.mustache index 0cf16488f099..2bc4838ff864 100644 --- a/cvat-sdk/gen/templates/openapi-generator/api_client.mustache +++ b/cvat-sdk/gen/templates/openapi-generator/api_client.mustache @@ -126,8 +126,8 @@ class ApiClient(object): self.default_headers['User-Agent'] = value def _serialize_post_parameter(self, obj): - if isinstance(obj, (str, int, float, none_type, bool)): - return ('', json.dumps(obj), 'application/json') + if isinstance(obj, (str, int, float, bool)): + return str(obj) elif isinstance(obj, io.IOBase): return self._serialize_file(obj) raise ApiValueError( From 079b2772b2730bf9ebc75073b37e3fc3b5755c7d Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Fri, 16 Dec 2022 14:41:59 +0300 Subject: [PATCH 2/3] SDK: Add a test that uses a string field in a multipart/form-data request --- tests/python/rest_api/test_tasks.py | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/python/rest_api/test_tasks.py b/tests/python/rest_api/test_tasks.py index b29f27f44f6b..d555a4d57277 100644 --- a/tests/python/rest_api/test_tasks.py +++ b/tests/python/rest_api/test_tasks.py @@ -483,6 +483,40 @@ def test_can_create_task_with_defined_start_and_stop_frames(self): (task, _) = api_client.tasks_api.retrieve(task_id) assert task.size == 4 + def test_can_create_task_with_sorting_method(self): + task_spec = { + "name": f"test {self._USERNAME} to create a task with a custom sorting method", + "labels": [ + { + "name": "car", + "color": "#ff00ff", + "attributes": [], + } + ], + } + + image_files = generate_image_files(15) + + task_data = { + "client_files": image_files[5:] + image_files[:5], # perturb the order + "image_quality": 70, + "sorting_method": "natural", + } + + # Besides testing that the sorting method is applied, this also checks for + # regressions of . + task_id = self._test_create_task( + self._USERNAME, task_spec, task_data, content_type="multipart/form-data" + ) + + # check that the frames were sorted again + with make_api_client(self._USERNAME) as api_client: + data_meta, _ = api_client.tasks_api.retrieve_data_meta(task_id) + + # generate_image_files produces files that are already naturally sorted + for image_file, frame in zip(image_files, data_meta.frames): + assert image_file.name == frame.name + def test_can_get_annotations_from_new_task_with_skeletons(self): spec = { "name": f"test admin1 to create a task with skeleton", From 185ae38a89b0d1c59e5edb61a649e1ad4e9b715f Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Fri, 16 Dec 2022 14:57:31 +0300 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50c0527fdced..1e251b0d331a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,6 +103,8 @@ non-ascii paths while adding files from "Connected file share" (issue #4428) - Missing source tag in project annotations () - Creating a task with a Git repository via the SDK () +- Queries via the low-level API using the `multipart/form-data` Content-Type with string fields + () ### Security - TDB