Skip to content

[change:radius] Replace third-party JSONField with Django built-in JSONField#619

Open
Eeshu-Yadav wants to merge 2 commits intoopenwisp:masterfrom
Eeshu-Yadav:issues/600-replace-jsonfield-clean
Open

[change:radius] Replace third-party JSONField with Django built-in JSONField#619
Eeshu-Yadav wants to merge 2 commits intoopenwisp:masterfrom
Eeshu-Yadav:issues/600-replace-jsonfield-clean

Conversation

@Eeshu-Yadav
Copy link
Contributor

Checklist

  • I have read the OpenWISP Contributing Guidelines.
  • I have manually tested the changes proposed in this pull request.
  • I have written new test cases for new code and/or updated existing tests for changes to existing code.
  • I have updated the documentation.

Reference to Existing Issue

Closes #600.

Description of Changes

This PR replaces the third-party jsonfield library with Django's built-in JSONField. The third-party JSONField library hasn't received updates in 5 years, while Django's built-in JSONField is now mature and actively maintained.

Changes Made:

  • Replaced from jsonfield import JSONField with from django.db.models import JSONField in models
  • Updated RadiusBatch.user_credentials and OrganizationRadiusSettings.sms_meta_data fields
  • Removed "jsonfield~=3.1.0" dependency from setup.py
  • Added migration 0041_replace_jsonfield_with_django_builtin.py to handle field type transition
  • Updated test migrations to use Django's JSONField

Testing:

  • All existing tests pass with the new implementation
  • Verified RadiusBatch and SMS metadata functionality
  • Migration tested without data loss

@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch 2 times, most recently from a9ad394 to 998b688 Compare August 22, 2025 22:21
@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch 2 times, most recently from a0d7e52 to 21b4925 Compare October 9, 2025 17:27
@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch 4 times, most recently from 665a8b5 to fa44638 Compare October 22, 2025 18:37
@Eeshu-Yadav
Copy link
Contributor Author

@nemesifier kindly review

@coveralls
Copy link

coveralls commented Oct 22, 2025

Coverage Status

coverage: 88.285% (-3.7%) from 91.995%
when pulling cdd7769 on Eeshu-Yadav:issues/600-replace-jsonfield-clean
into 33bbd55 on openwisp:master.

Copy link
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.

Thanks @Eeshu-Yadav, see my comments below.

@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch from fa44638 to a763c2b Compare November 18, 2025 15:50
@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch from a763c2b to ba82cd9 Compare November 30, 2025 13:28
@Eeshu-Yadav
Copy link
Contributor Author

@nemesifier kindly review

Copilot AI review requested due to automatic review settings January 30, 2026 10:49
@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch from ba82cd9 to 1c67f5d Compare January 30, 2026 10:49
@coderabbitai
Copy link

coderabbitai bot commented Jan 30, 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

Replaces third-party jsonfield.JSONField with Django's built-in models.JSONField across models, tests, and migrations; adds DjangoJSONEncoder to JSONField declarations; removes jsonfield from setup.py. Updates imports in openwisp_radius/base/models.py and changes AbstractRadiusBatch.prefix_add to assign Python objects (not JSON strings) into user_credentials. Adds schema migrations to alter JSONField metadata and a data migration that iterates RadiusBatch records, converts double-encoded JSON strings in user_credentials into proper JSON objects, and saves the normalized values.

Sequence Diagram(s)

sequenceDiagram
    participant Migration as Migration (RunPython)
    participant ORM as ORM (apps.get_model)
    participant DB as Database
    participant JSON as json.loads

    Migration->>ORM: get RadiusBatch model
    Migration->>DB: query RadiusBatch objects (exclude null user_credentials)
    DB-->>Migration: iterator of RadiusBatch rows
    loop per RadiusBatch
        Migration->>Migration: read instance.user_credentials
        alt value is string
            Migration->>JSON: json.loads(string)
            JSON-->>Migration: python_obj
            Migration->>DB: save instance with user_credentials=python_obj (update_fields)
            DB-->>Migration: acknowledge save
        else value already object
            Migration-->>Migration: skip
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: replacing third-party JSONField with Django's built-in JSONField, which matches the primary objective of the PR.
Description check ✅ Passed The PR description covers all key aspects: rationale for change, specific modifications made, testing verification, and checklist completion status (4 of 4 items addressed).
Linked Issues check ✅ Passed The PR fully addresses issue #600 objectives: replaced third-party jsonfield with Django's JSONField, ensured data migration without loss via migrations, and updated all relevant imports and dependencies.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the JSONField replacement objective: model field updates, migrations, dependency removal, and test migration updates with no extraneous modifications.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into master

