Skip to content

[fix] Fixed ssid boolean coercion#381

Merged
nemesifier merged 2 commits intoopenwisp:masterfrom
Viscous106:fix-ssid-true-coercion
Mar 12, 2026
Merged

[fix] Fixed ssid boolean coercion#381
nemesifier merged 2 commits intoopenwisp:masterfrom
Viscous106:fix-ssid-true-coercion

Conversation

@Viscous106
Copy link
Copy Markdown
Contributor

@Viscous106 Viscous106 commented Mar 10, 2026

The naive .replace("True", "1") in cleanup() was incorrectly replacing substrings, e.g. SSID "TrueGait Living Guest" → "1Gait Living Guest".

Update: As suggested in the review, fixing this via string replacement in cleanup() is flawed because a real boolean True and a string exactly equal to "True" both arrive as the same string indistinguishable from one another.

The proper fix implemented here handles boolean coercion at the Jinja2 template level using the is sameas true / is sameas false type checks, and removes the boolean str.replace from cleanup() entirely. This ensures Python booleans are coerced to 1/0, while string values (like an SSID literally named "True") remain untouched as strings.

Fixes #383

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 10, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Removes boolean-substitution logic from the OpenWrt renderer and adds explicit boolean handling to the openwrt.jinja2 template so true/false values are rendered as '1'/'0' when the value is a non-string scalar. Adds unit tests that render multiple SSID variants (including strings containing "True") to ensure SSIDs are preserved and booleans are handled correctly. No public API or method signatures changed; indentation normalization and other post-processing are unchanged.

Sequence Diagram(s)

(Skipped — conditions for diagram generation not met.)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • #250 — SSID containing substring True converted to 1 in generated OpenWrt configuration
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR successfully addresses issue #250 by implementing template-level boolean coercion using 'is sameas' checks and removing problematic string replacement from cleanup(), preventing string values like 'TrueGait Living Guest' from being corrupted.
Out of Scope Changes check ✅ Passed All changes are focused on fixing the SSID true coercion issue: updates to OpenWrt and OpenWisp templates to handle booleans correctly, removal of problematic cleanup logic, and addition of test cases to verify the fix.
Description check ✅ Passed The pull request description clearly explains the problem, the original flawed approach, and the proper fix implemented using Jinja2 template-level boolean handling.
Title check ✅ Passed The title '[fix] Fixed ssid boolean coercion' directly relates to the main changeset objective: fixing a bug where string values containing 'True' were being incorrectly converted to '1' during OpenWrt/OpenWiSP configuration rendering. The changes move boolean coercion logic from string replacement in cleanup() to type-aware Jinja2 templates, which is the core fix.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@netjsonconfig/backends/openwrt/renderer.py`:
- Line 1: Replace the literal-regex calls to re.sub that change the strings
'True' and 'False' in netjsonconfig/backends/openwrt/renderer.py with
str.replace for simplicity and performance (i.e., swap each re.sub(...) that
targets the exact text 'True' or 'False' to use .replace(...)), ensure
replacement order doesn't accidentally re-replace results, and remove the
now-unused import re if there are no other regex uses in the file.

In `@tests/openwrt/test_wireless.py`:
- Around line 66-78: Add edge-case unit tests in tests/openwrt/test_wireless.py
to cover SSIDs that are exactly "True" and "False" and an SSID with "True" as a
mid-word (e.g., "MyTrueNetwork"); copy the pattern used by
test_render_wifi_issue_250_true_ssid/test_render_wifi_issue_250_false_ssid:
deepcopy self._wifi_netjson, set netjson["interfaces"][0]["wireless"]["ssid"] to
the exact value, create OpenWrt(netjson), compute expected_uci by replacing
"'MyWifiAP'" in self._wifi_uci with the new quoted SSID, and assert o.render()
== self._tabs(expected_uci) so the OpenWrt.render behavior is verified for these
edge cases.
- Around line 66-80: There are two consecutive blank lines between the test
methods test_render_wifi_issue_250_true_ssid and
test_render_wifi_issue_250_false_ssid; remove the extra blank line so there is
exactly one blank line separating the method definitions in
tests/openwrt/test_wireless.py to satisfy PEP8 formatting.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 7260d4bf-0f44-48f9-8fdd-a02ee4fcc58a

📥 Commits

Reviewing files that changed from the base of the PR and between 99225ad and 35c202a.

📒 Files selected for processing (2)
  • netjsonconfig/backends/openwrt/renderer.py
  • tests/openwrt/test_wireless.py
📜 Review details
🧰 Additional context used
🪛 GitHub Actions: Netjsonconfig CI Build
tests/openwrt/test_wireless.py

[error] 74-74: Black check failed! Hint: did you forget to run openwisp-qa-format?


[error] 81-81: E303 too many blank lines (2)

Comment thread netjsonconfig/backends/openwrt/renderer.py Outdated
Comment thread tests/openwrt/test_wireless.py Outdated
Comment thread tests/openwrt/test_wireless.py Outdated
@Viscous106 Viscous106 force-pushed the fix-ssid-true-coercion branch 2 times, most recently from 56df23b to ec90d55 Compare March 11, 2026 04:51
@coveralls
Copy link
Copy Markdown

coveralls commented Mar 11, 2026

Coverage Status

coverage: 99.18%. remained the same
when pulling ccd2585 on Viscous106:fix-ssid-true-coercion
into 9a91092 on openwisp:master.

@Viscous106 Viscous106 force-pushed the fix-ssid-true-coercion branch from a453bbf to 6277cad Compare March 11, 2026 05:15
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@netjsonconfig/backends/openwrt/renderer.py`:
- Around line 19-20: The current cleanup() in renderer.py mutates the rendered
string and therefore converts user strings like "True"/"False" as well as real
booleans; instead move boolean coercion to a typed stage by either updating
netjsonconfig/backends/openwrt/templates/openwrt.jinja2 to render booleans as
1/0 (use Jinja conditional or a custom filter) or normalizing booleans in the
renderer's input data before templating; locate the cleanup() call and the
output variable in renderer.py and remove the "'True'"/"'False'" replace, then
implement the conversion in the template (or in the data-prep function that
feeds the template) so only actual boolean values become 1/0 while literal
strings remain unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: baf8f36a-efa8-44a3-86e1-146bad74dfed

