Skip to content

Commit cfb8707

Browse files
committed
[fix] Add dashboard API filters to browsable API docs #715
Added @swagger_auto_schema to DashboardTimeseriesView.get() to expose query parameters (organization_slug, location_id, floorplan_id, time, start, end, timezone, csv) in the Swagger/browsable API documentation. Since DashboardTimeseriesView extends APIView and manually reads query parameters from request.query_params, drf-yasg had no way to discover them. The @swagger_auto_schema decorator explicitly declares these parameters so they appear in the API docs. Also added a regression test to verify the parameters are properly declared. Closes #715
1 parent 1e013b3 commit cfb8707

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

openwisp_monitoring/monitoring/api/views.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from cache_memoize import cache_memoize
22
from django.db.models import Q
3+
from drf_yasg import openapi
4+
from drf_yasg.utils import swagger_auto_schema
35
from rest_framework.exceptions import NotFound, PermissionDenied
46
from rest_framework.permissions import IsAuthenticated
57
from rest_framework.views import APIView
@@ -181,6 +183,64 @@ def _get_user_managed_orgs(self, request):
181183
return []
182184
return orgs
183185

186+
@swagger_auto_schema(
187+
manual_parameters=[
188+
openapi.Parameter(
189+
"organization_slug",
190+
openapi.IN_QUERY,
191+
description="Comma-separated list of organization slugs",
192+
type=openapi.TYPE_STRING,
193+
),
194+
openapi.Parameter(
195+
"location_id",
196+
openapi.IN_QUERY,
197+
description="Comma-separated list of location UUIDs",
198+
type=openapi.TYPE_STRING,
199+
),
200+
openapi.Parameter(
201+
"floorplan_id",
202+
openapi.IN_QUERY,
203+
description="Comma-separated list of floorplan UUIDs",
204+
type=openapi.TYPE_STRING,
205+
),
206+
openapi.Parameter(
207+
"time",
208+
openapi.IN_QUERY,
209+
description=(
210+
"Timeframe for chart data " "(e.g. 1d, 3d, 7d, 30d, 365d)"
211+
),
212+
type=openapi.TYPE_STRING,
213+
),
214+
openapi.Parameter(
215+
"start",
216+
openapi.IN_QUERY,
217+
description=(
218+
"Start date/time in YYYY-MM-DD H:M:S format " '(use with "end")'
219+
),
220+
type=openapi.TYPE_STRING,
221+
),
222+
openapi.Parameter(
223+
"end",
224+
openapi.IN_QUERY,
225+
description=(
226+
"End date/time in YYYY-MM-DD H:M:S format " '(use with "start")'
227+
),
228+
type=openapi.TYPE_STRING,
229+
),
230+
openapi.Parameter(
231+
"timezone",
232+
openapi.IN_QUERY,
233+
description="Timezone name (e.g. UTC, Europe/Rome)",
234+
type=openapi.TYPE_STRING,
235+
),
236+
openapi.Parameter(
237+
"csv",
238+
openapi.IN_QUERY,
239+
description="Set to export data as CSV",
240+
type=openapi.TYPE_STRING,
241+
),
242+
]
243+
)
184244
def get(self, request, *args, **kwargs):
185245
response = super().get(request, *args, **kwargs)
186246
if not request.GET.get("csv"):

openwisp_monitoring/monitoring/tests/test_api.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,3 +724,37 @@ def test_organizations_list(self):
724724
response.data["organizations"],
725725
[{"id": org.slug, "text": org.name} for org in [org1, org2]],
726726
)
727+
728+
def test_dashboard_api_filters_in_schema(self):
729+
"""Regression test for https://github.com/openwisp/openwisp-monitoring/issues/715
730+
731+
Verifies that DashboardTimeseriesView exposes the expected
732+
query parameters via @swagger_auto_schema so they are
733+
visible in the browsable API docs.
734+
"""
735+
from openwisp_monitoring.monitoring.api.views import DashboardTimeseriesView
736+
737+
swagger_decorator = getattr(
738+
DashboardTimeseriesView.get, "_swagger_auto_schema", None
739+
)
740+
self.assertIsNotNone(
741+
swagger_decorator,
742+
"DashboardTimeseriesView.get is missing @swagger_auto_schema",
743+
)
744+
manual_parameters = swagger_decorator.get("manual_parameters", [])
745+
param_names = {p.name for p in manual_parameters}
746+
expected_params = {
747+
"organization_slug",
748+
"location_id",
749+
"floorplan_id",
750+
"time",
751+
"start",
752+
"end",
753+
"timezone",
754+
"csv",
755+
}
756+
self.assertEqual(
757+
param_names,
758+
expected_params,
759+
f"Missing or extra swagger parameters: {expected_params - param_names}",
760+
)

0 commit comments

Comments
 (0)