Skip to content

Add weaknesses data in packages endpoint #1789

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion vulnerabilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class VulnSerializerRefsAndSummary(BaseResourceSerializer):

aliases = serializers.SerializerMethodField()

weaknesses = serializers.SerializerMethodField()

def get_aliases(self, obj):
# Assuming `obj.aliases` is a queryset of `Alias` objects
return [alias.alias for alias in obj.aliases.all()]
Expand All @@ -173,6 +175,9 @@ def get_references(self, vulnerability):

return serialized_references

def get_weaknesses(self, obj):
return [weakness.to_dict() for weakness in getattr(obj, "prefetched_weaknesses", [])]

class Meta:
model = Vulnerability
fields = [
Expand All @@ -185,6 +190,7 @@ class Meta:
"risk_score",
"exploitability",
"weighted_severity",
"weaknesses",
]


Expand Down Expand Up @@ -355,14 +361,19 @@ def get_vulnerabilities_for_a_package(self, package, fix) -> dict:
"fixed_by_packages",
queryset=fixed_packages,
to_attr="filtered_fixed_packages",
),
Prefetch(
"weaknesses",
queryset=Weakness.objects.all(),
Copy link
Member

Choose a reason for hiding this comment

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

Why all?

Copy link
Author

Choose a reason for hiding this comment

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

I used .all() to ensure a queryset is passed, as omitting it caused a server error. Should we instead be filtering the queryset?

to_attr="prefetched_weaknesses",
)
)
return VulnSerializerRefsAndSummary(
instance=qs,
many=True,
context={"request": self.context["request"]},
).data

def get_fixing_vulnerabilities(self, package) -> dict:
"""
Return a mapping of vulnerabilities fixed in the given `package`.
Expand Down