Skip to content

Commit ea146c9

Browse files
committed
Check if weakness exists in DB or not
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 2a168d6 commit ea146c9

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

vulnerabilities/api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ class Meta:
131131
model = Weakness
132132
fields = ["cwe_id", "name", "description"]
133133

134+
def to_representation(self, instance):
135+
"""
136+
Override to include 'weakness' only if it is not None.
137+
"""
138+
representation = super().to_representation(instance)
139+
if instance.weakness is None:
140+
return None
141+
return representation
142+
134143

135144
class VulnerabilitySerializer(serializers.HyperlinkedModelSerializer):
136145
fixed_packages = MinimalPackageSerializer(
@@ -142,6 +151,15 @@ class VulnerabilitySerializer(serializers.HyperlinkedModelSerializer):
142151
aliases = AliasSerializer(many=True, source="alias")
143152
weaknesses = WeaknessSerializer(many=True)
144153

154+
def to_representation(self, instance):
155+
representation = super().to_representation(instance)
156+
157+
# Exclude None values from the weaknesses list
158+
weaknesses = representation.get("weaknesses", [])
159+
representation["weaknesses"] = [weakness for weakness in weaknesses if weakness is not None]
160+
161+
return representation
162+
145163
class Meta:
146164
model = Vulnerability
147165
fields = [

vulnerabilities/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ def description(self):
316316
"""Return the weakness's description."""
317317
return self.weakness.description if self.weakness else ""
318318

319+
def to_dict(self):
320+
return {"cwe_id": self.cwe_id, "name": self.name, "description": self.description}
321+
319322

320323
class VulnerabilityReferenceQuerySet(BaseQuerySet):
321324
def for_cpe(self):

vulnerabilities/tests/test_api.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,6 @@ def test_api_with_single_vulnerability(self):
245245
"name": "Improper Restriction of Operations within the Bounds of a Memory Buffer",
246246
"description": "The software performs operations on a memory buffer, but it can read from or write to a memory location that is outside of the intended boundary of the buffer.",
247247
},
248-
{
249-
"cwe_id": 10000,
250-
"name": "",
251-
"description": "",
252-
},
253248
],
254249
}
255250

@@ -278,11 +273,6 @@ def test_api_with_single_vulnerability_with_filters(self):
278273
"name": "Improper Restriction of Operations within the Bounds of a Memory Buffer",
279274
"description": "The software performs operations on a memory buffer, but it can read from or write to a memory location that is outside of the intended boundary of the buffer.",
280275
},
281-
{
282-
"cwe_id": 10000,
283-
"name": "",
284-
"description": "",
285-
},
286276
],
287277
}
288278

vulnerabilities/tests/test_data/suse_oval/suse-oval-CVE-2008-5679-expected.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"type": "rpm",
1111
"namespace": "opensuse",
1212
"name": "opera",
13-
"version": null,
14-
"qualifiers": null,
15-
"subpath": null
13+
"version": "",
14+
"qualifiers": "",
15+
"subpath": ""
1616
},
1717
"affected_version_range": "vers:rpm/<9.63-1.1",
1818
"fixed_version": null
@@ -29,4 +29,4 @@
2929
"weaknesses": [],
3030
"url": ""
3131
}
32-
]
32+
]

0 commit comments

Comments
 (0)