Skip to content
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
12 changes: 10 additions & 2 deletions label_studio/core/label_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,16 @@ def _fix_choices(config):
workaround for single choice
https://github.com/heartexlabs/label-studio/issues/1259
'''
if 'Choices' in config and 'Choice' in config['Choices'] and not isinstance(config['Choices']['Choice'], list):
config['Choices']['Choice'] = [config['Choices']['Choice']]
if 'Choices' in config:
# for single Choices tag in View
if 'Choice' in config['Choices'] and not isinstance(config['Choices']['Choice'], list):
config['Choices']['Choice'] = [config['Choices']['Choice']]
# for several Choices tags in View
elif isinstance(config['Choices'], list) and all('Choice' in tag_choices for tag_choices in config['Choices']):
for n in range(len(config['Choices'])):
# check that Choices tag has only 1 choice
if not isinstance(config['Choices'][n]['Choice'], list):
config['Choices'][n]['Choice'] = [config['Choices'][n]['Choice']]
if 'View' in config:
if isinstance(config['View'], OrderedDict):
config['View'] = _fix_choices(config['View'])
Expand Down
25 changes: 25 additions & 0 deletions label_studio/tests/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,28 @@ def test_parse_all_configs():
assert parse_config(config)
assert parse_config_to_json(config)
validate_label_config(config)


@pytest.mark.django_db
def test_config_validation_for_choices_workaround(business_client, project_id):
"""
Validate Choices tag for 1 choice with workaround
Example bug DEV-3635
"""
payload = {
'label_config': '<View><Text name="artist" /><View><Choices name="choices_1" toName="artist"><Choice name="choice_1" value="1"/></Choices></View><View><Choices name="choices_2" toName="artist"><Choice name="choice_2" value="2"/></Choices></View></View>'}
response = business_client.patch(
f"/api/projects/{project_id}",
data=json.dumps(payload),
content_type="application/json",
)
assert response.status_code == 200

payload = {
'label_config': '<View><Text name="artist" /><View><Choices name="choices_1" toName="artist"><Choice name="choice_1" value="1"/></Choices><Choices name="choices_2" toName="artist"><Choice name="choice_2" value="2"/></Choices></View></View>'}
response = business_client.patch(
f"/api/projects/{project_id}",
data=json.dumps(payload),
content_type="application/json",
)
assert response.status_code == 200