✏️ 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

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


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

@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

🤖 Fix all issues with AI agents
In `@openwisp_radius/migrations/0044_convert_user_credentials_data.py`:
- Around line 8-17: The migration's DB operations must be routed to the
migration database: obtain db_alias = schema_editor.connection.alias inside
convert_user_credentials_data and use it when querying and saving RadiusBatch
(e.g., use RadiusBatch.objects.using(db_alias).exclude(...).iterator() and call
batch.save(update_fields=["user_credentials"], using=db_alias)) so reads/writes
go to the correct database during migrations.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43361fd and 1c67f5d.

📒 Files selected for processing (5)
  • openwisp_radius/base/models.py
  • openwisp_radius/migrations/0043_alter_organizationradiussettings_sms_meta_data_and_more.py
  • openwisp_radius/migrations/0044_convert_user_credentials_data.py
  • setup.py
  • tests/openwisp2/sample_radius/migrations/0002_initial_openwisp_app.py
💤 Files with no reviewable changes (1)
  • setup.py
🧰 Additional context used
🧬 Code graph analysis (1)
openwisp_radius/migrations/0043_alter_organizationradiussettings_sms_meta_data_and_more.py (1)
openwisp_radius/migrations/0044_convert_user_credentials_data.py (1)
  • Migration (20-33)
🪛 Ruff (0.14.14)
openwisp_radius/migrations/0044_convert_user_credentials_data.py

[warning] 8-8: Unused function argument: schema_editor

(ARG001)


[warning] 22-27: Mutable class attributes should be annotated with typing.ClassVar

(RUF012)


[warning] 29-33: Mutable class attributes should be annotated with typing.ClassVar

(RUF012)

openwisp_radius/migrations/0043_alter_organizationradiussettings_sms_meta_data_and_more.py

[warning] 8-10: Mutable class attributes should be annotated with typing.ClassVar

(RUF012)


[warning] 12-31: Mutable class attributes should be annotated with typing.ClassVar

(RUF012)

⏰ 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). (13)
  • GitHub Check: CodeQL analysis (python)
  • GitHub Check: Agent
  • GitHub Check: Python==3.12 | django~=5.2.0
  • GitHub Check: Python==3.13 | django~=5.2.0
  • GitHub Check: Python==3.12 | django~=5.1.0
  • GitHub Check: Python==3.10 | django~=4.2.0
  • GitHub Check: Python==3.10 | django~=5.1.0
  • GitHub Check: Python==3.11 | django~=5.1.0
  • GitHub Check: Python==3.10 | 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.11 | django~=4.2.0
  • GitHub Check: Python==3.12 | django~=4.2.0
🔇 Additional comments (4)
openwisp_radius/base/models.py (2)

20-20: Import adjustment looks fine.

Line 20 change is consistent with the rest of the file.


1017-1023: prefix_add update looks consistent.

Line 1022 assignment aligns with the surrounding logic in prefix_add.

openwisp_radius/migrations/0043_alter_organizationradiussettings_sms_meta_data_and_more.py (1)

13-30: Migration update looks good.

The field alterations are straightforward and consistent.

tests/openwisp2/sample_radius/migrations/0002_initial_openwisp_app.py (1)

544-553: Sample migration updates look good.

These updates are consistent with the rest of the migration.

Also applies to: 696-698

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Copy link

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

This pull request migrates from the unmaintained third-party jsonfield library to Django's built-in JSONField. The third-party library hasn't been updated in 5 years, while Django's JSONField is actively maintained and natively supported across all databases used by the project.

Changes:

  • Replaced third-party JSONField with Django's built-in models.JSONField in model definitions
  • Removed jsonfield~=3.1.0 dependency from setup.py
  • Added migrations to handle the field type transition and data conversion

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
setup.py Removed the deprecated jsonfield dependency
openwisp_radius/base/models.py Updated imports to use Django's JSONField and removed manual json.dumps() call that was causing double-encoding
openwisp_radius/migrations/0043_alter_organizationradiussettings_sms_meta_data_and_more.py AlterField migration to change field types from third-party JSONField to Django's JSONField
openwisp_radius/migrations/0044_convert_user_credentials_data.py Data migration to convert double-encoded JSON strings to proper Python objects
tests/openwisp2/sample_radius/migrations/0002_initial_openwisp_app.py Updated test migrations to use Django's JSONField

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

RadiusBatch = apps.get_model("openwisp_radius", "RadiusBatch")
for batch in RadiusBatch.objects.exclude(user_credentials__isnull=True).iterator():
if isinstance(batch.user_credentials, str):
batch.user_credentials = json.loads(batch.user_credentials)
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

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

The migration should add error handling for edge cases where user_credentials might be an empty string or invalid JSON. Currently, if user_credentials is an empty string, json.loads will raise a JSONDecodeError and fail the migration. Consider adding a try-except block to handle this gracefully, or add an additional check to skip empty strings before attempting to parse.

Suggested change
batch.user_credentials = json.loads(batch.user_credentials)
# Skip empty or whitespace-only strings which would fail json.loads
if not batch.user_credentials.strip():
continue
# Safely attempt to decode JSON; skip records with invalid JSON
try:
decoded_credentials = json.loads(batch.user_credentials)
except json.JSONDecodeError:
continue
batch.user_credentials = decoded_credentials

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nemesifier previously requested that this migration "fail loudly" on invalid data rather than silently skip errors, to ensure data integrity issues are caught and investigated rather than hidden. If empty strings or invalid JSON turn out to be common during testing, we can address them explicitly with proper handling.

Copy link
Member

Choose a reason for hiding this comment

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

Ok but this is not the right way of doing this, we should:

try/except and if the exception is reached: call log.exception() and print an error message with the exception message, eg:

print("Encountered error while processing {batch}: {e}")

@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch from 1c67f5d to 3f0f101 Compare January 30, 2026 11:05
coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 30, 2026
@Eeshu-Yadav
Copy link
Contributor Author

@nemesifier kindly review

@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch from 3f0f101 to 88f4e34 Compare February 4, 2026 18:25
@nemesifier nemesifier added enhancement New feature or request dependencies Pull requests that update a dependency file labels Feb 5, 2026
@github-project-automation github-project-automation bot moved this from To do (general) to In progress in OpenWISP Contributor's Board Feb 5, 2026
RadiusBatch = apps.get_model("openwisp_radius", "RadiusBatch")
for batch in RadiusBatch.objects.exclude(user_credentials__isnull=True).iterator():
if isinstance(batch.user_credentials, str):
batch.user_credentials = json.loads(batch.user_credentials)
Copy link
Member

Choose a reason for hiding this comment

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

Ok but this is not the right way of doing this, we should:

try/except and if the exception is reached: call log.exception() and print an error message with the exception message, eg:

print("Encountered error while processing {batch}: {e}")

@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch from 88f4e34 to 5621afc Compare February 13, 2026 18:17
coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 13, 2026
@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch 5 times, most recently from 7e73208 to 930abf0 Compare February 16, 2026 03:51
…#600

Replaced jsonfield package's JSONField with Django's built-in JSONField across all models to use the modern, maintained Django implementation. Updated RadiusBatch.prefix_add method to directly assign user_credentials list instead of using json.dumps(). Added migration 0043 to replace JSONField in model definitions and migration 0044 to convert existing double-encoded JSON data in user_credentials field with proper error handling. Used .iterator() in data migration for memory efficiency and removed jsonfield dependency from setup.py.

Closes openwisp#600
@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/600-replace-jsonfield-clean branch from 11359eb to b07ac97 Compare February 23, 2026 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file enhancement New feature or request

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

[change:radius] Replace thirdparty JSONField with Django built in JSONField

4 participants