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

Fix the xml serialization order #246

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions src/CycloneDX.Core/Models/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,19 @@ public bool NonNullableModified
public List<ExternalReference> ExternalReferences { get; set; }
public bool ShouldSerializeExternalReferences() { return ExternalReferences?.Count > 0; }

[XmlArray("components")]
[ProtoMember(21)]
public List<Component> Components { get; set; }
//In the xml format, Properties is in front of Components.
//XML serialization uses the member order unless explicitly specified differently.
public bool ShouldSerializeComponents() { return Components?.Count > 0; }

[XmlArray("properties")]
[XmlArrayItem("property")]
[ProtoMember(22)]
public List<Property> Properties { get; set; }
public bool ShouldSerializeProperties() { return Properties?.Count > 0; }

[XmlArray("components")]
[ProtoMember(21)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case: ProtoMember numbers should remain as they were, right? Or should match the order in other serializers?..

Copy link
Contributor Author

@andreas-hilti andreas-hilti Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they need to remain as they are; they are defined in
https://github.com/CycloneDX/specification/blob/c320fc0f0b46873864927d9d5684eea7ba439728/schema/bom-1.4.proto#L110-L113
(The order in the proto is different from the one in the xml.)

public List<Component> Components { get; set; }

[XmlElement("evidence")]
[ProtoMember(23)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.4">
<metadata>
<properties>
<property name="Foo">Bar</property>
<property name="Foo">You</property>
<property name="Foo">Two</property>
<property name="Bar">Foo</property>
</properties>
</metadata>
<components>
<component type="library">
<name>acme-library</name>
<version>1.0.0</version>
<properties>
<property name="Foo">Bar</property>
<property name="Bar">Foo</property>
</properties>
<components>
<component type="library">
<name>acme-sub-library</name>
<version>1.0.0</version>
<properties>
<property name="Foo">Bar</property>
<property name="Bar">Foo</property>
</properties>
</component>
</components>
</component>
</components>
</bom>
1 change: 1 addition & 0 deletions tests/CycloneDX.Core.Tests/Xml/v1.4/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class SerializationTests
[InlineData("valid-minimal-viable-1.4.xml")]
[InlineData("valid-patch-1.4.xml")]
[InlineData("valid-properties-1.4.xml")]
[InlineData("valid-properties-components-1.4.xml")]
// [InlineData("valid-random-attributes-1.4.xml")]
[InlineData("valid-release-notes-1.4.xml")]
[InlineData("valid-service-1.4.xml")]
Expand Down