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

fixed server for duplicate attribute names #7890

Merged
merged 7 commits into from
May 16, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Fixed

- REST API allowed to create several attributes with the same name within one label
(<https://github.com/cvat-ai/cvat/pull/7890>)
14 changes: 14 additions & 0 deletions cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ def validate(self, attrs):

return attrs

@staticmethod
def check_attribute_names_unique(attrs):
encountered_names = set()
for attribute in attrs:
attr_name = attribute.get('name')
if attr_name in encountered_names:
raise serializers.ValidationError(f"Duplicate attribute with name '{attr_name}' exists")
else:
encountered_names.add(attr_name)

@classmethod
@transaction.atomic
def update_label(
Expand All @@ -336,6 +346,8 @@ def update_label(

attributes = validated_data.pop('attributespec_set', [])

cls.check_attribute_names_unique(attributes)

if validated_data.get('id') is not None:
try:
db_label = models.Label.objects.get(id=validated_data['id'], **parent_info)
Expand Down Expand Up @@ -451,6 +463,8 @@ def create_labels(cls,
for label in labels:
attributes = label.pop('attributespec_set')

cls.check_attribute_names_unique(attributes)

if label.get('id', None):
del label['id']

Expand Down
Loading