Skip to content

Commit 7e3f802

Browse files
authored
Validate label types in the serializer (#8980)
1 parent d0e0359 commit 7e3f802

File tree

8 files changed

+143
-27
lines changed

8 files changed

+143
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Fixed
2+
3+
- The backend now rejects invalid label types
4+
(<https://github.com/cvat-ai/cvat/pull/8980>)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generated by Django 4.2.17 on 2025-01-22 13:48
2+
3+
from django.db import migrations, models
4+
5+
import cvat.apps.engine.models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
("engine", "0086_profile_has_analytics_access"),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name="label",
17+
name="type",
18+
field=models.CharField(
19+
choices=[
20+
("any", "ANY"),
21+
("cuboid", "CUBOID"),
22+
("ellipse", "ELLIPSE"),
23+
("mask", "MASK"),
24+
("points", "POINTS"),
25+
("polygon", "POLYGON"),
26+
("polyline", "POLYLINE"),
27+
("rectangle", "RECTANGLE"),
28+
("skeleton", "SKELETON"),
29+
("tag", "TAG"),
30+
],
31+
default=cvat.apps.engine.models.LabelType["ANY"],
32+
max_length=32,
33+
),
34+
),
35+
]

cvat/apps/engine/models.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ def __str__(self):
7272
return self.value
7373

7474
class LabelType(str, Enum):
75-
BBOX = 'bbox'
75+
ANY = 'any'
76+
CUBOID = 'cuboid'
7677
ELLIPSE = 'ellipse'
78+
MASK = 'mask'
79+
POINTS = 'points'
7780
POLYGON = 'polygon'
7881
POLYLINE = 'polyline'
79-
POINTS = 'points'
80-
CUBOID = 'cuboid'
81-
CUBOID_3D = 'cuboid_3d'
82+
RECTANGLE = 'rectangle'
8283
SKELETON = 'skeleton'
8384
TAG = 'tag'
84-
ANY = 'any'
8585

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

952952
def __str__(self):

cvat/apps/engine/serializers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class SublabelSerializer(serializers.ModelSerializer):
288288
color = serializers.CharField(allow_blank=True, required=False,
289289
help_text="The hex value for the RGB color. "
290290
"Will be generated automatically, unless specified explicitly.")
291-
type = serializers.CharField(allow_blank=True, required=False,
291+
type = serializers.ChoiceField(choices=models.LabelType.choices(), required=False,
292292
help_text="Associated annotation type for this label")
293293
has_parent = serializers.BooleanField(source='has_parent_label', required=False)
294294

@@ -418,7 +418,7 @@ def update_label(
418418
try:
419419
db_label = models.Label.create(
420420
name=validated_data.get('name'),
421-
type=validated_data.get('type'),
421+
type=validated_data.get('type', models.LabelType.ANY),
422422
parent=parent_label,
423423
**parent_info
424424
)

cvat/schema.yml

+89-13
Original file line numberDiff line numberDiff line change
@@ -2809,16 +2809,16 @@ paths:
28092809
schema:
28102810
type: string
28112811
enum:
2812-
- bbox
2812+
- any
2813+
- cuboid
28132814
- ellipse
2815+
- mask
2816+
- points
28142817
- polygon
28152818
- polyline
2816-
- points
2817-
- cuboid
2818-
- cuboid_3d
2819+
- rectangle
28192820
- skeleton
28202821
- tag
2821-
- any
28222822
tags:
28232823
- labels
28242824
security:
@@ -8487,8 +8487,21 @@ components:
84878487
description: The list of attributes. If you want to remove an attribute,
84888488
you need to recreate the label and specify the remaining attributes.
84898489
type:
8490-
type: string
8491-
description: Associated annotation type for this label
8490+
allOf:
8491+
- $ref: '#/components/schemas/LabelType'
8492+
description: |-
8493+
Associated annotation type for this label
8494+
8495+
* `any` - ANY
8496+
* `cuboid` - CUBOID
8497+
* `ellipse` - ELLIPSE
8498+
* `mask` - MASK
8499+
* `points` - POINTS
8500+
* `polygon` - POLYGON
8501+
* `polyline` - POLYLINE
8502+
* `rectangle` - RECTANGLE
8503+
* `skeleton` - SKELETON
8504+
* `tag` - TAG
84928505
svg:
84938506
type: string
84948507
sublabels:
@@ -8531,6 +8544,30 @@ components:
85318544
a parent label
85328545
required:
85338546
- name
8547+
LabelType:
8548+
enum:
8549+
- any
8550+
- cuboid
8551+
- ellipse
8552+
- mask
8553+
- points
8554+
- polygon
8555+
- polyline
8556+
- rectangle
8557+
- skeleton
8558+
- tag
8559+
type: string
8560+
description: |-
8561+
* `any` - ANY
8562+
* `cuboid` - CUBOID
8563+
* `ellipse` - ELLIPSE
8564+
* `mask` - MASK
8565+
* `points` - POINTS
8566+
* `polygon` - POLYGON
8567+
* `polyline` - POLYLINE
8568+
* `rectangle` - RECTANGLE
8569+
* `skeleton` - SKELETON
8570+
* `tag` - TAG
85348571
LabeledData:
85358572
type: object
85368573
properties:
@@ -9589,8 +9626,21 @@ components:
95899626
description: Delete the label. Only applicable in the PATCH methods of a
95909627
project or a task.
95919628
type:
9592-
type: string
9593-
description: Associated annotation type for this label
9629+
allOf:
9630+
- $ref: '#/components/schemas/LabelType'
9631+
description: |-
9632+
Associated annotation type for this label
9633+
9634+
* `any` - ANY
9635+
* `cuboid` - CUBOID
9636+
* `ellipse` - ELLIPSE
9637+
* `mask` - MASK
9638+
* `points` - POINTS
9639+
* `polygon` - POLYGON
9640+
* `polyline` - POLYLINE
9641+
* `rectangle` - RECTANGLE
9642+
* `skeleton` - SKELETON
9643+
* `tag` - TAG
95949644
svg:
95959645
type: string
95969646
sublabels:
@@ -10764,8 +10814,21 @@ components:
1076410814
description: The list of attributes. If you want to remove an attribute,
1076510815
you need to recreate the label and specify the remaining attributes.
1076610816
type:
10767-
type: string
10768-
description: Associated annotation type for this label
10817+
allOf:
10818+
- $ref: '#/components/schemas/LabelType'
10819+
description: |-
10820+
Associated annotation type for this label
10821+
10822+
* `any` - ANY
10823+
* `cuboid` - CUBOID
10824+
* `ellipse` - ELLIPSE
10825+
* `mask` - MASK
10826+
* `points` - POINTS
10827+
* `polygon` - POLYGON
10828+
* `polyline` - POLYLINE
10829+
* `rectangle` - RECTANGLE
10830+
* `skeleton` - SKELETON
10831+
* `tag` - TAG
1076910832
has_parent:
1077010833
type: boolean
1077110834
required:
@@ -10804,8 +10867,21 @@ components:
1080410867
description: The list of attributes. If you want to remove an attribute,
1080510868
you need to recreate the label and specify the remaining attributes.
1080610869
type:
10807-
type: string
10808-
description: Associated annotation type for this label
10870+
allOf:
10871+
- $ref: '#/components/schemas/LabelType'
10872+
description: |-
10873+
Associated annotation type for this label
10874+
10875+
* `any` - ANY
10876+
* `cuboid` - CUBOID
10877+
* `ellipse` - ELLIPSE
10878+
* `mask` - MASK
10879+
* `points` - POINTS
10880+
* `polygon` - POLYGON
10881+
* `polyline` - POLYLINE
10882+
* `rectangle` - RECTANGLE
10883+
* `skeleton` - SKELETON
10884+
* `tag` - TAG
1080910885
has_parent:
1081010886
type: boolean
1081110887
required:

cvat/settings/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ class CVAT_QUEUES(Enum):
662662
'COMPONENT_SPLIT_REQUEST': True,
663663

664664
'ENUM_NAME_OVERRIDES': {
665+
'LabelType': 'cvat.apps.engine.models.LabelType',
665666
'ShapeType': 'cvat.apps.engine.models.ShapeType',
666667
'OperationStatus': 'cvat.apps.engine.models.StateChoice',
667668
'ChunkType': 'cvat.apps.engine.models.DataChoice',

site/content/en/docs/manual/advanced/xml_format.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ For what is `rle`, see [Run-length encoding](https://en.wikipedia.org/wiki/Run-l
4040
<labels>
4141
<label>
4242
<name>String: name of the label (e.g. car, person)</name>
43-
<type>String: any, bbox, cuboid, cuboid_3d, ellipse, mask, polygon, polyline, points, skeleton, tag</type>
43+
<type>String: any, bbox, cuboid, ellipse, mask, polygon, polyline, points, skeleton, tag</type>
4444
<attributes>
4545
<attribute>
4646
<name>String: attribute name</name>

tests/python/rest_api/test_labels.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -644,16 +644,16 @@ def _get_patch_data(
644644
"color": ["#2000c0"],
645645
"name": ["modified"],
646646
"type": [
647-
"bbox",
647+
"any",
648+
"cuboid",
648649
"ellipse",
650+
"mask",
651+
"points",
649652
"polygon",
650653
"polyline",
651-
"points",
652-
"cuboid",
653-
"cuboid_3d",
654+
"rectangle",
654655
"skeleton",
655656
"tag",
656-
"any",
657657
],
658658
}.items()
659659
)

0 commit comments

Comments
 (0)