[fix] Merging with wrong format now raises Validation error #351#355
[fix] Merging with wrong format now raises Validation error #351#355stktyagi wants to merge 7 commits intoopenwisp:masterfrom
Conversation
An attempt to merge config with wrong format will now raise a validation error instead of failing. Fixes openwisp#351
It only prevents the original AttributeError crash and correctly lets the .validate() method handle all validation of the final merged configuration. Fixes openwisp#351
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📜 Recent review details🧰 Additional context used🧠 Learnings (3)📓 Common learnings📚 Learning: 2026-03-13T05:53:38.499ZApplied to files:
📚 Learning: 2026-03-13T05:53:38.499ZApplied to files:
🔇 Additional comments (3)
📝 WalkthroughWalkthroughmerge_config in netjsonconfig/utils.py was changed to only recurse when both existing and new values are dicts, using the existing value as the merge target. When both values are lists, merging is delegated to merge_list and list_identifiers is propagated. If the existing and new values are incompatible container types (dict vs list) or different primitive types, merge_config now raises a local ValidationError wrapping a JsonSchemaError with a descriptive message. The docstring was updated to document the raised ValidationError. tests/openwrt/test_default.py adds test_merge_invalid_format asserting a ValidationError for mixed-format templates. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
nemesifier
left a comment
There was a problem hiding this comment.
@stktyagi the description says:
An attempt to merge config with wrong format will now raise a validation error instead of failing.
However, I don't see this in this implementation, nor in the test. No validation error is raised, it looks as if the error is silently ignored, which is not what I was expecting.
Why this inconsistency? Is this intentional?
There was a problem hiding this comment.
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 `@tests/openwrt/test_default.py`:
- Around line 249-272: The test test_merge_invalid_format currently calls
o.validate() without asserting failure; update it to expect a ValidationError by
wrapping the call in a pytest.raises(ValidationError) context (or using the test
framework's equivalent) and ensure ValidationError is imported from the module
that defines it; reference the OpenWrt class and its validate() call (i.e., wrap
OpenWrt(...).validate() in the raises block) so the test will fail if no
ValidationError is raised.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 4f361efe-c095-4ed5-88cc-86211f95ba8a
📒 Files selected for processing (1)
tests/openwrt/test_default.py
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 openwisp#351
@nemesifier, I must have missed this while updating all my PRs in feb. These changes don't make sense. I apologise for the oversight, I've added changes as per requirements. |
There was a problem hiding this comment.
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/utils.py`:
- Around line 30-40: The current check in merge_config rejects any differing
scalar types; change it so scalar overrides are allowed and only container-shape
conflicts raise. In the block handling key/value merges (referencing
merge_config, result, key, value and merge_list), replace the broad
type-mismatch check with logic that only raises a ValidationError when one side
is a container (dict or list) and the other is not the same container type
(e.g., dict vs list or dict vs scalar), allowing differing primitive scalars
(int vs str vs bool) to overwrite the template.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 6f65b1a5-55aa-4fc8-b948-073e80837fd2
📒 Files selected for processing (2)
netjsonconfig/utils.pytests/openwrt/test_default.py
📜 Review details
🔇 Additional comments (1)
tests/openwrt/test_default.py (1)
250-273: Good regression coverage.This reproduces the dict-vs-list
dhcpclash from the bug report and now asserts the expectedValidationErrorat merge time.
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 openwisp#351
An attempt to merge config with wrong format will now raise a validation error instead of failing.
Fixes #351
Checklist
Reference to Existing Issue
Closes #351
Description of Changes
Added conditionals to check if two template types can be merged and if not raise validation error instead of failing.