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 swagger API docs generation #1366

Merged
merged 6 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
45 changes: 36 additions & 9 deletions vulnerabilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

from django.db.models import Prefetch
from django_filters import rest_framework as filters
from drf_spectacular.utils import extend_schema
from packageurl import PackageURL
from rest_framework import serializers
from rest_framework import status
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
Expand Down Expand Up @@ -272,6 +274,16 @@ def filter_purl(self, queryset, name, value):
return self.queryset.filter(**lookups)


class PackageBulkSearchRequestSerializer(serializers.Serializer):
purls = serializers.ListField(
child=serializers.CharField(),
allow_empty=False,
help_text="List of PackageURL strings in canonical form.",
)
purl_only = serializers.BooleanField(required=False, default=False)
plain_purl = serializers.BooleanField(required=False, default=False)


class PackageViewSet(viewsets.ReadOnlyModelViewSet):
"""
Lookup for vulnerable packages by Package URL.
Expand All @@ -283,21 +295,36 @@ class PackageViewSet(viewsets.ReadOnlyModelViewSet):
filterset_class = PackageFilterSet
throttle_classes = [StaffUserRateThrottle, AnonRateThrottle]

# TODO: Fix the swagger documentation for this endpoint
@action(detail=False, methods=["post"])
@extend_schema(
request=PackageBulkSearchRequestSerializer,
responses={
200: PackageSerializer(many=True),
},
)
@action(
detail=False,
methods=["post"],
serializer_class=PackageBulkSearchRequestSerializer,
filter_backends=[],
pagination_class=None,
)
def bulk_search(self, request):
"""
Lookup for vulnerable packages using many Package URLs at once.
"""

purls = request.data.get("purls", []) or []
purl_only = request.data.get("purl_only", False)
plain_purl = request.data.get("plain_purl", False)
if not purls or not isinstance(purls, list):
serializer = self.serializer_class(data=request.data)
if not serializer.is_valid():
keshav-space marked this conversation as resolved.
Show resolved Hide resolved
return Response(
status=400,
data={"Error": "A non-empty 'purls' list of PURLs is required."},
status=status.HTTP_400_BAD_REQUEST,
data={
"error": serializer.errors,
"message": "A non-empty 'purls' list of PURLs is required.",
},
)
validated_data = serializer.validated_data
purls = validated_data.get("purls")
purl_only = validated_data.get("purl_only", False)
plain_purl = validated_data.get("plain_purl", False)

if plain_purl:
purl_objects = [PackageURL.from_string(purl) for purl in purls]
Expand Down
34 changes: 34 additions & 0 deletions vulnerabilities/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,40 @@ def test_bulk_api_with_purl_only_option(self):
assert len(response) == 1
assert response[0] == "pkg:nginx/[email protected]"

def test_bulk_api_without_purls_list(self):
request_body = {
"purls": None,
}
response = self.csrf_client.post(
"/api/packages/bulk_search",
data=json.dumps(request_body),
content_type="application/json",
).json()

expected = {
"error": {"purls": ["This field may not be null."]},
"message": "A non-empty 'purls' list of PURLs is required.",
}

self.assertEqual(response, expected)

def test_bulk_api_without_purls_empty_list(self):
keshav-space marked this conversation as resolved.
Show resolved Hide resolved
request_body = {
"purls": [],
}
response = self.csrf_client.post(
"/api/packages/bulk_search",
data=json.dumps(request_body),
content_type="application/json",
).json()

expected = {
"error": {"purls": ["This list may not be empty."]},
"message": "A non-empty 'purls' list of PURLs is required.",
}

self.assertEqual(response, expected)


class BulkSearchAPICPE(TestCase):
def setUp(self):
Expand Down
Loading