Skip to content

Validate label types in the serializer #8980

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

Merged
merged 2 commits into from
Jan 28, 2025
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
4 changes: 4 additions & 0 deletions changelog.d/20250122_142453_roman_labeltype_choicefield.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Fixed

- The backend now rejects invalid label types
(<https://github.com/cvat-ai/cvat/pull/8980>)
35 changes: 35 additions & 0 deletions cvat/apps/engine/migrations/0087_alter_label_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.2.17 on 2025-01-22 13:48

from django.db import migrations, models

import cvat.apps.engine.models


class Migration(migrations.Migration):

dependencies = [
("engine", "0086_profile_has_analytics_access"),
]

operations = [
migrations.AlterField(
model_name="label",
name="type",
field=models.CharField(
choices=[
("any", "ANY"),
("cuboid", "CUBOID"),
("ellipse", "ELLIPSE"),
("mask", "MASK"),
("points", "POINTS"),
("polygon", "POLYGON"),
("polyline", "POLYLINE"),
("rectangle", "RECTANGLE"),
("skeleton", "SKELETON"),
("tag", "TAG"),
],
default=cvat.apps.engine.models.LabelType["ANY"],
max_length=32,
),
),
]
12 changes: 6 additions & 6 deletions cvat/apps/engine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ def __str__(self):
return self.value

class LabelType(str, Enum):
BBOX = 'bbox'
ANY = 'any'
CUBOID = 'cuboid'
ELLIPSE = 'ellipse'
MASK = 'mask'
POINTS = 'points'
POLYGON = 'polygon'
POLYLINE = 'polyline'
POINTS = 'points'
CUBOID = 'cuboid'
CUBOID_3D = 'cuboid_3d'
RECTANGLE = 'rectangle'
SKELETON = 'skeleton'
TAG = 'tag'
ANY = 'any'

@classmethod
def choices(cls):
Expand Down Expand Up @@ -946,7 +946,7 @@ class Label(models.Model):
project = models.ForeignKey(Project, null=True, blank=True, on_delete=models.CASCADE)
name = SafeCharField(max_length=64)
color = models.CharField(default='', max_length=8)
type = models.CharField(max_length=32, null=True, choices=LabelType.choices(), default=LabelType.ANY)
type = models.CharField(max_length=32, choices=LabelType.choices(), default=LabelType.ANY)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='sublabels')

