-
-
Notifications
You must be signed in to change notification settings - Fork 76
[Fix] Fixed triggering of uncaught exception on submitting empty imag… #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
WalkthroughRewrites DeviceFirmwareForm.full_clean() to avoid accessing missing keys in self.cleaned_data when the form is unbound or has field errors. The new implementation checks binding and errors, ensures the image field is present (adding a field error if missing), short-circuits when device is absent, constructs an UpgradeOperation and calls its full_clean(), and reports any ValidationError via add_error(None, error). Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
openwisp_firmware_upgrader/tests/test_admin.py (1)
512-531: Consider using a more explicit assertion for checking DeviceFirmware creation.The test logic is sound and correctly validates the error handling behavior. However, line 530 uses
hasattr(device, "devicefirmware")which may not reliably detect whether a DeviceFirmware instance was created, especially if the relationship is a OneToOneField (wherehasattrreturnsTrueeven when the related object doesn't exist, but accessing it raises an exception).💡 More explicit assertion
- self.assertFalse(hasattr(device, "devicefirmware")) + self.assertFalse(DeviceFirmware.objects.filter(device=device).exists())This approach directly queries the database and is unambiguous regardless of the relationship type.
openwisp_firmware_upgrader/admin.py (1)
356-379: Fix correctly prevents KeyError, but exception handling can be simplified.The rewritten validation logic properly addresses the issue described in #338 by:
- Checking if the form is bound and has no errors before proceeding
- Explicitly validating that
imageexists incleaned_databefore use- Gracefully handling missing
deviceto prevent KeyError- Properly propagating
UpgradeOperationvalidation errors to the formThis ensures validation errors are displayed in the admin UI instead of causing an uncaught exception.
🔧 Minor simplification: remove redundant exception type
Line 378 catches both
ValidationErrorandforms.ValidationError, but these are the same exception class.django.forms.ValidationErroris just a re-export ofdjango.core.exceptions.ValidationError, so catching both is redundant:try: upgrade_op.full_clean() - except (ValidationError, forms.ValidationError) as error: + except ValidationError as error: self.add_error(None, error)This simplification improves code clarity without changing behavior.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
openwisp_firmware_upgrader/admin.pyopenwisp_firmware_upgrader/tests/test_admin.py
🧰 Additional context used
🧬 Code graph analysis (2)
openwisp_firmware_upgrader/tests/test_admin.py (2)
openwisp_firmware_upgrader/tests/test_api.py (1)
_login(56-58)openwisp_firmware_upgrader/tests/base.py (2)
_create_device_with_connection(199-203)_create_firmware_image(76-91)
openwisp_firmware_upgrader/admin.py (3)
openwisp_firmware_upgrader/api/views.py (1)
get(94-105)openwisp_firmware_upgrader/models.py (1)
UpgradeOperation(43-46)tests/openwisp2/sample_firmware_upgrader/models.py (1)
UpgradeOperation(45-47)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
- GitHub Check: Python==3.10 | django~=5.2.0
- GitHub Check: Python==3.13 | django~=5.2.0
- GitHub Check: Python==3.11 | django~=5.1.0
- GitHub Check: Python==3.12 | django~=5.2.0
- GitHub Check: Python==3.13 | django~=5.1.0
- GitHub Check: Python==3.11 | django~=5.2.0
- GitHub Check: Python==3.10 | django~=4.2.0
- GitHub Check: Python==3.11 | django~=4.2.0
- GitHub Check: Python==3.12 | django~=4.2.0
- GitHub Check: Python==3.13 | django~=4.2.0
- GitHub Check: Python==3.12 | django~=5.1.0
- GitHub Check: Python==3.10 | django~=5.1.0
🔇 Additional comments (1)
openwisp_firmware_upgrader/admin.py (1)
11-11: LGTM: ValidationError import added for explicit error handling.The import supports the improved validation logic in
DeviceFirmwareForm.full_clean().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow up with @coderabbitai review
Simplified exception handling and implemented a more explicit assertion for checking DeviceFirmware creation. Fixes #338
…e #338
Ensures that DeviceFirmwareForm.full_clean does not crash when 'device' or 'image' keys are missing from cleaned_data. Fixes #338
Checklist
Reference to Existing Issue
Closes #338.
Description of Changes
Explicitly added a ValidationError if the image is missing to prevent silent save failures with a test to verify the implementation.
Screenshot