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

Commit

Permalink
Allow attribute default/permitted values to be blank (cvat-ai#6454)
Browse files Browse the repository at this point in the history
This is de facto already allowed when CVAT is accessed via the UI/API,
even though the model/serializer configuration seem to prohibit it.
That's because validation for attribute properties is effectively
disabled on the server due to a bug. However, the SDK checks the schema,
and thus doesn't allow such values.

I think blank default values make sense (particularly for "text" type
attributes), so I updated the code to allow them.

Blank permitted values don't make quite as much sense, but I had to
allow them too, because the UI always submits the default value as the
first permitted value (even for freeform text attributes). This will let
us fix the broken validation on the server side (which I'm planning to
do soon) without removing the ability to set blank default attribute
values via the UI.

(FWIW, I don't think that the UI should add a `values` property when
serializing freeform text attributes at all, but it would take a more
substantial change to fix that, which I don't have time for right now.)
  • Loading branch information
SpecLad authored and mikhail-treskin committed Oct 25, 2023
1 parent db79f14 commit e4c7be9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- TDB

### Fixed
- TDB
- \[SDK\] Ability to create attributes with blank default values
(<https://github.com/opencv/cvat/pull/6454>)

### Security
- TDB
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.1 on 2023-07-10 15:57

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("engine", "0072_alter_issue_updated_date"),
]

operations = [
migrations.AlterField(
model_name="attributespec",
name="default_value",
field=models.CharField(blank=True, max_length=128),
),
migrations.AlterField(
model_name="attributespec",
name="values",
field=models.CharField(blank=True, max_length=4096),
),
]
4 changes: 2 additions & 2 deletions cvat/apps/engine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,8 @@ class AttributeSpec(models.Model):
mutable = models.BooleanField()
input_type = models.CharField(max_length=16,
choices=AttributeType.choices())
default_value = models.CharField(max_length=128)
values = models.CharField(max_length=4096)
default_value = models.CharField(blank=True, max_length=128)
values = models.CharField(blank=True, max_length=4096)

class Meta:
default_permissions = ()
Expand Down
2 changes: 1 addition & 1 deletion cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Meta:

class AttributeSerializer(serializers.ModelSerializer):
values = serializers.ListField(allow_empty=True,
child=serializers.CharField(max_length=200),
child=serializers.CharField(allow_blank=True, max_length=200),
)

class Meta:
Expand Down
4 changes: 0 additions & 4 deletions cvat/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5993,7 +5993,6 @@ components:
type: string
maxLength: 200
required:
- default_value
- input_type
- mutable
- name
Expand All @@ -6011,16 +6010,13 @@ components:
$ref: '#/components/schemas/InputTypeEnum'
default_value:
type: string
minLength: 1
maxLength: 128
values:
type: array
items:
type: string
minLength: 1
maxLength: 200
required:
- default_value
- input_type
- mutable
- name
Expand Down
24 changes: 24 additions & 0 deletions tests/python/sdk/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ def test_can_create_empty_project(self):
assert project.id != 0
assert project.name == "test project"

def test_can_create_project_with_attribute_with_blank_default(self):
project = self.client.projects.create(
spec=models.ProjectWriteRequest(
name="test project",
labels=[
models.PatchedLabelRequest(
name="text",
attributes=[
models.AttributeRequest(
name="text",
mutable=True,
input_type=models.InputTypeEnum("text"),
values=[],
default_value="",
)
],
)
],
)
)

labels = project.get_labels()
assert labels[0].attributes[0].default_value == ""

def test_can_create_project_from_dataset(self, fxt_coco_dataset: Path):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)
Expand Down

0 comments on commit e4c7be9

Please sign in to comment.