Skip to content
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

Fix some openapi warnings #897

Merged
merged 8 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.d/+fix-some-openapi-warnings.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
There should be fewer warnings/log messages when visiting the autogenerated
OpenAPI. There is one commit per change to help with future wrangling. There
are still some warnings left; getting rid of those is left as an exercise to
the reader.
6 changes: 6 additions & 0 deletions src/argus/auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ class RefreshTokenSerializer(serializers.Serializer):

class EmptySerializer(serializers.Serializer):
pass


class AuthMethodSerializer(serializers.Serializer):
type = serializers.CharField(read_only=True)
url = serializers.CharField(read_only=True)
name = serializers.CharField(read_only=True)
6 changes: 1 addition & 5 deletions src/argus/auth/urls.py
hmpf marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
from django.urls import path
from rest_framework import routers

from . import views


router = routers.SimpleRouter()


app_name = "auth"
urlpatterns = [
# path("login/", django_views.LoginView.as_view(redirect_authenticated_user=True), name="login"),
path("logout/", views.LogoutView.as_view(), name="logout"),
path("refresh-token/", views.RefreshTokenView.as_view(), name="refresh-token"),
path("user/", views.CurrentUserView.as_view(), name="current-user"),
path("users/<int:pk>/", views.BasicUserDetail.as_view(), name="user"),
] + router.urls
]
13 changes: 12 additions & 1 deletion src/argus/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
from rest_framework.views import APIView

from .models import User
from .serializers import BasicUserSerializer, EmptySerializer, RefreshTokenSerializer, UserSerializer
from .serializers import (
BasicUserSerializer,
EmptySerializer,
RefreshTokenSerializer,
UserSerializer,
AuthMethodSerializer,
)
from .utils import get_authentication_backend_name_and_type


Expand Down Expand Up @@ -62,6 +68,11 @@ class BasicUserDetail(generics.RetrieveAPIView):
queryset = User.objects.all()


@extend_schema_view(
get=extend_schema(
responses=AuthMethodSerializer,
),
)
class AuthMethodListView(APIView):
http_method_names = ["get", "head", "options", "trace"]
permission_classes = [AllowAny]
Expand Down
3 changes: 2 additions & 1 deletion src/argus/incident/V1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def create(self, validated_data: dict):


class AcknowledgementSerializerV1(serializers.ModelSerializer):
pk = serializers.IntegerField(required=False) # Type hint for OpenAPI
event = EventSerializer()

class Meta:
Expand Down Expand Up @@ -130,5 +131,5 @@ def to_representation(self, instance):


# Get rid of this!
class MetadataSerializer(serializers.Serializer):
class MetadataSerializerV1(serializers.Serializer):
sourceSystems = SourceSystemSerializer(many=True)
4 changes: 2 additions & 2 deletions src/argus/incident/V1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .serializers import (
AcknowledgementSerializerV1,
IncidentSerializerV1,
MetadataSerializer,
MetadataSerializerV1,
UpdateAcknowledgementSerializerV1,
)

Expand Down Expand Up @@ -108,7 +108,7 @@ def get_serializer_class(self):

# DEPRECATED: This view will be removed in V2
@extend_schema(
responses=MetadataSerializer,
responses=MetadataSerializerV1,
description=("Metadata used by incidents.\n\nDeprecated, use the individual endpoints instead"),
deprecated=True,
)
Expand Down
2 changes: 2 additions & 0 deletions src/argus/incident/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def to_representation(self, instance):


class RequestAcknowledgementSerializer(serializers.ModelSerializer):
pk = serializers.IntegerField(required=False) # Type hint for OpenAPI
timestamp = serializers.DateTimeField()
description = serializers.CharField(required=False, allow_blank=True)
expiration = serializers.DateTimeField(required=False, allow_null=True)
Expand Down Expand Up @@ -401,6 +402,7 @@ def validate(self, attrs: dict):


class ResponseAcknowledgementSerializer(serializers.ModelSerializer):
pk = serializers.IntegerField() # Type hint for OpenAPI
event = EventSerializer()

class Meta:
Expand Down
11 changes: 10 additions & 1 deletion src/argus/incident/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,10 @@ def perform_destroy(self, instance):

@extend_schema_view(
list=extend_schema(
operation_id="api_v2_events_list",
parameters=[
OpenApiParameter(name="cursor", description="The pagination cursor value.", type=str),
]
],
)
)
class AllEventsViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
Expand All @@ -439,6 +440,14 @@ def get_queryset(self):
return Event.objects.all()


@extend_schema_view(
list=extend_schema(
operation_id="api_v2_events_per_incident_list",
parameters=[
OpenApiParameter(name="cursor", description="The pagination cursor value.", type=str),
],
)
)
class EventViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
queryset = Incident.objects.none() # For OpenAPI
permission_classes = [IsAuthenticated]
Expand Down
6 changes: 6 additions & 0 deletions src/argus/notificationprofile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ def destroy(self, request, *args, **kwargs):


# TODO: change HTTP method to GET, and get query data from URL
@extend_schema_view(
post=extend_schema(
request=FilterBlobSerializer,
responses={200: IncidentSerializer},
),
)
class FilterPreviewView(APIView):
def post(self, request, format=None):
"""
Expand Down
18 changes: 18 additions & 0 deletions src/argus/site/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Any

from rest_framework import serializers


class MetadataSerializer(serializers.Serializer):
server_version = serializers.CharField()
api_version = serializers.DictField(child=serializers.CharField(), read_only=True)
jsonapi_schema = serializers.DictField(child=serializers.CharField(), read_only=True)
ticket_plugin = serializers.CharField(read_only=True, allow_null=True)
destination_plugins = serializers.ListField(child=serializers.CharField(), read_only=True, allow_empty=True)

def get_fields(self) -> dict[str, Any]:
fields = super().get_fields()
fields["server-version"] = fields.pop("server_version")
fields["api-version"] = fields.pop("api_version")
fields["jsonapi-schema"] = fields.pop("jsonapi_schema")
return fields
4 changes: 4 additions & 0 deletions src/argus/site/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from rest_framework.views import APIView
from rest_framework import permissions
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema, extend_schema_view

from .serializers import MetadataSerializer


ERROR_TEMPLATE = """<html>
Expand Down Expand Up @@ -81,6 +84,7 @@ def render_error_page(code, reason) -> bytes:
# fmt: on


@extend_schema_view(get=extend_schema(responses=MetadataSerializer))
class MetadataView(APIView):
http_method_names = ["get", "head", "options", "trace"]
authentication_classes = []
Expand Down
4 changes: 2 additions & 2 deletions tests/incident/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_create_golden_path(self):
"expiration": None,
}
serializer = AcknowledgementSerializerV1(data=data, context={"request": request})
serializer.is_valid()
self.assertTrue(serializer.is_valid(), serializer.errors)
validated_data = serializer.validated_data
validated_data["actor"] = self.user
validated_data["incident"] = incident
Expand Down Expand Up @@ -115,7 +115,7 @@ def test_create_golden_path(self):
"expiration": None,
}
serializer = RequestAcknowledgementSerializer(data=data, context={"request": request})
serializer.is_valid()
self.assertTrue(serializer.is_valid(), serializer.errors)
validated_data = serializer.validated_data
validated_data["actor"] = self.user
validated_data["incident"] = incident
Expand Down
Loading