Fix: Coerce language app config field to None if empty

This commit is contained in:
shamoon 2024-01-10 13:21:51 -08:00
parent 1ac298f6ff
commit b0aeec4c43
2 changed files with 7 additions and 2 deletions

View File

@ -76,10 +76,10 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
config = ApplicationConfiguration.objects.first()
self.assertEqual(config.color_conversion_strategy, ColorConvertChoices.RGB)
def test_api_update_config_empty_json_field(self):
def test_api_update_config_empty_fields(self):
"""
GIVEN:
- API request to update app config with empty string for user_args JSONField
- API request to update app config with empty string for user_args JSONField and language field
WHEN:
- API is called
THEN:
@ -91,6 +91,7 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
json.dumps(
{
"user_args": "",
"language": "",
},
),
content_type="application/json",
@ -98,3 +99,4 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
config = ApplicationConfiguration.objects.first()
self.assertEqual(config.user_args, None)
self.assertEqual(config.language, None)

View File

@ -125,8 +125,11 @@ class ApplicationConfigurationSerializer(serializers.ModelSerializer):
user_args = serializers.JSONField(binary=True, allow_null=True)
def run_validation(self, data):
# Empty strings treated as None to avoid unexpected behavior
if "user_args" in data and data["user_args"] == "":
data["user_args"] = None
if "language" in data and data["language"] == "":
data["language"] = None
return super().run_validation(data)
class Meta: