From cc04ab920214437a81d53ac93e82d7ec1eeb482a Mon Sep 17 00:00:00 2001 From: stktyagi Date: Tue, 10 Jun 2025 19:24:54 +0530 Subject: [PATCH 1/5] [Fix] Merging with wrong format now raises Validation error #351 An attempt to merge config with wrong format will now raise a validation error instead of failing. Fixes #351 --- netjsonconfig/utils.py | 28 ++++++++++++++++++++++++---- tests/openwrt/test_default.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/netjsonconfig/utils.py b/netjsonconfig/utils.py index de9a08bf9..65a042cfd 100644 --- a/netjsonconfig/utils.py +++ b/netjsonconfig/utils.py @@ -3,6 +3,12 @@ from copy import deepcopy +class ValidationError(Exception): + """exception for validation errors during config merge""" + + pass + + def merge_config(template, config, list_identifiers=None): """ Merges ``config`` on top of ``template``. @@ -23,10 +29,24 @@ def merge_config(template, config, list_identifiers=None): result = deepcopy(template) for key, value in config.items(): if isinstance(value, dict): - node = result.get(key, OrderedDict()) - result[key] = merge_config(node, value) - elif isinstance(value, list) and isinstance(result.get(key), list): - result[key] = merge_list(result[key], value, list_identifiers) + if isinstance(result.get(key), dict) or key not in result: + node = result.get(key, OrderedDict()) + result[key] = merge_config(node, value) + else: + raise ValidationError( + f"Cannot merge dict into non-dict for key '{key}': " + f"{type(result.get(key)).__name__} vs dict" + ) + elif isinstance(value, list): + if isinstance(result.get(key), list): + result[key] = merge_list(result[key], value, list_identifiers) + elif key in result: + raise ValidationError( + f"Cannot merge list into non-list for key '{key}': " + f"{type(result.get(key)).__name__} vs list" + ) + else: + result[key] = value else: result[key] = value return result diff --git a/tests/openwrt/test_default.py b/tests/openwrt/test_default.py index 9a7eb3eaa..5d65d93a6 100644 --- a/tests/openwrt/test_default.py +++ b/tests/openwrt/test_default.py @@ -3,7 +3,7 @@ from openwisp_utils.tests import capture_stdout from netjsonconfig import OpenWrt -from netjsonconfig.utils import _TabsMixin +from netjsonconfig.utils import ValidationError, _TabsMixin class TestDefault(unittest.TestCase, _TabsMixin): @@ -251,3 +251,30 @@ def test_render_invalid_uci_name(self): """ ) self.assertEqual(o.render(), expected) + + def test_merge_invalid_format(self): + invalid = { + "dhcp": { + "lan": { + "interface": "lan", + "start": 100, + "limit": 150, + "leasetime": "12h", + } + } + } + valid = { + "dhcp": [ + { + "dhcpv6": "disabled", + "ignore": True, + "ra": "disabled", + "config_value": "lan", + "config_name": "dhcp", + } + ] + } + with self.assertRaises(ValidationError) as context: + OpenWrt({}, templates=[valid, invalid]).validate() + + self.assertIn("Cannot merge dict into non-dict", str(context.exception)) From a9969bfdc4edd386715023db2fa7d726b829263b Mon Sep 17 00:00:00 2001 From: stktyagi Date: Sun, 26 Oct 2025 16:37:32 +0530 Subject: [PATCH 2/5] [Refactor] Change aggressive validation in merge_config #351 It only prevents the original AttributeError crash and correctly lets the .validate() method handle all validation of the final merged configuration. Fixes #351 --- netjsonconfig/utils.py | 29 ++++------------------------- tests/openwrt/test_default.py | 8 +++----- 2 files changed, 7 insertions(+), 30 deletions(-) diff --git a/netjsonconfig/utils.py b/netjsonconfig/utils.py index 65a042cfd..c91feeab8 100644 --- a/netjsonconfig/utils.py +++ b/netjsonconfig/utils.py @@ -3,12 +3,6 @@ from copy import deepcopy -class ValidationError(Exception): - """exception for validation errors during config merge""" - - pass - - def merge_config(template, config, list_identifiers=None): """ Merges ``config`` on top of ``template``. @@ -28,25 +22,10 @@ def merge_config(template, config, list_identifiers=None): """ result = deepcopy(template) for key, value in config.items(): - if isinstance(value, dict): - if isinstance(result.get(key), dict) or key not in result: - node = result.get(key, OrderedDict()) - result[key] = merge_config(node, value) - else: - raise ValidationError( - f"Cannot merge dict into non-dict for key '{key}': " - f"{type(result.get(key)).__name__} vs dict" - ) - elif isinstance(value, list): - if isinstance(result.get(key), list): - result[key] = merge_list(result[key], value, list_identifiers) - elif key in result: - raise ValidationError( - f"Cannot merge list into non-list for key '{key}': " - f"{type(result.get(key)).__name__} vs list" - ) - else: - result[key] = value + if isinstance(value, dict) and isinstance(result.get(key), dict): + result[key] = merge_config(result.get(key), value, list_identifiers) + elif isinstance(value, list) and isinstance(result.get(key), list): + result[key] = merge_list(result[key], value, list_identifiers) else: result[key] = value return result diff --git a/tests/openwrt/test_default.py b/tests/openwrt/test_default.py index 5d65d93a6..b5e487b20 100644 --- a/tests/openwrt/test_default.py +++ b/tests/openwrt/test_default.py @@ -3,7 +3,7 @@ from openwisp_utils.tests import capture_stdout from netjsonconfig import OpenWrt -from netjsonconfig.utils import ValidationError, _TabsMixin +from netjsonconfig.utils import _TabsMixin class TestDefault(unittest.TestCase, _TabsMixin): @@ -274,7 +274,5 @@ def test_merge_invalid_format(self): } ] } - with self.assertRaises(ValidationError) as context: - OpenWrt({}, templates=[valid, invalid]).validate() - - self.assertIn("Cannot merge dict into non-dict", str(context.exception)) + o = OpenWrt({}, templates=[valid, invalid]) + o.validate() From 9d0c8b132f20048913b10e946a8822a60266e7f3 Mon Sep 17 00:00:00 2001 From: stktyagi Date: Fri, 13 Mar 2026 11:14:07 +0530 Subject: [PATCH 3/5] [fix] Riase ValidationError on incompatible types #351 Merge_config now raises ValidationError when templates have incompatible types for the same key (e.g. list vs dict), instead of silently overwriting. Test updated to assert ValidationError. Fixes #351 --- netjsonconfig/utils.py | 12 ++++++++++++ tests/openwrt/test_default.py | 5 +++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/netjsonconfig/utils.py b/netjsonconfig/utils.py index c91feeab8..771c85bde 100644 --- a/netjsonconfig/utils.py +++ b/netjsonconfig/utils.py @@ -2,6 +2,10 @@ from collections import OrderedDict from copy import deepcopy +from jsonschema import ValidationError as JsonSchemaError + +from .exceptions import ValidationError + def merge_config(template, config, list_identifiers=None): """ @@ -19,6 +23,7 @@ def merge_config(template, config, list_identifiers=None): :param config: config ``dict`` :param list_identifiers: ``list`` or ``None`` :returns: merged ``dict`` + :raises ValidationError: if incompatible types are found """ result = deepcopy(template) for key, value in config.items(): @@ -26,6 +31,13 @@ def merge_config(template, config, list_identifiers=None): result[key] = merge_config(result.get(key), value, list_identifiers) elif isinstance(value, list) and isinstance(result.get(key), list): result[key] = merge_list(result[key], value, list_identifiers) + elif result.get(key) is not None and type(value) is not type(result.get(key)): + raise ValidationError( + JsonSchemaError( + f"incompatible types for '{key}': expected {type(result.get(key)).__name__}, " + f"got {type(value).__name__}" + ) + ) else: result[key] = value return result diff --git a/tests/openwrt/test_default.py b/tests/openwrt/test_default.py index 1bc2b7ea6..a22802b5b 100644 --- a/tests/openwrt/test_default.py +++ b/tests/openwrt/test_default.py @@ -3,6 +3,7 @@ from openwisp_utils.tests import capture_stdout from netjsonconfig import OpenWrt +from netjsonconfig.exceptions import ValidationError from netjsonconfig.utils import _TabsMixin @@ -268,5 +269,5 @@ def test_merge_invalid_format(self): } ] } - o = OpenWrt({}, templates=[valid, invalid]) - o.validate() + with self.assertRaises(ValidationError): + OpenWrt({}, templates=[valid, invalid]) From aae764512bbd62d2f9ea1c4233edf919224c2ecf Mon Sep 17 00:00:00 2001 From: stktyagi Date: Fri, 13 Mar 2026 11:25:22 +0530 Subject: [PATCH 4/5] [fix] Scope type check to container-type conflicts onl #351 Narrowed the incompatible type check in merge_config to only raise ValidationError when a dict or list conflicts with a different type. Scalar overrides (e.g. str to int) are still allowed as before. Fixes #351 --- netjsonconfig/utils.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/netjsonconfig/utils.py b/netjsonconfig/utils.py index 771c85bde..8088ca51c 100644 --- a/netjsonconfig/utils.py +++ b/netjsonconfig/utils.py @@ -27,14 +27,19 @@ def merge_config(template, config, list_identifiers=None): """ result = deepcopy(template) for key, value in config.items(): - if isinstance(value, dict) and isinstance(result.get(key), dict): - result[key] = merge_config(result.get(key), value, list_identifiers) - elif isinstance(value, list) and isinstance(result.get(key), list): - result[key] = merge_list(result[key], value, list_identifiers) - elif result.get(key) is not None and type(value) is not type(result.get(key)): + existing = result.get(key) + if isinstance(value, dict) and isinstance(existing, dict): + result[key] = merge_config(existing, value, list_identifiers) + elif isinstance(value, list) and isinstance(existing, list): + result[key] = merge_list(existing, value, list_identifiers) + elif ( + existing is not None + and (isinstance(value, (dict, list)) or isinstance(existing, (dict, list))) + and type(value) is not type(existing) + ): raise ValidationError( JsonSchemaError( - f"incompatible types for '{key}': expected {type(result.get(key)).__name__}, " + f"incompatible types for '{key}': expected {type(existing).__name__}, " f"got {type(value).__name__}" ) ) From d6bc52f7d59a9a12c2d5ad0fd38d10f31e27bb18 Mon Sep 17 00:00:00 2001 From: stktyagi Date: Sat, 14 Mar 2026 17:40:28 +0530 Subject: [PATCH 5/5] [chores] Added ci failure bot Added ci failure bot caller workflow for netjsonconfig. --- .github/workflows/bot-ci-failure.yml | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 .github/workflows/bot-ci-failure.yml diff --git a/.github/workflows/bot-ci-failure.yml b/.github/workflows/bot-ci-failure.yml new file mode 100644 index 000000000..05f500c0d --- /dev/null +++ b/.github/workflows/bot-ci-failure.yml @@ -0,0 +1,83 @@ +name: CI Failure Bot + +on: + workflow_run: + workflows: ["Netjsonconfig CI Build"] + types: + - completed + +permissions: + pull-requests: write + actions: read + contents: read + +concurrency: + group: ci-failure-${{ github.repository }}-${{ github.event.workflow_run.pull_requests[0].number || github.event.workflow_run.head_branch }} + cancel-in-progress: true + +jobs: + find-pr: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' }} + outputs: + pr_number: ${{ steps.pr.outputs.number }} + pr_author: ${{ steps.pr.outputs.author }} + steps: + - name: Find PR Number + id: pr + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + PR_NUMBER_PAYLOAD: ${{ github.event.workflow_run.pull_requests[0].number }} + EVENT_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} + run: | + emit_pr() { + local pr_number="$1" + local pr_author + pr_author=$(gh pr view "$pr_number" --repo "$REPO" --json author --jq '.author.login' 2>/dev/null || echo "") + if [ -z "$pr_author" ]; then + echo "::warning::Could not fetch PR author for PR #$pr_number" + fi + echo "number=$pr_number" >> "$GITHUB_OUTPUT" + echo "author=$pr_author" >> "$GITHUB_OUTPUT" + } + PR_NUMBER="$PR_NUMBER_PAYLOAD" + if [ -n "$PR_NUMBER" ]; then + echo "Found PR #$PR_NUMBER from workflow payload." + emit_pr "$PR_NUMBER" + exit 0 + fi + HEAD_SHA="$EVENT_HEAD_SHA" + echo "Payload empty. Searching for PR via Commits API..." + PR_NUMBER=$(gh api repos/$REPO/commits/$HEAD_SHA/pulls -q '.[0].number' 2>/dev/null || true) + if [ -n "$PR_NUMBER" ] && [ "$PR_NUMBER" != "null" ]; then + echo "Found PR #$PR_NUMBER using Commits API." + emit_pr "$PR_NUMBER" + exit 0 + fi + echo "API lookup failed/empty. Scanning open PRs for matching head SHA..." + PR_NUMBER=$(gh pr list --repo "$REPO" --state open --limit 100 --json number,headRefOid --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number" | head -n 1) + if [ -n "$PR_NUMBER" ]; then + echo "Found PR #$PR_NUMBER by scanning open PRs." + emit_pr "$PR_NUMBER" + exit 0 + fi + echo "::warning::No open PR found. This workflow run might not be attached to an open PR." + exit 0 + + call-ci-failure-bot: + needs: find-pr + if: ${{ needs.find-pr.outputs.pr_number != '' }} + uses: openwisp/openwisp-utils/.github/workflows/reusable-bot-ci-failure.yml@master + with: + pr_number: ${{ needs.find-pr.outputs.pr_number }} + head_sha: ${{ github.event.workflow_run.head_sha }} + head_repo: ${{ github.event.workflow_run.head_repository.full_name }} + base_repo: ${{ github.repository }} + run_id: ${{ github.event.workflow_run.id }} + pr_author: ${{ needs.find-pr.outputs.pr_author }} + actor: ${{ github.event.workflow_run.actor.login }} + secrets: + GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} + APP_ID: ${{ secrets.OPENWISP_BOT_APP_ID }} + PRIVATE_KEY: ${{ secrets.OPENWISP_BOT_PRIVATE_KEY }}