From 93fc4a2e0fb656ca6c7cc37916ac21332759d5d6 Mon Sep 17 00:00:00 2001 From: andreas-hilti <69210561+andreas-hilti@users.noreply.github.com> Date: Sun, 19 May 2024 22:28:06 +0200 Subject: [PATCH] fix: serialization of enums VolumeMode and DataType (#290) Fix serialization of enums VolumeMode and DataType Add test to validate SBOM after round trip --------- Signed-off-by: andreas hilti Co-authored-by: MTsfoni Signed-off-by: MTsfoni --- src/CycloneDX.Core/Json/Utils.cs | 2 + .../Json/v1.5/ValidationTests.cs | 50 +++++++++++++++++++ ...pAsyncTest_valid-formulation-1.5.json.snap | 2 +- ...ndTripTest_valid-formulation-1.5.json.snap | 2 +- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/CycloneDX.Core/Json/Utils.cs b/src/CycloneDX.Core/Json/Utils.cs index 9390cf34..7091024c 100644 --- a/src/CycloneDX.Core/Json/Utils.cs +++ b/src/CycloneDX.Core/Json/Utils.cs @@ -77,6 +77,8 @@ public static JsonSerializerOptions GetJsonSerializerOptions() options.Converters.Add(new HyphenEnumConverter()); options.Converters.Add(new HyphenEnumConverter()); options.Converters.Add(new UnderscoreEnumConverter()); + options.Converters.Add(new HyphenEnumConverter()); + options.Converters.Add(new HyphenEnumConverter()); options.Converters.Add(new JsonStringEnumConverter()); diff --git a/tests/CycloneDX.Core.Tests/Json/v1.5/ValidationTests.cs b/tests/CycloneDX.Core.Tests/Json/v1.5/ValidationTests.cs index 9f35e63f..0310f610 100644 --- a/tests/CycloneDX.Core.Tests/Json/v1.5/ValidationTests.cs +++ b/tests/CycloneDX.Core.Tests/Json/v1.5/ValidationTests.cs @@ -153,5 +153,55 @@ public void InvalidJsonTest(string filename) Assert.False(validationResult.Valid); } + + [Theory] + [InlineData("valid-annotation-1.5.json")] + [InlineData("valid-assembly-1.5.json")] + [InlineData("valid-bom-1.5.json")] + [InlineData("valid-component-hashes-1.5.json")] + [InlineData("valid-component-ref-1.5.json")] + [InlineData("valid-component-swid-1.5.json")] + [InlineData("valid-component-swid-full-1.5.json")] + [InlineData("valid-component-types-1.5.json")] + [InlineData("valid-compositions-1.5.json")] + [InlineData("valid-dependency-1.5.json")] + [InlineData("valid-empty-components-1.5.json")] + [InlineData("valid-evidence-1.5.json")] + [InlineData("valid-external-reference-1.5.json")] + [InlineData("valid-formulation-1.5.json")] + [InlineData("valid-license-expression-1.5.json")] + [InlineData("valid-license-id-1.5.json")] + [InlineData("valid-license-licensing-1.5.json")] + [InlineData("valid-license-name-1.5.json")] + [InlineData("valid-machine-learning-1.5.json")] + [InlineData("valid-metadata-author-1.5.json")] + [InlineData("valid-metadata-license-1.5.json")] + [InlineData("valid-metadata-lifecycle-1.5.json")] + [InlineData("valid-metadata-manufacture-1.5.json")] + [InlineData("valid-metadata-supplier-1.5.json")] + [InlineData("valid-metadata-timestamp-1.5.json")] + [InlineData("valid-metadata-tool-1.5.json")] + [InlineData("valid-metadata-tool-deprecated-1.5.json")] + [InlineData("valid-minimal-viable-1.5.json")] + [InlineData("valid-patch-1.5.json")] + [InlineData("valid-properties-1.5.json")] + [InlineData("valid-release-notes-1.5.json")] + [InlineData("valid-saasbom-1.5.json")] + [InlineData("valid-service-1.5.json")] + [InlineData("valid-service-empty-objects-1.5.json")] + [InlineData("valid-signatures-1.5.json")] + [InlineData("valid-vulnerability-1.5.json")] + public void ValidateRoundTripTest(string filename) + { + var resourceFilename = Path.Join("Resources", "v1.5", filename); + var jsonBom = File.ReadAllText(resourceFilename); + + var bom = Serializer.Deserialize(jsonBom); + jsonBom = Serializer.Serialize(bom); + + var validationResult = Validator.Validate(jsonBom, SpecificationVersion.v1_5); + + Assert.True(validationResult.Valid, string.Join(Environment.NewLine, validationResult.Messages)); + } } } diff --git a/tests/CycloneDX.Core.Tests/Json/v1.5/__snapshots__/SerializationTests.JsonRoundTripAsyncTest_valid-formulation-1.5.json.snap b/tests/CycloneDX.Core.Tests/Json/v1.5/__snapshots__/SerializationTests.JsonRoundTripAsyncTest_valid-formulation-1.5.json.snap index 07d453e8..354f0ecd 100644 --- a/tests/CycloneDX.Core.Tests/Json/v1.5/__snapshots__/SerializationTests.JsonRoundTripAsyncTest_valid-formulation-1.5.json.snap +++ b/tests/CycloneDX.Core.Tests/Json/v1.5/__snapshots__/SerializationTests.JsonRoundTripAsyncTest_valid-formulation-1.5.json.snap @@ -269,7 +269,7 @@ "volume": { "uid": "volume-1", "name": "My volume", - "mode": "Filesystem", + "mode": "filesystem", "path": "/", "sizeAllocated": "10GB", "persistent": true, diff --git a/tests/CycloneDX.Core.Tests/Json/v1.5/__snapshots__/SerializationTests.JsonRoundTripTest_valid-formulation-1.5.json.snap b/tests/CycloneDX.Core.Tests/Json/v1.5/__snapshots__/SerializationTests.JsonRoundTripTest_valid-formulation-1.5.json.snap index 07d453e8..354f0ecd 100644 --- a/tests/CycloneDX.Core.Tests/Json/v1.5/__snapshots__/SerializationTests.JsonRoundTripTest_valid-formulation-1.5.json.snap +++ b/tests/CycloneDX.Core.Tests/Json/v1.5/__snapshots__/SerializationTests.JsonRoundTripTest_valid-formulation-1.5.json.snap @@ -269,7 +269,7 @@ "volume": { "uid": "volume-1", "name": "My volume", - "mode": "Filesystem", + "mode": "filesystem", "path": "/", "sizeAllocated": "10GB", "persistent": true,