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

Add documentation of Bulk Search API #400

Closed
Closed
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
16 changes: 9 additions & 7 deletions vulnerabilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@ class PackageViewSet(viewsets.ReadOnlyModelViewSet):
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = PackageFilterSet

# TODO: Fix the swagger documentation for this endpoint
@extend_schema(request=placeholder_serializer, responses=placeholder_serializer)
class PackageBulkRequestSerializer(serializers.Serializer):
purls = serializers.ListField(child=serializers.CharField(max_length=100))

class PackageBulkResponseSerializer(serializers.Serializer):
result = serializers.ListField(child=PackageSerializer())

@extend_schema(request=PackageBulkRequestSerializer, responses=PackageBulkResponseSerializer)
@action(detail=False, methods=["post"])
def bulk_search(self, request):
"""
See https://github.com/nexB/vulnerablecode/pull/369#issuecomment-796877606 for docs
"""
response = []
purls = request.data.get("purls", []) or []
if not purls or not isinstance(purls, list):
Expand All @@ -168,8 +170,8 @@ def bulk_search(self, request):
purl_response["resolved_vulnerabilities"] = []
purl_response["purl"] = purl_string
response.append(purl_response)

return Response(response)
res = {"result": response}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is still nested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is still nesting the results.

As mentioned here https://drf-spectacular.readthedocs.io/en/latest/drf_spectacular.html#drf_spectacular.utils.extend_schema
to listing object we can use Serializer(many=True)

@extend_schema(request=PackageBulkRequestSerializer, responses=PackageSerializer(many=True))

When I am trying to return a list of Packages then it automatically convert it into paginator view.

Output :

image

And I also tried to remove pagination from setting.py file and add a custom paginator in other API's but then it adds unwanted parameters in bulk_api as it fetches directly from the Package Model.

Preview :

image

Still trying some other options..

return Response(res)


class VulnerabilityFilterSet(filters.FilterSet):
Expand Down
103 changes: 52 additions & 51 deletions vulnerabilities/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def cleaned_response(response):
- sort lists with a stable order
"""
cleaned_response = []
response_copy = sorted(response, key=lambda x: x.get("purl", ""))
response_copy = sorted(response["result"], key=lambda x: x.get("purl", ""))
for package_data in response_copy:
package_data["unresolved_vulnerabilities"] = sorted(
package_data["unresolved_vulnerabilities"], key=lambda x: x["vulnerability_id"]
Expand Down Expand Up @@ -256,56 +256,57 @@ def test_bulk_packages_api(self):
content_type="application/json",
).json()

expected_response = [
{
"purl": "pkg:deb/debian/[email protected]?distro=jessie",
"name": "doesnotexist",
"namespace": "debian",
"qualifiers": {"distro": "jessie"},
"resolved_vulnerabilities": [],
"subpath": None,
"type": "deb",
"unresolved_vulnerabilities": [],
"version": "0.9.7-10",
},
{
"name": "datadog-api-client",
"namespace": "com.datadoghq",
"purl": "pkg:maven/com.datadoghq/[email protected]",
"qualifiers": {},
"resolved_vulnerabilities": [],
"subpath": "",
"type": "maven",
"unresolved_vulnerabilities": [
{
"references": [
{
"reference_id": "",
"scores": [],
"source": "",
"url": "https://nvd.nist.gov/vuln/detail/CVE-2021-21331",
},
{
"reference_id": "GHSA-2cxf-6567-7pp6",
"scores": [{"scoring_system": "cvssv3.1_qr", "value": "LOW"}],
"source": "",
"url": "https://github.com/DataDog/datadog-api-client-java/security/advisories/GHSA-2cxf-6567-7pp6",
},
{
"reference_id": "GHSA-2cxf-6567-7pp6",
"scores": [],
"source": "",
"url": "https://github.com/advisories/GHSA-2cxf-6567-7pp6",
},
],
"url": "http://testserver/api/vulnerabilities/60",
"vulnerability_id": "CVE-2021-21331",
}
],
"url": "http://testserver/api/packages/3467",
"version": "1.0.0-beta.7",
},
]
expected_response = {
"result": [
{
"name": "doesnotexist",
"namespace": "debian",
"qualifiers": {"distro": "jessie"},
"resolved_vulnerabilities": [],
"subpath": None,
"type": "deb",
"unresolved_vulnerabilities": [],
"version": "0.9.7-10",
},
{
"name": "datadog-api-client",
"namespace": "com.datadoghq",
"purl": "pkg:maven/com.datadoghq/[email protected]",
"qualifiers": {},
"resolved_vulnerabilities": [],
"subpath": "",
"type": "maven",
"unresolved_vulnerabilities": [
{
"references": [
{
"reference_id": "",
"scores": [],
"source": "",
"url": "https://nvd.nist.gov/vuln/detail/CVE-2021-21331",
},
{
"reference_id": "GHSA-2cxf-6567-7pp6",
"scores": [{"scoring_system": "cvssv3.1_qr", "value": "LOW"}],
"source": "",
"url": "https://github.com/DataDog/datadog-api-client-java/security/advisories/GHSA-2cxf-6567-7pp6",
},
{
"reference_id": "GHSA-2cxf-6567-7pp6",
"scores": [],
"source": "",
"url": "https://github.com/advisories/GHSA-2cxf-6567-7pp6",
},
],
"url": "http://testserver/api/vulnerabilities/60",
"vulnerability_id": "CVE-2021-21331",
}
],
"url": "http://testserver/api/packages/3467",
"version": "1.0.0-beta.7",
},
]
}
assert cleaned_response(expected_response) == cleaned_response(response)

def test_invalid_request_bulk_packages(self):
Expand Down