Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a097c38

Browse files
committedJul 29, 2022
implement dependnecy graph for nested components
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 1ea42a2 commit a097c38

16 files changed

+234
-13
lines changed
 

‎src/helpers/tree.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*!
2+
This file is part of CycloneDX JavaScript Library.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache-2.0
17+
Copyright (c) OWASP Foundation. All Rights Reserved.
18+
*/
19+
20+
export const treeIterator = Symbol('iteratorTree')

‎src/models/component.ts

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { ExternalReferenceRepository } from './externalReference'
2828
import { LicenseRepository } from './license'
2929
import { SWID } from './swid'
3030
import { Comparable, SortableSet } from '../helpers/sortableSet'
31+
import { treeIterator } from '../helpers/tree'
3132

3233
interface OptionalProperties {
3334
bomRef?: BomRef['value']
@@ -137,4 +138,10 @@ export class Component implements Comparable {
137138
}
138139

139140
export class ComponentRepository extends SortableSet<Component> {
141+
* [treeIterator] (): Generator<Component> {
142+
for (const component of this) {
143+
yield component
144+
yield * component.components[treeIterator]()
145+
}
146+
}
140147
}

‎src/serialize/json/normalize.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as Models from '../../models'
2323
import { Protocol as Spec, Version as SpecVersion } from '../../spec'
2424
import { NormalizerOptions } from '../types'
2525
import { JsonSchema, Normalized } from './types'
26+
import { treeIterator } from '../../helpers/tree'
2627

2728
export class Factory {
2829
readonly #spec: Spec
@@ -397,9 +398,12 @@ export class DependencyGraphNormalizer extends Base {
397398
const allRefs = new Map<Models.BomRef, Models.BomRefRepository>()
398399
if (data.metadata.component !== undefined) {
399400
allRefs.set(data.metadata.component.bomRef, data.metadata.component.dependencies)
401+
for (const component of data.metadata.component.components[treeIterator]()) {
402+
allRefs.set(component.bomRef, component.dependencies)
403+
}
400404
}
401-
for (const c of data.components) {
402-
allRefs.set(c.bomRef, new Models.BomRefRepository(c.dependencies))
405+
for (const component of data.components[treeIterator]()) {
406+
allRefs.set(component.bomRef, new Models.BomRefRepository(component.dependencies))
403407
}
404408

405409
const normalized: Normalized.Dependency[] = []

‎src/serialize/xml/normalize.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as Models from '../../models'
2323
import { Protocol as Spec, Version as SpecVersion } from '../../spec'
2424
import { NormalizerOptions } from '../types'
2525
import { SimpleXml, XmlSchema } from './types'
26+
import { treeIterator } from '../../helpers/tree'
2627

2728
export class Factory {
2829
readonly #spec: Spec
@@ -517,9 +518,12 @@ export class DependencyGraphNormalizer extends Base {
517518
const allRefs = new Map<Models.BomRef, Models.BomRefRepository>()
518519
if (data.metadata.component !== undefined) {
519520
allRefs.set(data.metadata.component.bomRef, data.metadata.component.dependencies)
521+
for (const component of data.metadata.component.components[treeIterator]()) {
522+
allRefs.set(component.bomRef, new Models.BomRefRepository(component.dependencies))
523+
}
520524
}
521-
for (const c of data.components) {
522-
allRefs.set(c.bomRef, new Models.BomRefRepository(c.dependencies))
525+
for (const component of data.components[treeIterator]()) {
526+
allRefs.set(component.bomRef, new Models.BomRefRepository(component.dependencies))
523527
}
524528

525529
const normalized: Array<(SimpleXml.Element & { attributes: { ref: string } })> = []

‎tests/_data/normalizeResults/json_sortedLists_spec1.2.json

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/normalizeResults/json_sortedLists_spec1.3.json

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/normalizeResults/json_sortedLists_spec1.4.json

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/normalizeResults/xml_sortedLists_spec1.2.json

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/normalizeResults/xml_sortedLists_spec1.3.json

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/normalizeResults/xml_sortedLists_spec1.4.json

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/serializeResults/json_complex_spec1.2.json.bin

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/serializeResults/json_complex_spec1.3.json.bin

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/serializeResults/json_complex_spec1.4.json.bin

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/serializeResults/xml_complex_spec1.2.xml.bin

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/serializeResults/xml_complex_spec1.3.xml.bin

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/_data/serializeResults/xml_complex_spec1.4.xml.bin

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.