Skip to content

Commit cb45054

Browse files
pragnya17sfoslundppandrate
authored
Update metadata contract to be backcompatible with SPDX 2.2 parser (#918)
* Remove unnecessary parser errors which disallow syft SBOMs * Add Spdx22Metadata back in to avoid major version bump * update unit tests to reflect metadata class change * revert to using spdx22metadata --------- Co-authored-by: Sarah Oslund <sfoslund@microsoft.com> Co-authored-by: ppandrate <ppandrate@microsoft.com>
1 parent b943a0f commit cb45054

File tree

7 files changed

+20
-7
lines changed

7 files changed

+20
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.Sbom.Contracts;
5+
6+
public class Spdx22Metadata : SpdxMetadata { }

src/Microsoft.Sbom.Extensions/ISbomParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface ISbomParser
2727
/// </summary>
2828
/// <param name="stream"></param>
2929
/// <returns></returns>
30-
SpdxMetadata GetMetadata();
30+
Spdx22Metadata GetMetadata();
3131

3232
/// <summary>
3333
/// This function is called by the sbom tool upon initialization to get all the

src/Microsoft.Sbom.Parsers.Spdx22SbomParser/Parser/SPDXParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ internal SPDXParser(
172172
return null;
173173
}
174174

175-
public SpdxMetadata GetMetadata()
175+
public Spdx22Metadata GetMetadata()
176176
{
177177
if (!this.parsingComplete)
178178
{
179179
throw new ParserException($"{nameof(this.GetMetadata)} can only be called after Parsing is complete to ensure that a whole object is returned.");
180180
}
181181

182-
var spdxMetadata = new SpdxMetadata();
182+
var spdxMetadata = new Spdx22Metadata();
183183
foreach (var kvp in this.metadata)
184184
{
185185
switch (kvp.Key)

src/Microsoft.Sbom.Parsers.Spdx30SbomParser/Parser/SPDX30Parser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SPDX30Parser : ISbomParser
3535

3636
public ComplianceStandard? RequiredComplianceStandard;
3737
public IReadOnlyCollection<string>? EntitiesToEnforceComplianceStandardsFor;
38-
public SpdxMetadata Metadata = new SpdxMetadata();
38+
public Spdx22Metadata Metadata = new Spdx22Metadata();
3939
private readonly LargeJsonParser parser;
4040
private readonly IList<string> observedFieldNames = new List<string>();
4141
private readonly bool requiredFieldsCheck = true;
@@ -157,13 +157,14 @@ public SPDX30Parser(
157157
return null;
158158
}
159159

160-
public SpdxMetadata GetMetadata()
160+
public Spdx22Metadata GetMetadata()
161161
{
162162
if (!this.parsingComplete)
163163
{
164164
throw new ParserException($"{nameof(this.GetMetadata)} can only be called after Parsing is complete to ensure that a whole object is returned.");
165165
}
166166

167+
// TODO: Eventually this return type should be changed to SpdxMetadata to be consistent with naming.
167168
return this.Metadata;
168169
}
169170

test/Microsoft.Sbom.Parsers.Spdx22SbomParser.Tests/Parser/SbomFileParserTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Text;
8+
using Microsoft.Sbom.Contracts;
89
using Microsoft.Sbom.JsonAsynchronousNodeKit.Exceptions;
910
using Microsoft.Sbom.Parser.Strings;
1011
using Microsoft.Sbom.Parsers.Spdx22SbomParser;
@@ -41,6 +42,7 @@ public void MetadataPopulates()
4142
var metadata = parser.GetMetadata();
4243

4344
Assert.IsNotNull(metadata);
45+
Assert.IsInstanceOfType(metadata, typeof(Spdx22Metadata));
4446
Assert.IsNotNull(metadata.CreationInfo);
4547
var expectedTime = DateTime.Parse("2023-05-11T00:24:54Z").ToUniversalTime();
4648
Assert.AreEqual(expectedTime, metadata.CreationInfo.Created);

test/Microsoft.Sbom.Parsers.Spdx30SbomParser.Tests/Parser/SbomMetadataParserTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Text;
8+
using Microsoft.Sbom.Contracts;
89
using Microsoft.Sbom.JsonAsynchronousNodeKit.Exceptions;
910
using Microsoft.Sbom.Parser.JsonStrings;
1011
using Microsoft.Sbom.Parsers.Spdx30SbomParser;
@@ -27,6 +28,7 @@ public void MetadataPopulates()
2728
Assert.AreEqual(5, results.FormatEnforcedSPDX3Result.Graph.Count());
2829

2930
var metadata = parser.GetMetadata();
31+
Assert.IsInstanceOfType(metadata, typeof(Spdx22Metadata));
3032
Assert.IsNotNull(metadata);
3133
Assert.IsNotNull(metadata.DocumentNamespace);
3234
Assert.AreEqual("spdx-doc-name", metadata.Name);
@@ -49,6 +51,7 @@ public void MetadataEmpty()
4951
var results = this.Parse(parser);
5052

5153
var metadata = parser.GetMetadata();
54+
Assert.IsInstanceOfType(metadata, typeof(Spdx22Metadata));
5255
Assert.IsNotNull(metadata);
5356
Assert.IsNull(metadata.DocumentNamespace);
5457
Assert.IsNull(metadata.Name);

test/Microsoft.Sbom.Parsers.Spdx30SbomParser.Tests/Parser/SbomParserTestsBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,10 @@ public void ValidateSbomPackagesForNTIA(List<Element> elementsList)
243243
/// Sets metadata based on parsed SBOM elements.
244244
/// </summary>
245245
/// <param name="result"></param>
246-
public SpdxMetadata SetMetadata(ParserResults result)
246+
public Spdx22Metadata SetMetadata(ParserResults result)
247247
{
248-
var metadata = new SpdxMetadata();
248+
// TODO: Eventually this return type should be changed to SpdxMetadata to be consistent with naming.
249+
var metadata = new Spdx22Metadata();
249250
var spdxDocumentElement = (SpdxDocument?)result.FormatEnforcedSPDX3Result.Graph.FirstOrDefault(element => element.Type == "SpdxDocument");
250251

251252
if (spdxDocumentElement == null)

0 commit comments

Comments
 (0)