Skip to content

[fix] Fixed unused fields in device monitoring API docs#750

Open
sargunn20 wants to merge 2 commits intoopenwisp:masterfrom
sargunn20:fix/716-device-monitoring-api-schema
Open

[fix] Fixed unused fields in device monitoring API docs#750
sargunn20 wants to merge 2 commits intoopenwisp:masterfrom
sargunn20:fix/716-device-monitoring-api-schema

Conversation

@sargunn20
Copy link
Copy Markdown

Removed the 'schema' attribute from DeviceMetricView which was accidentally shadowing DRF's APIView.schema attribute used by drf-yasg for Swagger documentation generation. This caused the browsable API to show misleading 'example' and 'Model' tabs with empty content.

Also changed serializer_class from the empty serializers.Serializer to MonitoringDeviceDetailSerializer so that drf-yasg can properly auto-generate the response schema documentation.

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 #716

Description of Changes

  • Removed schema = schema from DeviceMetricView — this NetJSON validation dict was shadowing DRF's APIView.schema attribute, confusing drf-yasg
  • Changed serializer_class from empty serializers.Serializer to MonitoringDeviceDetailSerializer
  • Removed the now-unused from ..schema import schema import

Note: The data validation schema on the model (AbstractDeviceData.schema) is unaffected — only the view attribute was removed.

Removed the 'schema' attribute from DeviceMetricView which was
accidentally shadowing DRF's APIView.schema attribute used by
drf-yasg for Swagger documentation generation. This caused the
browsable API to show misleading 'example' and 'Model' tabs with
empty content.

Also changed serializer_class from the empty serializers.Serializer
to MonitoringDeviceDetailSerializer so that drf-yasg can properly
auto-generate the response schema documentation.

Closes openwisp#716
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 3, 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

DeviceMetricView in openwisp_monitoring/device/api/views.py now sets serializer_class = MonitoringDeviceDetailSerializer (replacing the previous generic serializers.Serializer) and the view-level schema attribute was removed. The import of the serializers module from that file was removed. A regression test test_device_metric_view_schema_not_empty was added in openwisp_monitoring/device/tests/test_api.py to assert the view uses the concrete serializer, that get_fields() returns real fields (including id, name, monitoring), and that DeviceMetricView.schema is not a plain dict if present.

Sequence Diagram(s)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main fix: addressing unused fields in device monitoring API documentation generation.
Description check ✅ Passed The PR description provides detailed context about the changes and follows the repository template with completed checklist items, issue reference, and comprehensive change description.
Linked Issues check ✅ Passed The PR successfully addresses issue #716 by removing the schema attribute that shadowed DRF's APIView.schema and setting a concrete serializer to enable proper API documentation generation.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the device monitoring API documentation issue: view-level schema removal, serializer change, and related test addition are all directly addressing issue #716.

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

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
openwisp_monitoring/device/api/views.py (1)

102-127: 🧹 Nitpick | 🔵 Trivial

Add a regression test for API docs output on this view.

This fix is documentation-facing; add a test that asserts /monitoring/device/{id}/ no longer exposes empty/misleading “example”/“Model” artifacts, so the shadowing issue does not regress.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openwisp_monitoring/device/api/views.py` around lines 102 - 127, Add a
regression test that requests the API schema/docs (OpenAPI/Swagger JSON) for the
DeviceMetricView endpoint and asserts the /monitoring/device/{id}/ operation
does not contain empty or misleading "example"/"Model" artifacts; locate the
endpoint by referencing the DeviceMetricView view name or URL pattern for the
device detail route and use the MonitoringDeviceDetailSerializer to confirm the
documented request/response schema matches the serializer fields (and contains
no empty example/model entries). Implement the test by generating the schema
(using DRF/OpenAPI schema generator or the project test client hitting the docs
JSON endpoint), finding the path item for /monitoring/device/{id}/, and failing
the test if any schema objects for that operation include empty "example" or
placeholder "Model" strings.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@openwisp_monitoring/device/api/views.py`:
- Around line 102-127: Add a regression test that requests the API schema/docs
(OpenAPI/Swagger JSON) for the DeviceMetricView endpoint and asserts the
/monitoring/device/{id}/ operation does not contain empty or misleading
"example"/"Model" artifacts; locate the endpoint by referencing the
DeviceMetricView view name or URL pattern for the device detail route and use
the MonitoringDeviceDetailSerializer to confirm the documented request/response
schema matches the serializer fields (and contains no empty example/model
entries). Implement the test by generating the schema (using DRF/OpenAPI schema
generator or the project test client hitting the docs JSON endpoint), finding
the path item for /monitoring/device/{id}/, and failing the test if any schema
objects for that operation include empty "example" or placeholder "Model"
strings.

ℹ️ Review info

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1e013b3 and 4bac3ce.

📒 Files selected for processing (1)
  • openwisp_monitoring/device/api/views.py
📜 Review details
⏰ 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). (9)
  • GitHub Check: Python==3.13 | django~=5.1.0
  • GitHub Check: Python==3.12 | django~=5.1.0
  • GitHub Check: Python==3.13 | 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.11 | django~=5.2.0
  • GitHub Check: Python==3.10 | django~=5.1.0
  • GitHub Check: Python==3.10 | django~=5.2.0
  • GitHub Check: Python==3.11 | django~=5.1.0
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-02-21T18:44:28.852Z
Learnt from: dee077
Repo: openwisp/openwisp-monitoring PR: 738
File: openwisp_monitoring/device/api/views.py:263-281
Timestamp: 2026-02-21T18:44:28.852Z
Learning: In openwisp-monitoring, MonitoringIndoorCoordinatesList inherits organization scoping from the parent IndoorCoordinatesList (from openwisp-controller), which uses FilterByParentManaged mixin and filters by location_id in get_queryset(). The child class only overrides the queryset attribute to add monitoring-specific select_related fields; this pattern is safe as long as get_queryset() from the parent is not bypassed. During reviews, verify that MonitoringIndoorCoordinatesList continues to rely on the parent's get_queryset() and that any added select_related fields in the child do not alter the parent's filtering logic.

Applied to files:

  • openwisp_monitoring/device/api/views.py
🧬 Code graph analysis (1)
openwisp_monitoring/device/api/views.py (1)
openwisp_monitoring/device/api/serializers.py (1)
  • MonitoringDeviceDetailSerializer (118-119)
🔇 Additional comments (1)
openwisp_monitoring/device/api/views.py (1)

126-126: Good serializer binding for schema generation.

Using MonitoringDeviceDetailSerializer here is the right fix to expose meaningful response schema metadata for this endpoint.

@atif09
Copy link
Copy Markdown
Contributor

atif09 commented Mar 3, 2026

Please run the following in order:

openwisp-qa-format

And then

./run-qa-checks

(Make sure all QA checks show SUCCESSbefore pushing your changes to the PR branch)

Also the commit message (according to this PR) should follow this structure:

[fix] Pr title #<issue_number>

Commit message body (descriptive)

Fixes #<issue_number>

Please make sure to read more here

@sargunn20 sargunn20 force-pushed the fix/716-device-monitoring-api-schema branch 2 times, most recently from 8b3d895 to fa3a0d5 Compare March 4, 2026 07:25
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 `@openwisp_monitoring/device/tests/test_api.py`:
- Around line 1453-1458: The current loop over schema_dict paths uses a loose
substring check on path_name which can select nested endpoints (e.g.,
/monitoring/device/{id}/nearby/); tighten the matching in the loop that assigns
path_item by changing the condition to only accept exact device-detail endpoints
by matching path_name against a precise pattern (for example, use a regex to
match ^/monitoring/device/(\{id\}|\{uuid\})/?$) instead of the current "in"
checks; update the condition that references path_name, path_obj and sets
path_item so only the intended detail endpoint is selected.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4fa75fcb-635b-48ba-8ee9-cc0ba17fe04b

📥 Commits

Reviewing files that changed from the base of the PR and between 8b3d895 and fa3a0d5.

📒 Files selected for processing (2)
  • openwisp_monitoring/device/api/views.py
  • openwisp_monitoring/device/tests/test_api.py
📜 Review details
⏰ 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). (8)
  • GitHub Check: Python==3.12 | django~=5.1.0
  • GitHub Check: Python==3.12 | django~=5.2.0
  • GitHub Check: Python==3.11 | django~=5.2.0
  • GitHub Check: Python==3.11 | django~=4.2.0
  • GitHub Check: Python==3.10 | django~=4.2.0
  • GitHub Check: Python==3.13 | django~=5.2.0
  • GitHub Check: Python==3.10 | django~=5.1.0
  • GitHub Check: Python==3.13 | django~=5.1.0
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2026-02-21T18:44:28.852Z
Learnt from: dee077
Repo: openwisp/openwisp-monitoring PR: 738
File: openwisp_monitoring/device/api/views.py:263-281
Timestamp: 2026-02-21T18:44:28.852Z
Learning: In openwisp-monitoring, MonitoringIndoorCoordinatesList inherits organization scoping from the parent IndoorCoordinatesList (from openwisp-controller), which uses FilterByParentManaged mixin and filters by location_id in get_queryset(). The child class only overrides the queryset attribute to add monitoring-specific select_related fields; this pattern is safe as long as get_queryset() from the parent is not bypassed. During reviews, verify that MonitoringIndoorCoordinatesList continues to rely on the parent's get_queryset() and that any added select_related fields in the child do not alter the parent's filtering logic.

Applied to files:

  • openwisp_monitoring/device/api/views.py
  • openwisp_monitoring/device/tests/test_api.py
📚 Learning: 2026-02-21T01:03:37.822Z
Learnt from: nemesifier
Repo: openwisp/openwisp-monitoring PR: 738
File: openwisp_monitoring/tests/test_selenium.py:827-859
Timestamp: 2026-02-21T01:03:37.822Z
Learning: In Selenium tests (e.g., in openwisp_monitoring/tests/test_selenium.py and similar test files), when testing JS animations on dashboards or elements driven by JavaScript (such as real-time location updates), insert a short sleep (e.g., sleep(0.3)) before WebDriverWait assertions to allow animations to complete and reduce flakiness. Note: use this as a targeted workaround and prefer explicit waits or animation-end checks where possible to avoid relying on fixed delays.

Applied to files:

  • openwisp_monitoring/device/tests/test_api.py
📚 Learning: 2026-02-25T18:42:08.825Z
Learnt from: dee077
Repo: openwisp/openwisp-monitoring PR: 738
File: openwisp_monitoring/tests/test_selenium.py:309-317
Timestamp: 2026-02-25T18:42:08.825Z
Learning: In Selenium tests that use ChannelsLiveServerTestCase (e.g., openwisp_monitoring/tests/test_selenium.py), override settings to configure CHANNEL_LAYERS with channels_redis.core.RedisChannelLayer instead of InMemoryChannelLayer. This is required because the live server runs in a separate process from the test process, and InMemoryChannelLayer is per-process only and cannot handle cross-process WebSocket broadcasting needed for real-time location update tests.

Applied to files:

  • openwisp_monitoring/device/tests/test_api.py
🔇 Additional comments (2)
openwisp_monitoring/device/api/views.py (2)

16-16: Import cleanup is correct.

Dropping the unused serializers import here is consistent with the updated view implementation.


126-126: Good fix for API schema generation.

Using MonitoringDeviceDetailSerializer on Line 126 is the right change for accurate DRF/drf-yasg response documentation.

Comment thread openwisp_monitoring/device/tests/test_api.py Outdated
@sargunn20 sargunn20 force-pushed the fix/716-device-monitoring-api-schema branch from fa3a0d5 to 2619adc Compare March 4, 2026 07:42
@sargunn20
Copy link
Copy Markdown
Author

9 out of 11 CI jobs pass. The 2 failures (Python 3.12 + Django 5.2 and Python 3.13 + Django 5.2) are a pre-existing upstream issue, the same jobs fail on other PRs as well (e.g., CI Run #2122 on a different branch)

Comment on lines +1442 to +1443
from ..api.serializers import MonitoringDeviceDetailSerializer
from ..api.views import DeviceMetricView
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you move these to the top

Comment on lines +1437 to +1441
"""
Regression test for https://github.com/openwisp/openwisp-monitoring/issues/716.
Ensures DeviceMetricView uses a proper serializer (not blank)
and does not shadow DRF's schema attribute with a plain dict.
"""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can you remove the first line and keep only bottom 2

"""
from ..api.serializers import MonitoringDeviceDetailSerializer
from ..api.views import DeviceMetricView

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the review! I've made the 3 requested changes (moved imports to the top, shortened the docstring, and removed the trailing blank line). Everything is pushed.

Removed empty schema from DeviceMetricView that shadowed DRF auto-generated schema.

Fixes openwisp#716
@sargunn20 sargunn20 force-pushed the fix/716-device-monitoring-api-schema branch from 2619adc to 09bfed4 Compare March 5, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] REST API: device monitoring API mentions unused fields "example" and "Model"

3 participants