Skip to content

Commit 3d9c5ad

Browse files
authored
SDK: fix string field serialization for multipart/form-data requests (#5479)
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.
1 parent 47860c9 commit 3d9c5ad

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,14 @@ non-ascii paths while adding files from "Connected file share" (issue #4428)
135135
- Missing source tag in project annotations (<https://github.com/opencv/cvat/pull/5408>)
136136
- Creating a task with a Git repository via the SDK
137137
(<https://github.com/opencv/cvat/issues/4365>)
138+
- Queries via the low-level API using the `multipart/form-data` Content-Type with string fields
139+
(<https://github.com/opencv/cvat/pull/5479>)
140+
141+
### Security
138142
- `Project.import_dataset` not waiting for completion correctly
139143
(<https://github.com/opencv/cvat/pull/5459>)
140144

145+
141146
## \[2.2.0] - 2022-09-12
142147
### Added
143148
- Added ability to delete frames from a job based on (<https://github.com/openvinotoolkit/cvat/pull/4194>)

cvat-sdk/gen/templates/openapi-generator/api_client.mustache

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ class ApiClient(object):
126126
self.default_headers['User-Agent'] = value
127127

128128
def _serialize_post_parameter(self, obj):
129-
if isinstance(obj, (str, int, float, none_type, bool)):
130-
return ('', json.dumps(obj), 'application/json')
129+
if isinstance(obj, (str, int, float, bool)):
130+
return str(obj)
131131
elif isinstance(obj, io.IOBase):
132132
return self._serialize_file(obj)
133133
raise ApiValueError(

tests/python/rest_api/test_tasks.py

+34
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,40 @@ def test_can_create_task_with_defined_start_and_stop_frames(self):
492492
(task, _) = api_client.tasks_api.retrieve(task_id)
493493
assert task.size == 4
494494

495+
def test_can_create_task_with_sorting_method(self):
496+
task_spec = {
497+
"name": f"test {self._USERNAME} to create a task with a custom sorting method",
498+
"labels": [
499+
{
500+
"name": "car",
501+
"color": "#ff00ff",
502+
"attributes": [],
503+
}
504+
],
505+
}
506+
507+
image_files = generate_image_files(15)
508+
509+
task_data = {
510+
"client_files": image_files[5:] + image_files[:5], # perturb the order
511+
"image_quality": 70,
512+
"sorting_method": "natural",
513+
}
514+
515+
# Besides testing that the sorting method is applied, this also checks for
516+
# regressions of <https://github.com/opencv/cvat/issues/4962>.
517+
task_id = self._test_create_task(
518+
self._USERNAME, task_spec, task_data, content_type="multipart/form-data"
519+
)
520+
521+
# check that the frames were sorted again
522+
with make_api_client(self._USERNAME) as api_client:
523+
data_meta, _ = api_client.tasks_api.retrieve_data_meta(task_id)
524+
525+
# generate_image_files produces files that are already naturally sorted
526+
for image_file, frame in zip(image_files, data_meta.frames):
527+
assert image_file.name == frame.name
528+
495529
def test_can_get_annotations_from_new_task_with_skeletons(self):
496530
spec = {
497531
"name": f"test admin1 to create a task with skeleton",

0 commit comments

Comments
 (0)