Skip to content

Commit

Permalink
fix: serialization of enums VolumeMode and DataType (#290)
Browse files Browse the repository at this point in the history
Fix serialization of enums VolumeMode and DataType
Add test to validate SBOM after round trip

---------

Signed-off-by: andreas hilti <andreas.hilti@bluewin.ch>
Co-authored-by: MTsfoni <mibau89@gmail.com>
Signed-off-by: MTsfoni <mibau89@gmail.com>
  • Loading branch information
andreas-hilti and mtsfoni authored May 19, 2024
1 parent 7a0682d commit 93fc4a2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/CycloneDX.Core/Json/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public static JsonSerializerOptions GetJsonSerializerOptions()
options.Converters.Add(new HyphenEnumConverter<Output.OutputType>());
options.Converters.Add(new HyphenEnumConverter<ModelCard.ModelParameterApproachType>());
options.Converters.Add(new UnderscoreEnumConverter<Status>());
options.Converters.Add(new HyphenEnumConverter<Volume.VolumeMode>());
options.Converters.Add(new HyphenEnumConverter<Data.DataType>());

options.Converters.Add(new JsonStringEnumConverter());

Expand Down
50 changes: 50 additions & 0 deletions tests/CycloneDX.Core.Tests/Json/v1.5/ValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
"volume": {
"uid": "volume-1",
"name": "My volume",
"mode": "Filesystem",
"mode": "filesystem",
"path": "/",
"sizeAllocated": "10GB",
"persistent": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
"volume": {
"uid": "volume-1",
"name": "My volume",
"mode": "Filesystem",
"mode": "filesystem",
"path": "/",
"sizeAllocated": "10GB",
"persistent": true,
Expand Down

0 comments on commit 93fc4a2

Please sign in to comment.