Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for consistency between JSON, xml, and protobuf serialization #331

Draft
wants to merge 20 commits into
base: cdx1.6
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3911b77
add tests for consistency between JSON and protobuf serialization (dr…
andreas-hilti Aug 26, 2024
f5de0e9
Fix CertificationLevel protobuf serialization
andreas-hilti Aug 27, 2024
c5eee25
add tests for consistency between JSON and protobuf serialization (dr…
andreas-hilti Aug 26, 2024
51e1d0b
Merge branch 'cdx1.6' into cdx1.6_consistency_tests
andreas-hilti Aug 29, 2024
8a9d0ee
Merge branch 'cdx1.6_consistency_tests' of https://github.com/andreas…
andreas-hilti Aug 29, 2024
521a2e9
Add xml json consistency tests
andreas-hilti Aug 30, 2024
2ad49f3
Adapt test resources to ensure consistency between protobuf and json
andreas-hilti Aug 30, 2024
06f2e77
Use Protobuf serialization for comparison
andreas-hilti Aug 31, 2024
4ec3aad
Adapt test resources to ensure consistency between xml and json
andreas-hilti Aug 31, 2024
ab86851
Further adapt test resources to ensure consistency between protobuf a…
andreas-hilti Aug 31, 2024
fd3076d
Fix style and fix minor issues
andreas-hilti Aug 31, 2024
5beb671
Add tests for consistency of enum serialization in xml and json
andreas-hilti Sep 1, 2024
e936464
Merge branch 'cdx1.6' into cdx1.6_consistency_tests
andreas-hilti Sep 1, 2024
7705280
Adapt snapshots
andreas-hilti Sep 1, 2024
70cc397
Adapt valid-bom example
andreas-hilti Sep 14, 2024
05fa0f7
Merge branch 'cdx1.6' into cdx1.6_consistency_tests
andreas-hilti Sep 14, 2024
d532ad9
Fix valid-bom sample
andreas-hilti Sep 14, 2024
70b6c60
Adapt snaptshots
andreas-hilti Sep 14, 2024
52ad1fd
Skip expected failure
andreas-hilti Sep 14, 2024
3473898
Skip tests if protoc is not available
andreas-hilti Sep 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/CycloneDX.Core/Json/Serializer.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using CycloneDX;
using CycloneDX.Models;

[assembly: InternalsVisibleTo("CycloneDX.Core.Tests")]

namespace CycloneDX.Json
{
/// <summary>
Expand Down Expand Up @@ -135,5 +138,19 @@ internal static string Serialize(Models.Attestation obj)
Contract.Requires(obj != null);
return JsonSerializer.Serialize(obj, _options);
}
internal static string Serialize<TValue>(TValue value)
{
Contract.Requires(value != null);
var json = JsonSerializer.Serialize<TValue>(value, _options);
return json;
}

internal static string Serialize(object value, Type type)
{
Contract.Requires(value != null);
var json = JsonSerializer.Serialize(value, type, _options);
return json;
}

}
}
39 changes: 39 additions & 0 deletions src/CycloneDX.Core/Xml/Serializer.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using CycloneDX.Models;
using ProtoBuf;

[assembly: InternalsVisibleTo("CycloneDX.Core.Tests")]

namespace CycloneDX.Xml
{
/// <summary>
Expand Down Expand Up @@ -83,6 +86,12 @@ private static XmlSerializer GetXmlSerializer(SpecificationVersion specification
}
}

internal static XmlSerializer GetXmlSerializer(Type type,SpecificationVersion specificationVersion)
{
var attributeOverrides = GetOverrides(specificationVersion);
return new XmlSerializer(type, attributeOverrides);
}

// Todo: this is a workaround to set the correct namespace
// when writing licenses. Can this be avoided?
private readonly static Dictionary<XmlWriter, string> WriterToNamespace = new Dictionary<XmlWriter, string>();
Expand Down Expand Up @@ -120,6 +129,25 @@ public static void Serialize(Bom bom, Stream outputStream)
}
}

internal static void Serialize(object value, Type type, SpecificationVersion specVersion, Stream outputStream)
{
Contract.Requires(value != null);

var serializer = GetXmlSerializer(type, specVersion);
using (var xmlWriter = XmlWriter.Create(outputStream, WriterSettings))
{
lock (WriterToNamespace)
{
WriterToNamespace[xmlWriter] = SpecificationVersionHelpers.XmlNamespace(specVersion);
}
serializer.Serialize(xmlWriter, value);
lock (WriterToNamespace)
{
WriterToNamespace.Remove(xmlWriter);
}
}
}

/// <summary>
/// Serializes a CycloneDX BOM to a string.
/// </summary>
Expand All @@ -136,6 +164,17 @@ public static string Serialize(Bom bom)
}
}

internal static string Serialize(object value, Type type, SpecificationVersion specVersion)
{
Contract.Requires(value != null);

using (var ms = new MemoryStream())
{
Serialize(value, type, specVersion, ms);
return Encoding.UTF8.GetString(ms.ToArray());
}
}

internal static XmlSerializer GetElementSerializer<T>(SpecificationVersion specVersion, string elementName)
{
if (!_elementSerializers.ContainsKey(specVersion))
Expand Down
Loading
Loading