From 9abe1cafbeab88acef84c96775580c25440da60a Mon Sep 17 00:00:00 2001 From: Gaurav Pandey Date: Sat, 8 May 2021 21:06:47 +0530 Subject: [PATCH] api: Fix encoding of strings in display settings endpoint. * Remove unnecessary json validator for string validator. * Update frontend to pass right validator. * Update zulip.yaml to pass right parameter for curl request in openapi. * Update python_examples to pass right paramater. Fixes part of #18035. --- static/js/settings_display.js | 8 ++++---- zerver/openapi/python_examples.py | 2 +- zerver/openapi/zulip.yaml | 2 +- zerver/tests/test_settings.py | 6 +++--- zerver/views/user_settings.py | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/static/js/settings_display.js b/static/js/settings_display.js index 4eb39b855c..e97dbaf73a 100644 --- a/static/js/settings_display.js +++ b/static/js/settings_display.js @@ -126,7 +126,7 @@ export function set_up() { }); $("#default_view").on("change", function () { - const data = {default_view: JSON.stringify(this.value)}; + const data = {default_view: this.value}; change_display_setting(data, "#display-settings-status"); }); @@ -140,12 +140,12 @@ export function set_up() { }); $("#user_timezone").on("change", function () { - const data = {timezone: JSON.stringify(this.value)}; + const data = {timezone: this.value}; change_display_setting(data, "#time-settings-status"); }); $(".emojiset_choice").on("click", function () { - const data = {emojiset: JSON.stringify($(this).val())}; - const current_emojiset = JSON.stringify(page_params.emojiset); + const data = {emojiset: $(this).val()}; + const current_emojiset = page_params.emojiset; if (current_emojiset === data.emojiset) { return; } diff --git a/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.py index d703070c6f..b91df29daf 100644 --- a/zerver/openapi/python_examples.py +++ b/zerver/openapi/python_examples.py @@ -1131,7 +1131,7 @@ def update_display_settings(client: Client) -> None: # Change emoji set used for display to Google modern. request = { "left_side_userlist": True, - "emojiset": '"google"', + "emojiset": "google", } result = client.call_endpoint("settings/display", method="PATCH", request=request) # {code_example|end} diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index 00ee91c9c1..26329e2f4e 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -9636,7 +9636,7 @@ paths: * "text" - Plain text schema: type: string - example: '"google"' + example: "google" - name: demote_inactive_streams in: query description: | diff --git a/zerver/tests/test_settings.py b/zerver/tests/test_settings.py index 9649c8a8bb..93b93df65b 100644 --- a/zerver/tests/test_settings.py +++ b/zerver/tests/test_settings.py @@ -354,7 +354,7 @@ class ChangeSettingsTest(ZulipTestCase): else: invalid_value = "invalid_" + setting_name - if setting_name == "default_language": + if setting_name not in ["demote_inactive_streams", "color_scheme"]: data = {setting_name: test_value} else: data = {setting_name: orjson.dumps(test_value).decode()} @@ -366,7 +366,7 @@ class ChangeSettingsTest(ZulipTestCase): # Test to make sure invalid settings are not accepted # and saved in the db. - if setting_name == "default_language": + if setting_name not in ["demote_inactive_streams", "color_scheme"]: data = {setting_name: invalid_value} else: data = {setting_name: orjson.dumps(invalid_value).decode()} @@ -389,7 +389,7 @@ class ChangeSettingsTest(ZulipTestCase): def do_change_emojiset(self, emojiset: str) -> HttpResponse: self.login("hamlet") - data = {"emojiset": orjson.dumps(emojiset).decode()} + data = {"emojiset": emojiset} result = self.client_patch("/json/settings/display", data) return result diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py index f24753d6d5..c45d3d0e73 100644 --- a/zerver/views/user_settings.py +++ b/zerver/views/user_settings.py @@ -199,15 +199,15 @@ def update_display_settings_backend( translate_emoticons: Optional[bool] = REQ(json_validator=check_bool, default=None), default_language: Optional[str] = REQ(default=None), default_view: Optional[str] = REQ( - json_validator=check_string_in(default_view_options), default=None + str_validator=check_string_in(default_view_options), default=None ), left_side_userlist: Optional[bool] = REQ(json_validator=check_bool, default=None), - emojiset: Optional[str] = REQ(json_validator=check_string_in(emojiset_choices), default=None), + emojiset: Optional[str] = REQ(str_validator=check_string_in(emojiset_choices), default=None), demote_inactive_streams: Optional[int] = REQ( json_validator=check_int_in(UserProfile.DEMOTE_STREAMS_CHOICES), default=None ), timezone: Optional[str] = REQ( - json_validator=check_string_in(pytz.all_timezones_set), default=None + str_validator=check_string_in(pytz.all_timezones_set), default=None ), ) -> HttpResponse: