Skip to content

Commit fa3a0d5

Browse files
committed
[fix] Fix device metric API docs schema #716
Removed empty schema from DeviceMetricView that shadowed DRF auto-generated schema. Fixes #716
1 parent 4bac3ce commit fa3a0d5

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

openwisp_monitoring/device/api/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.utils.translation import gettext_lazy as _
1414
from django_filters.rest_framework import DjangoFilterBackend
1515
from pytz import UTC
16-
from rest_framework import pagination, serializers, status
16+
from rest_framework import pagination, status
1717
from rest_framework.generics import (
1818
GenericAPIView,
1919
ListAPIView,

openwisp_monitoring/device/tests/test_api.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,47 @@ def test_missing_tx_bytes(self):
14331433
self.assertEqual(points[0].get("rx_bytes"), 324)
14341434
self.assertEqual(points[0].get("tx_bytes"), 0)
14351435

1436+
def test_device_metric_view_schema_not_empty(self):
1437+
"""
1438+
Regression test for https://github.com/openwisp/openwisp-monitoring/issues/716.
1439+
Ensures DeviceMetricView exposes correctly populated schema fields via drf-yasg.
1440+
"""
1441+
try:
1442+
from drf_yasg import openapi
1443+
from drf_yasg.generators import OpenAPISchemaGenerator
1444+
except ImportError:
1445+
self.skipTest("drf-yasg is not installed")
1446+
1447+
info = openapi.Info(title="OpenWISP Monitoring API", default_version="v1")
1448+
generator = OpenAPISchemaGenerator(info=info)
1449+
schema = generator.get_schema(public=True)
1450+
schema_dict = schema.as_dict()
1451+
1452+
path_item = None
1453+
for path_name, path_obj in schema_dict.get("paths", {}).items():
1454+
if "monitoring/device/" in path_name and (
1455+
"{id}" in path_name or "{uuid}" in path_name
1456+
):
1457+
path_item = path_obj
1458+
break
1459+
1460+
self.assertIsNotNone(path_item, "Device Metric API path not found in schema")
1461+
self.assertIn("get", path_item)
1462+
1463+
resp_200 = path_item["get"]["responses"].get("200")
1464+
self.assertIsNotNone(resp_200)
1465+
self.assertIn("schema", resp_200)
1466+
1467+
schema_obj = resp_200["schema"]
1468+
if "$ref" in schema_obj:
1469+
ref_path = schema_obj["$ref"]
1470+
definition_name = ref_path.split("/")[-1]
1471+
schema_obj = schema_dict.get("definitions", {}).get(definition_name, {})
1472+
1473+
self.assertIn("properties", schema_obj)
1474+
self.assertIn("mac_address", schema_obj["properties"])
1475+
self.assertIn("management_ip", schema_obj["properties"])
1476+
14361477

14371478
class TestGeoApi(TestGeoMixin, AuthenticationMixin, DeviceMonitoringTestCase):
14381479
location_model = Location

0 commit comments

Comments
 (0)