Skip to content

Commit 04b963d

Browse files
committed
Address straightforward PR comments
1 parent 11d78a5 commit 04b963d

File tree

6 files changed

+24
-25
lines changed

6 files changed

+24
-25
lines changed

src/models/vulnerability/reference.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ export class Reference implements Comparable<Reference> {
5252
}
5353
}
5454

55+
/** @beta */
5556
export class ReferenceRepository extends SortableComparables<Reference> {
5657
}

src/models/vulnerability/source.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ export class Source implements Comparable<Source> {
3636
}
3737

3838
compare (other: Source): number {
39-
function normalizeUrl (u: URL | string): string {
40-
return (typeof u === 'string') ? u : u.toString()
39+
if (this.url !== undefined && other.url !== undefined) {
40+
return (this.url.toString() ?? '').localeCompare(other.url.toString() ?? '')
4141
}
42-
const urlCompare = normalizeUrl(this.url ?? '').localeCompare(normalizeUrl(other.url ?? ''))
43-
4442
/* eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- run compares in weighted order */
45-
return (this.name ?? '').localeCompare(other.name ?? '') || urlCompare
43+
return (this.name ?? '').localeCompare(other.name ?? '')
4644
}
4745
}

src/models/vulnerability/vulnerability.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import type { Analysis } from './analysis'
2929
import type { Credits } from './credits'
3030
import { RatingRepository } from './rating'
3131
import { ReferenceRepository } from './reference'
32-
import { Source } from './source'
32+
import type { Source } from './source'
3333

3434
/** @beta */
3535
export interface OptionalVulnerabilityProperties {
@@ -58,7 +58,7 @@ export class Vulnerability implements Comparable<Vulnerability> {
5858
/** @see bomRef */
5959
readonly #bomRef: BomRef
6060
id?: string
61-
source: Source
61+
source?: Source
6262
references: ReferenceRepository
6363
ratings: RatingRepository
6464
cwes: CweRepository
@@ -78,7 +78,7 @@ export class Vulnerability implements Comparable<Vulnerability> {
7878
constructor (op: OptionalVulnerabilityProperties = {}) {
7979
this.#bomRef = new BomRef(op.bomRef)
8080
this.id = op.id
81-
this.source = op.source ?? new Source()
81+
this.source = op.source
8282
this.references = op.references ?? new ReferenceRepository()
8383
this.ratings = op.ratings ?? new RatingRepository()
8484
this.cwes = op.cwes ?? new CweRepository()
@@ -105,9 +105,12 @@ export class Vulnerability implements Comparable<Vulnerability> {
105105
if (bomRefCompare !== 0) {
106106
return bomRefCompare
107107
}
108+
if (this.source !== undefined && other.source !== undefined) {
109+
return this.source.compare(other.source)
110+
}
111+
108112
/* eslint-disable @typescript-eslint/strict-boolean-expressions -- run compares in weighted order */
109113
return (this.id ?? '').localeCompare(other.id ?? '') ||
110-
this.source.compare(other.source) ||
111114
(this.description ?? '').localeCompare(other.description ?? '') ||
112115
(this.detail ?? '').localeCompare(other.detail ?? '')
113116
/* eslint-enable @typescript-eslint/strict-boolean-expressions */

src/serialize/json/normalize.ts

+10-13
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,10 @@ export class DependencyGraphNormalizer extends BaseJsonNormalizer<Models.Bom> {
527527
}
528528

529529
export class VulnerabilityNormalizer extends BaseJsonNormalizer<Models.Vulnerability.Vulnerability> {
530-
normalize (data: Models.Vulnerability.Vulnerability, options: NormalizerOptions): Normalized.Vulnerability | undefined {
531-
const source = data.source !== undefined
532-
? this._factory.makeForVulnerabilitySource().normalize(data.source, options)
533-
: undefined
530+
normalize (data: Models.Vulnerability.Vulnerability, options: NormalizerOptions): Normalized.Vulnerability {
531+
const source = data.source === undefined
532+
? undefined
533+
: this._factory.makeForVulnerabilitySource().normalize(data.source, options)
534534
const references = data.references.size > 0
535535
? this._factory.makeForVulnerabilityReference().normalizeIterable(data.references, options)
536536
: undefined
@@ -555,18 +555,15 @@ export class VulnerabilityNormalizer extends BaseJsonNormalizer<Models.Vulnerabi
555555
: Array.from(data)
556556
).map(
557557
c => this.normalize(c, options)
558-
).filter(isNotUndefined)
558+
)
559559
}
560560
}
561561

562562
export class VulnerabilitySourceNormalizer extends BaseJsonNormalizer<Models.Vulnerability.Source> {
563563
normalize (data: Models.Vulnerability.Source, options: NormalizerOptions): Normalized.VulnerabilitySource {
564-
const url = data.url !== undefined && typeof data.url !== 'string'
565-
? data.url.toString()
566-
: data.url
567564
return {
568565
name: data.name,
569-
url
566+
url: data.url?.toString()
570567
}
571568
}
572569
}
@@ -575,9 +572,9 @@ export class VulnerabilityReferenceNormalizer extends BaseJsonNormalizer<Models.
575572
normalize (data: Models.Vulnerability.Reference, options: NormalizerOptions): Normalized.VulnerabilityReference {
576573
return {
577574
id: data.id,
578-
source: data.source !== undefined
579-
? this._factory.makeForVulnerabilitySource().normalize(data.source, options)
580-
: undefined
575+
source: data.source === undefined
576+
? undefined
577+
: this._factory.makeForVulnerabilitySource().normalize(data.source, options)
581578
}
582579
}
583580

@@ -588,7 +585,7 @@ export class VulnerabilityReferenceNormalizer extends BaseJsonNormalizer<Models.
588585
: Array.from(data)
589586
).map(
590587
c => this.normalize(c, options)
591-
).filter(isNotUndefined)
588+
)
592589
}
593590
}
594591

tests/_data/models.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ module.exports.createComplexStructure = function () {
201201
id: '1',
202202
source: new Models.Vulnerability.Source({ name: 'manual' }),
203203
references: new Models.Vulnerability.ReferenceRepository([
204-
new Models.Vulnerability.Reference('CVE-2042-42420'),
205-
new Models.Vulnerability.Reference('CVE-2042-42421')
204+
new Models.Vulnerability.Reference('CVE-2042-42421'),
205+
new Models.Vulnerability.Reference('CVE-2042-42420')
206206
]),
207207
description: 'description of 1',
208208
detail: 'detail of 1',

tests/unit/Models.Vulnerability.Vulnerability.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ suite('Models.Vulnerability.Vulnerability', () => {
5858

5959
assert.strictEqual(vulnerability.bomRef.value, undefined)
6060
assert.strictEqual(vulnerability.id, undefined)
61-
assert.ok(vulnerability.source instanceof Source)
61+
assert.strictEqual(vulnerability.source, undefined)
6262
assert.ok(vulnerability.references instanceof ReferenceRepository)
6363
assert.strictEqual(vulnerability.references.size, 0)
6464
assert.ok(vulnerability.ratings instanceof RatingRepository)

0 commit comments

Comments
 (0)