From df7955a223d6f2f302cca6438ac5482d7fb322e3 Mon Sep 17 00:00:00 2001 From: Yash Virulkar <171834632+Viscous106@users.noreply.github.com> Date: Fri, 13 Mar 2026 03:11:22 +0530 Subject: [PATCH] [fix] Fixed naive boolean coercion #383 Fixes #383 --------- Co-authored-by: Federico Capoano (cherry picked from commit 044d8a854bd3f7bd06dec11291c3c084cc89ba15) --- .../backends/openwisp/templates/openwrt.jinja2 | 2 ++ netjsonconfig/backends/openwrt/renderer.py | 2 -- .../backends/openwrt/templates/openwrt.jinja2 | 2 ++ tests/openwisp/test_backend.py | 17 +++++++++++++++++ tests/openwrt/test_wireless.py | 17 +++++++++++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/netjsonconfig/backends/openwisp/templates/openwrt.jinja2 b/netjsonconfig/backends/openwisp/templates/openwrt.jinja2 index 876b27353..5b1039d40 100644 --- a/netjsonconfig/backends/openwisp/templates/openwrt.jinja2 +++ b/netjsonconfig/backends/openwisp/templates/openwrt.jinja2 @@ -9,6 +9,8 @@ {% for list_value in value %} list '{{ key }}' '{{ list_value }}' {% endfor %} + {% elif value is boolean %} + option '{{ key }}' '{{ value | int }}' {% else %} option '{{ key }}' '{{ value }}' {% endif %} diff --git a/netjsonconfig/backends/openwrt/renderer.py b/netjsonconfig/backends/openwrt/renderer.py index 1ce93e687..1a56578cd 100644 --- a/netjsonconfig/backends/openwrt/renderer.py +++ b/netjsonconfig/backends/openwrt/renderer.py @@ -16,8 +16,6 @@ def cleanup(self, output): .replace("\noption", "\n\toption") .replace("\nlist", "\n\tlist") ) - # convert True to 1 and False to 0 - output = output.replace("True", "1").replace("False", "0") # max 2 consecutive \n delimiters output = output.replace("\n\n\n", "\n\n") # if output is present diff --git a/netjsonconfig/backends/openwrt/templates/openwrt.jinja2 b/netjsonconfig/backends/openwrt/templates/openwrt.jinja2 index 9c267c8d7..b44d7d02d 100644 --- a/netjsonconfig/backends/openwrt/templates/openwrt.jinja2 +++ b/netjsonconfig/backends/openwrt/templates/openwrt.jinja2 @@ -9,6 +9,8 @@ {% for list_value in value %} list {{ key }} '{{ list_value }}' {% endfor %} + {% elif value is boolean %} + option {{ key }} '{{ value | int }}' {% else %} option {{ key }} '{{ value }}' {% endif %} diff --git a/tests/openwisp/test_backend.py b/tests/openwisp/test_backend.py index 61c00a67a..c4157881e 100644 --- a/tests/openwisp/test_backend.py +++ b/tests/openwisp/test_backend.py @@ -196,6 +196,23 @@ def test_wireless_radio_disabled_0(self): output = o.render() self.assertIn("option 'disabled' '0'", output) + def test_render_ssid_boolean_bug(self): + """Regression test for https://github.com/openwisp/netjsonconfig/issues/383""" + ssid_values = [ + "TrueGait Living Guest", + "FalseGait Living Guest", + "MyTrueNetwork", + "True", + "False", + ] + for ssid in ssid_values: + with self.subTest(ssid=ssid): + config = deepcopy(self.config) + config["interfaces"][2]["wireless"]["ssid"] = ssid + o = OpenWisp(config) + output = o.render() + self.assertIn(f"option 'ssid' '{ssid}'", output) + def test_tc_script(self): config = deepcopy(self.config) o = OpenWisp(config) diff --git a/tests/openwrt/test_wireless.py b/tests/openwrt/test_wireless.py index 2936cf78b..d8214b613 100644 --- a/tests/openwrt/test_wireless.py +++ b/tests/openwrt/test_wireless.py @@ -63,6 +63,23 @@ def test_render_wifi_interface(self): expected = self._tabs(self._wifi_uci) self.assertEqual(o.render(), expected) + def test_render_ssid_boolean_bug(self): + """Regression test for https://github.com/openwisp/netjsonconfig/issues/383""" + ssid_values = [ + "TrueGait Living Guest", + "FalseGait Living Guest", + "MyTrueNetwork", + "True", + "False", + ] + for ssid in ssid_values: + with self.subTest(ssid=ssid): + netjson = deepcopy(self._wifi_netjson) + netjson["interfaces"][0]["wireless"]["ssid"] = ssid + o = OpenWrt(netjson) + expected_uci = self._wifi_uci.replace("'MyWifiAP'", f"'{ssid}'") + self.assertEqual(o.render(), self._tabs(expected_uci)) + def test_parse_wifi_interface(self): o = OpenWrt(native=self._wifi_uci) self.assertDictEqual(o.config, self._wifi_netjson)