📥 Commits

Reviewing files that changed from the base of the PR and between ec90d55 and 6277cad.

📒 Files selected for processing (2)
  • netjsonconfig/backends/openwrt/renderer.py
  • tests/openwrt/test_wireless.py
📜 Review details
🔇 Additional comments (1)
tests/openwrt/test_wireless.py (1)

66-85: Good regression coverage for the reported substring cases.

These tests cover the user-facing failure mode with spaced and mid-word SSIDs without overfitting to implementation details.

Comment thread netjsonconfig/backends/openwrt/renderer.py Outdated
@Viscous106 Viscous106 force-pushed the fix-ssid-true-coercion branch from 6277cad to 6e9d79d Compare March 11, 2026 05:31
@Viscous106 Viscous106 marked this pull request as ready for review March 11, 2026 05:40
Copilot AI review requested due to automatic review settings March 11, 2026 05:41
coderabbitai[bot]
coderabbitai Bot previously approved these changes Mar 11, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect coercion of SSIDs containing "True"/"False" by moving boolean-to-1/0 rendering out of post-processing and into the Jinja2 templates (where actual boolean types can be detected reliably).

Changes:

  • Add explicit boolean rendering in OpenWrt/OpenWisp UCI Jinja2 templates via is sameas true/false.
  • Remove the fragile output.replace("True", "1").replace("False", "0") logic from OpenWrtRenderer.cleanup().
  • Add OpenWrt regression tests covering SSIDs that contain or equal "True"/"False".

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/openwrt/test_wireless.py Adds regression tests ensuring SSID strings aren’t altered by boolean coercion.
netjsonconfig/backends/openwrt/templates/openwrt.jinja2 Implements type-aware boolean coercion at template render time.
netjsonconfig/backends/openwrt/renderer.py Removes substring-based boolean replacement from cleanup.
netjsonconfig/backends/openwisp/templates/openwrt.jinja2 Mirrors the OpenWrt template boolean coercion for OpenWisp output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/openwrt/test_wireless.py Outdated
Comment thread netjsonconfig/backends/openwisp/templates/openwrt.jinja2 Outdated
coderabbitai[bot]
coderabbitai Bot previously approved these changes Mar 11, 2026
@Viscous106 Viscous106 force-pushed the fix-ssid-true-coercion branch from 5eacd3c to 2243fd9 Compare March 12, 2026 05:17
@Viscous106 Viscous106 force-pushed the fix-ssid-true-coercion branch from 2243fd9 to ecdc1e5 Compare March 12, 2026 07:15
@nemesifier nemesifier added the bug label Mar 12, 2026
@nemesifier nemesifier changed the title Fix ssid true coercion [fix] Fixed ssid boolean coercion Mar 12, 2026
Copy link
Copy Markdown
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Viscous106 thanks, I simplified a bit the if/else chain in the jinja2 templates, should be good to merge now.

@nemesifier nemesifier merged commit 044d8a8 into openwisp:master Mar 12, 2026
8 checks passed
@nemesifier
Copy link
Copy Markdown
Member

/backport 1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] SSID containing substring True converted to 1 in generated OpenWrt configuration

4 participants