Skip to content

Commit

Permalink
Merge pull request #1026 from biigle/patch-1
Browse files Browse the repository at this point in the history
Improve video annotation points validation
  • Loading branch information
mzur authored Jan 6, 2025
2 parents 725930d + 0ae5f97 commit 037e7dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 0 additions & 5 deletions app/Http/Controllers/Api/VideoAnnotationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,8 @@ public function store(StoreVideoAnnotation $request)
}
}

// from a JSON request, the array may already be decoded
$points = $request->input('points', []);

if (is_string($points)) {
$points = json_decode($points);
}

$annotation = new VideoAnnotation([
'video_id' => $request->video->id,
'shape_id' => $request->input('shape_id'),
Expand Down
9 changes: 3 additions & 6 deletions app/Http/Requests/StoreVideoAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function rules()
'required_unless:shape_id,'.Shape::wholeFrameId(),
'array',
],
'points.*' => 'array',
'points.*.*' => 'numeric',
'frames' => 'required|array',
'frames.*' => 'required|numeric|min:0|max:'.$this->video->duration,
'track' => 'filled|boolean',
Expand All @@ -67,18 +69,13 @@ public function withValidator($validator)
$validator->errors()->add('frames', 'A new whole frame annotation must not have more than two frames.');
}

$points = $this->input('points', []);
$allArrays = array_reduce($points, fn ($c, $i) => $c && is_array($i), true);

if (!$allArrays) {
$validator->errors()->add('points', 'The points must be an array of arrays.');
}

if ($this->shouldTrack()) {
if ($frameCount !== 1) {
$validator->errors()->add('id', 'Only single frame annotations can be tracked.');
}

$points = $this->input('points', []);
if (count($points) !== 1) {
$validator->errors()->add('id', 'Only single frame annotations can be tracked.');
}
Expand Down
13 changes: 13 additions & 0 deletions tests/php/Http/Controllers/Api/VideoAnnotationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,19 @@ public function testStoreValidatePointsArray()
->assertStatus(422);
}

public function testStoreValidatePointsArray2()
{
$this->beEditor();
$this
->postJson("/api/v1/videos/{$this->video->id}/annotations", [
'shape_id' => Shape::pointId(),
'label_id' => $this->labelRoot()->id,
'points' => [[[10, 11]]],
'frames' => [0.0],
])
->assertStatus(422);
}

public function testStoreValidateFrames()
{
$this->beEditor();
Expand Down

0 comments on commit 037e7dc

Please sign in to comment.