Skip to content

Commit db897b4

Browse files
authored
Merge pull request CycloneDX#83 from CycloneDX/hierarchical-merging
2 parents 147a93e + c70e97e commit db897b4

12 files changed

+541
-22
lines changed

Diff for: CycloneDX.Core/Json/Deserializer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static Models.v1_3.Bom Deserialize_v1_3(string jsonString)
7070
{
7171
Contract.Requires(jsonString != null);
7272

73-
if (_options_v1_3 == null) _options_v1_3 = Utils.GetJsonSerializerOptions_v1_3();
73+
if (_options_v1_3 is null) _options_v1_3 = Utils.GetJsonSerializerOptions_v1_3();
7474

7575
var bom = JsonSerializer.Deserialize<Models.v1_3.Bom>(jsonString, _options_v1_3);
7676

@@ -81,7 +81,7 @@ public static Models.v1_2.Bom Deserialize_v1_2(string jsonString)
8181
{
8282
Contract.Requires(jsonString != null);
8383

84-
if (_options_v1_2 == null) _options_v1_2 = Utils.GetJsonSerializerOptions_v1_2();
84+
if (_options_v1_2 is null) _options_v1_2 = Utils.GetJsonSerializerOptions_v1_2();
8585

8686
var bom = JsonSerializer.Deserialize<Models.v1_2.Bom>(jsonString, _options_v1_2);
8787

Diff for: CycloneDX.Core/Json/Serializer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static string Serialize(Models.v1_3.Bom bom)
4949
{
5050
Contract.Requires(bom != null);
5151

52-
if (_options_v1_3 == null) _options_v1_3 = Utils.GetJsonSerializerOptions_v1_3();
52+
if (_options_v1_3 is null) _options_v1_3 = Utils.GetJsonSerializerOptions_v1_3();
5353

5454
var jsonBom = JsonSerializer.Serialize(bom, _options_v1_3);
5555

@@ -60,7 +60,7 @@ public static string Serialize(Models.v1_2.Bom bom)
6060
{
6161
Contract.Requires(bom != null);
6262

63-
if (_options_v1_2 == null) _options_v1_2 = Utils.GetJsonSerializerOptions_v1_2();
63+
if (_options_v1_2 is null) _options_v1_2 = Utils.GetJsonSerializerOptions_v1_2();
6464

6565
var jsonBom = JsonSerializer.Serialize(bom, _options_v1_2);
6666

Diff for: CycloneDX.Core/Models/v1_2/Metadata.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public DateTime? Timestamp
3030
get => _timestamp;
3131
set
3232
{
33-
if (value == null)
33+
if (value is null)
3434
{
3535
_timestamp = null;
3636
}

Diff for: CycloneDX.Core/Models/v1_3/Dependency.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class Dependency
3434

3535
[XmlElement("dependency")]
3636
[ProtoMember(2)]
37-
public List<Dependency> Dependencies { get; set; } = new List<Dependency>();
37+
public List<Dependency> Dependencies { get; set; }
3838

3939
public Dependency() {}
4040

Diff for: CycloneDX.Core/Models/v1_3/IdentifiableAction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public DateTime? Timestamp
3232
get => _timestamp;
3333
set
3434
{
35-
if (value == null)
35+
if (value is null)
3636
{
3737
_timestamp = null;
3838
}

Diff for: CycloneDX.Core/Models/v1_3/Metadata.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public DateTime? Timestamp
3333
get => _timestamp;
3434
set
3535
{
36-
if (value == null)
36+
if (value is null)
3737
{
3838
_timestamp = null;
3939
}

Diff for: CycloneDX.Utils.Tests/CycloneDX.Utils.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
11+
<PackageReference Include="Snapshooter.Xunit" Version="0.6.2" />
1112
<PackageReference Include="xunit" Version="2.4.1" />
1213
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1314
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Diff for: CycloneDX.Utils.Tests/MergeTests.cs

+117-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
using System;
1919
using System.Collections.Generic;
2020
using Xunit;
21+
using Snapshooter;
22+
using Snapshooter.Xunit;
2123
using CycloneDX;
2224
using CycloneDX.Models.v1_3;
2325
using CycloneDX.Utils;
@@ -27,7 +29,7 @@ namespace CycloneDX.Utils.Tests
2729
public class MergeTests
2830
{
2931
[Fact]
30-
public void MergeToolsTest()
32+
public void FlatMergeToolsTest()
3133
{
3234
var sbom1 = new Bom
3335
{
@@ -58,13 +60,13 @@ public void MergeToolsTest()
5860
}
5961
};
6062

61-
var result = CycloneDXUtils.Merge(sbom1, sbom2);
63+
var result = CycloneDXUtils.FlatMerge(sbom1, sbom2);
6264

63-
Assert.Equal(2, result.Metadata.Tools.Count);
65+
Snapshot.Match(result);
6466
}
6567

6668
[Fact]
67-
public void MergeComponentsTest()
69+
public void FlatMergeComponentsTest()
6870
{
6971
var sbom1 = new Bom
7072
{
@@ -89,9 +91,118 @@ public void MergeComponentsTest()
8991
}
9092
};
9193

92-
var result = CycloneDXUtils.Merge(sbom1, sbom2);
94+
var result = CycloneDXUtils.FlatMerge(sbom1, sbom2);
9395

94-
Assert.Equal(2, result.Components.Count);
96+
Snapshot.Match(result);
97+
}
98+
99+
[Fact]
100+
public void HierarchicalMergeComponentsTest()
101+
{
102+
var sbom1 = new Bom
103+
{
104+
Metadata = new Metadata
105+
{
106+
Component = new Component
107+
{
108+
Name = "System1",
109+
Version = "1",
110+
BomRef = "System1@1"
111+
}
112+
},
113+
Components = new List<Component>
114+
{
115+
new Component
116+
{
117+
Name = "Component1",
118+
Version = "1",
119+
BomRef = "Component1@1"
120+
}
121+
},
122+
Dependencies = new List<Dependency>
123+
{
124+
new Dependency
125+
{
126+
Ref = "System1@1",
127+
Dependencies = new List<Dependency>
128+
{
129+
new Dependency
130+
{
131+
Ref = "Component1@1"
132+
}
133+
}
134+
}
135+
},
136+
Compositions = new List<Composition>
137+
{
138+
new Composition
139+
{
140+
Aggregate = Composition.AggregateType.Complete,
141+
Assemblies = new List<string>
142+
{
143+
"System1@1"
144+
},
145+
Dependencies = new List<string>
146+
{
147+
"System1@1"
148+
}
149+
}
150+
}
151+
};
152+
var sbom2 = new Bom
153+
{
154+
Metadata = new Metadata
155+
{
156+
Component = new Component
157+
{
158+
Name = "System2",
159+
Version = "1",
160+
BomRef = "System2@1"
161+
}
162+
},
163+
Components = new List<Component>
164+
{
165+
new Component
166+
{
167+
Name = "Component2",
168+
Version = "1",
169+
BomRef = "Component2@1"
170+
}
171+
},
172+
Dependencies = new List<Dependency>
173+
{
174+
new Dependency
175+
{
176+
Ref = "System2@1",
177+
Dependencies = new List<Dependency>
178+
{
179+
new Dependency
180+
{
181+
Ref = "Component2@1"
182+
}
183+
}
184+
}
185+
},
186+
Compositions = new List<Composition>
187+
{
188+
new Composition
189+
{
190+
Aggregate = Composition.AggregateType.Complete,
191+
Assemblies = new List<string>
192+
{
193+
"System2@1"
194+
},
195+
Dependencies = new List<string>
196+
{
197+
"System2@1"
198+
}
199+
}
200+
}
201+
};
202+
203+
var result = CycloneDXUtils.HierarchicalMerge(new [] { sbom1, sbom2 });
204+
205+
Snapshot.Match(result);
95206
}
96207
}
97208
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"BomFormat": "CycloneDX",
3+
"SpecVersion": "1.3",
4+
"SerialNumber": null,
5+
"Version": null,
6+
"Metadata": null,
7+
"Components": [
8+
{
9+
"Type": "Null",
10+
"MimeType": null,
11+
"BomRef": null,
12+
"Supplier": null,
13+
"Author": null,
14+
"Publisher": null,
15+
"Group": null,
16+
"Name": "Component1",
17+
"Version": "1",
18+
"Description": null,
19+
"Scope": null,
20+
"Hashes": null,
21+
"Licenses": null,
22+
"Copyright": null,
23+
"Cpe": null,
24+
"Purl": null,
25+
"Swid": null,
26+
"Modified": null,
27+
"Pedigree": null,
28+
"ExternalReferences": null,
29+
"Components": null,
30+
"Properties": null,
31+
"Evidence": null
32+
},
33+
{
34+
"Type": "Null",
35+
"MimeType": null,
36+
"BomRef": null,
37+
"Supplier": null,
38+
"Author": null,
39+
"Publisher": null,
40+
"Group": null,
41+
"Name": "Component2",
42+
"Version": "1",
43+
"Description": null,
44+
"Scope": null,
45+
"Hashes": null,
46+
"Licenses": null,
47+
"Copyright": null,
48+
"Cpe": null,
49+
"Purl": null,
50+
"Swid": null,
51+
"Modified": null,
52+
"Pedigree": null,
53+
"ExternalReferences": null,
54+
"Components": null,
55+
"Properties": null,
56+
"Evidence": null
57+
}
58+
],
59+
"Services": null,
60+
"ExternalReferences": null,
61+
"Dependencies": null,
62+
"Compositions": null
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"BomFormat": "CycloneDX",
3+
"SpecVersion": "1.3",
4+
"SerialNumber": null,
5+
"Version": null,
6+
"Metadata": {
7+
"Tools": [
8+
{
9+
"Vendor": null,
10+
"Name": "Tool1",
11+
"Version": "1",
12+
"Hashes": null
13+
},
14+
{
15+
"Vendor": null,
16+
"Name": "Tool2",
17+
"Version": "1",
18+
"Hashes": null
19+
}
20+
],
21+
"Authors": null,
22+
"Component": null,
23+
"Manufacture": null,
24+
"Supplier": null,
25+
"Licenses": null,
26+
"Properties": null
27+
},
28+
"Components": [],
29+
"Services": null,
30+
"ExternalReferences": null,
31+
"Dependencies": null,
32+
"Compositions": null
33+
}

0 commit comments

Comments
 (0)