def __str__(self):
Expand Down
4 changes: 2 additions & 2 deletions cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class SublabelSerializer(serializers.ModelSerializer):
color = serializers.CharField(allow_blank=True, required=False,
help_text="The hex value for the RGB color. "
"Will be generated automatically, unless specified explicitly.")
type = serializers.CharField(allow_blank=True, required=False,
type = serializers.ChoiceField(choices=models.LabelType.choices(), required=False,
help_text="Associated annotation type for this label")
has_parent = serializers.BooleanField(source='has_parent_label', required=False)

Expand Down Expand Up @@ -418,7 +418,7 @@ def update_label(
try:
db_label = models.Label.create(
name=validated_data.get('name'),
type=validated_data.get('type'),
type=validated_data.get('type', models.LabelType.ANY),
parent=parent_label,
**parent_info
)
Expand Down
102 changes: 89 additions & 13 deletions cvat/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2809,16 +2809,16 @@ paths:
schema:
type: string
enum:
- bbox
- any
- cuboid
- ellipse
- mask
- points
- polygon
- polyline
- points
- cuboid
- cuboid_3d
- rectangle
- skeleton
- tag
- any
tags:
- labels
security:
Expand Down Expand Up @@ -8487,8 +8487,21 @@ components:
description: The list of attributes. If you want to remove an attribute,
you need to recreate the label and specify the remaining attributes.
type:
type: string
description: Associated annotation type for this label
allOf:
- $ref: '#/components/schemas/LabelType'
description: |-
Associated annotation type for this label

* `any` - ANY
* `cuboid` - CUBOID
* `ellipse` - ELLIPSE
* `mask` - MASK
* `points` - POINTS
* `polygon` - POLYGON
* `polyline` - POLYLINE
* `rectangle` - RECTANGLE
* `skeleton` - SKELETON
* `tag` - TAG
svg:
type: string
sublabels:
Expand Down Expand Up @@ -8531,6 +8544,30 @@ components:
a parent label
required:
- name
LabelType:
enum:
- any
- cuboid
- ellipse
- mask
- points
- polygon
- polyline
- rectangle
- skeleton
- tag
type: string
description: |-
* `any` - ANY
* `cuboid` - CUBOID
* `ellipse` - ELLIPSE
* `mask` - MASK
* `points` - POINTS
* `polygon` - POLYGON
* `polyline` - POLYLINE
* `rectangle` - RECTANGLE
* `skeleton` - SKELETON
* `tag` - TAG
LabeledData:
type: object
properties:
Expand Down Expand Up @@ -9589,8 +9626,21 @@ components:
description: Delete the label. Only applicable in the PATCH methods of a
project or a task.
type:
type: string
description: Associated annotation type for this label
allOf:
- $ref: '#/components/schemas/LabelType'
description: |-
Associated annotation type for this label

* `any` - ANY
* `cuboid` - CUBOID
* `ellipse` - ELLIPSE
* `mask` - MASK
* `points` - POINTS
* `polygon` - POLYGON
* `polyline` - POLYLINE
* `rectangle` - RECTANGLE
* `skeleton` - SKELETON
* `tag` - TAG
svg:
type: string
sublabels:
Expand Down Expand Up @@ -10764,8 +10814,21 @@ components:
description: The list of attributes. If you want to remove an attribute,
you need to recreate the label and specify the remaining attributes.
type:
type: string
description: Associated annotation type for this label
allOf:
- $ref: '#/components/schemas/LabelType'
description: |-
Associated annotation type for this label

* `any` - ANY
* `cuboid` - CUBOID
* `ellipse` - ELLIPSE
* `mask` - MASK
* `points` - POINTS
* `polygon` - POLYGON
* `polyline` - POLYLINE
* `rectangle` - RECTANGLE
* `skeleton` - SKELETON
* `tag` - TAG
has_parent:
type: boolean
required:
Expand Down Expand Up @@ -10804,8 +10867,21 @@ components:
description: The list of attributes. If you want to remove an attribute,
you need to recreate the label and specify the remaining attributes.
type:
type: string
description: Associated annotation type for this label
allOf:
- $ref: '#/components/schemas/LabelType'
description: |-
Associated annotation type for this label

* `any` - ANY
* `cuboid` - CUBOID
* `ellipse` - ELLIPSE
* `mask` - MASK
* `points` - POINTS
* `polygon` - POLYGON
* `polyline` - POLYLINE
* `rectangle` - RECTANGLE
* `skeleton` - SKELETON
* `tag` - TAG
has_parent:
type: boolean
required:
Expand Down
1 change: 1 addition & 0 deletions cvat/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ class CVAT_QUEUES(Enum):
'COMPONENT_SPLIT_REQUEST': True,

'ENUM_NAME_OVERRIDES': {
'LabelType': 'cvat.apps.engine.models.LabelType',
'ShapeType': 'cvat.apps.engine.models.ShapeType',
'OperationStatus': 'cvat.apps.engine.models.StateChoice',
'ChunkType': 'cvat.apps.engine.models.DataChoice',
Expand Down
2 changes: 1 addition & 1 deletion site/content/en/docs/manual/advanced/xml_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ For what is `rle`, see [Run-length encoding](https://en.wikipedia.org/wiki/Run-l
<labels>
<label>
<name>String: name of the label (e.g. car, person)</name>
<type>String: any, bbox, cuboid, cuboid_3d, ellipse, mask, polygon, polyline, points, skeleton, tag</type>
<type>String: any, bbox, cuboid, ellipse, mask, polygon, polyline, points, skeleton, tag</type>
<attributes>
<attribute>
<name>String: attribute name</name>
Expand Down
10 changes: 5 additions & 5 deletions tests/python/rest_api/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,16 +644,16 @@ def _get_patch_data(
"color": ["#2000c0"],
"name": ["modified"],
"type": [
"bbox",
"any",
"cuboid",
"ellipse",
"mask",
"points",
"polygon",
"polyline",
"points",
"cuboid",
"cuboid_3d",
"rectangle",
"skeleton",
"tag",
"any",
],
}.items()
)
Expand Down
Loading