Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
SDK: fix string field serialization for multipart/form-data requests (c…
Browse files Browse the repository at this point in the history
…vat-ai#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.
  • Loading branch information
SpecLad authored and mikhail-treskin committed Jul 1, 2023
1 parent 685452d commit 83d3142
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,14 @@ non-ascii paths while adding files from "Connected file share" (issue #4428)
- Missing source tag in project annotations (<https://github.com/opencv/cvat/pull/5408>)
- Creating a task with a Git repository via the SDK
(<https://github.com/opencv/cvat/issues/4365>)
- Queries via the low-level API using the `multipart/form-data` Content-Type with string fields
(<https://github.com/opencv/cvat/pull/5479>)

### Security
- `Project.import_dataset` not waiting for completion correctly
(<https://github.com/opencv/cvat/pull/5459>)


## \[2.2.0] - 2022-09-12
### Added
- Added ability to delete frames from a job based on (<https://github.com/openvinotoolkit/cvat/pull/4194>)
Expand Down
4 changes: 2 additions & 2 deletions cvat-sdk/gen/templates/openapi-generator/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 34 additions & 0 deletions tests/python/rest_api/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,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 <https://github.com/opencv/cvat/issues/4962>.
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",
Expand Down

0 comments on commit 83d3142

Please sign in to comment.