Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK: fix string field serialization for multipart/form-data requests #5479

Merged
merged 4 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,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
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)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the exclusion of None mean SDK users will obtain errors when trying to send None values in POST requests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But I don't think you can send None values as multipart/form-data anyway, so better to fail early.

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 @@ -491,6 +491,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