From 2c26774c625da91f907804ee9bd7f5d9d2a06659 Mon Sep 17 00:00:00 2001 From: Pablo Chinea Date: Wed, 4 Jan 2017 16:23:17 +0000 Subject: [PATCH 1/2] Avoid collisions in choices names conversion. --- graphene_django/converter.py | 4 ++++ graphene_django/tests/test_converter.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index 706d968..addaec3 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -27,12 +27,16 @@ def convert_choice_name(name): def get_choices(choices): + converted_names = [] for value, help_text in choices: if isinstance(help_text, (tuple, list)): for choice in get_choices(help_text): yield choice else: name = convert_choice_name(value) + if name in converted_names: + name += '_' + str(len(converted_names)) + converted_names.append(name) description = help_text yield name, value, description diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index 69be406..b3ddf40 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -176,6 +176,21 @@ def test_field_with_choices_gettext(): convert_django_field_with_choices(field) +def test_field_with_choices_collision(): + field = models.CharField(help_text='Timezone', choices=( + ('Etc/GMT+1', 'Greenwich Mean Time +1'), + ('Etc/GMT-1', 'Greenwich Mean Time -1'), + )) + + class CollisionChoicesModel(models.Model): + timezone = field + + class Meta: + app_label = 'test' + + convert_django_field_with_choices(field) + + def test_should_float_convert_float(): assert_conversion(models.FloatField, graphene.Float) From 89d0f0ca926a1786cb84175f3c87203a736a9069 Mon Sep 17 00:00:00 2001 From: Pablo Chinea Date: Thu, 5 Jan 2017 09:49:26 +0000 Subject: [PATCH 2/2] Handles multiple collisions with the same key. --- graphene_django/converter.py | 2 +- graphene_django/tests/test_converter.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index addaec3..92812d1 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -34,7 +34,7 @@ def get_choices(choices): yield choice else: name = convert_choice_name(value) - if name in converted_names: + while name in converted_names: name += '_' + str(len(converted_names)) converted_names.append(name) description = help_text diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index b3ddf40..997b03c 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -178,6 +178,7 @@ def test_field_with_choices_gettext(): def test_field_with_choices_collision(): field = models.CharField(help_text='Timezone', choices=( + ('Etc/GMT+1+2', 'Fake choice to produce double collision'), ('Etc/GMT+1', 'Greenwich Mean Time +1'), ('Etc/GMT-1', 'Greenwich Mean Time -1'), ))