From 76cb04c51bfd8f174725c8276d507fb9140beee9 Mon Sep 17 00:00:00 2001 From: nscuro Date: Tue, 17 Oct 2023 14:08:00 +0200 Subject: [PATCH] Fix `metadata>component>properties` XML deserialization Fixes #336 Signed-off-by: nscuro --- .../java/org/cyclonedx/model/Component.java | 2 ++ .../org/cyclonedx/parsers/JsonParserTest.java | 7 +++++++ .../org/cyclonedx/parsers/XmlParserTest.java | 7 +++++++ src/test/resources/regression/issue336.json | 17 +++++++++++++++++ src/test/resources/regression/issue336.xml | 13 +++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 src/test/resources/regression/issue336.json create mode 100644 src/test/resources/regression/issue336.xml diff --git a/src/main/java/org/cyclonedx/model/Component.java b/src/main/java/org/cyclonedx/model/Component.java index 2fe1af970..7ae2df3ae 100644 --- a/src/main/java/org/cyclonedx/model/Component.java +++ b/src/main/java/org/cyclonedx/model/Component.java @@ -35,6 +35,7 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; import com.github.packageurl.PackageURL; +import org.cyclonedx.util.deserializer.PropertiesDeserializer; @SuppressWarnings("unused") @JacksonXmlRootElement(localName = "component") @@ -364,6 +365,7 @@ public void setExternalReferences(List externalReferences) { @JacksonXmlElementWrapper(localName = "properties") @JacksonXmlProperty(localName = "property") + @JsonDeserialize(using = PropertiesDeserializer.class) public List getProperties() { return properties; } diff --git a/src/test/java/org/cyclonedx/parsers/JsonParserTest.java b/src/test/java/org/cyclonedx/parsers/JsonParserTest.java index bf062158a..50d2797ea 100644 --- a/src/test/java/org/cyclonedx/parsers/JsonParserTest.java +++ b/src/test/java/org/cyclonedx/parsers/JsonParserTest.java @@ -185,4 +185,11 @@ public void testParsedObjects15Bom_validTools() throws Exception { assertCommonBomProperties(bom, Version.VERSION_15); assertMetadata_validTools(bom.getMetadata()); } + + @Test + public void testIssue336Regression() throws Exception { + final Bom bom = getJsonBom("regression/issue336.json"); + assertEquals("foo", bom.getMetadata().getComponent().getProperties().get(0).getName()); + assertEquals("bar", bom.getMetadata().getComponent().getProperties().get(0).getValue()); + } } diff --git a/src/test/java/org/cyclonedx/parsers/XmlParserTest.java b/src/test/java/org/cyclonedx/parsers/XmlParserTest.java index a1b2d9e92..f128359e1 100644 --- a/src/test/java/org/cyclonedx/parsers/XmlParserTest.java +++ b/src/test/java/org/cyclonedx/parsers/XmlParserTest.java @@ -341,4 +341,11 @@ public void testParsedObjects14Bom_WithVulnsExtension() throws Exception { assertEquals(1, bom.getVersion()); assertNull(bom.getVulnerabilities()); } + + @Test + public void testIssue336Regression() throws Exception { + final Bom bom = getXmlBom("regression/issue336.xml"); + assertEquals("foo", bom.getMetadata().getComponent().getProperties().get(0).getName()); + assertEquals("bar", bom.getMetadata().getComponent().getProperties().get(0).getValue()); + } } diff --git a/src/test/resources/regression/issue336.json b/src/test/resources/regression/issue336.json new file mode 100644 index 000000000..05e4d151f --- /dev/null +++ b/src/test/resources/regression/issue336.json @@ -0,0 +1,17 @@ +{ + "bomFormat" : "CycloneDX", + "specVersion" : "1.4", + "serialNumber": "urn:uuid:1624fa6f-aebe-4dba-8ead-f2c876c9b832", + "version" : 1, + "metadata": { + "component": { + "name": "acme-app", + "properties": [ + { + "name": "foo", + "value": "bar" + } + ] + } + } +} diff --git a/src/test/resources/regression/issue336.xml b/src/test/resources/regression/issue336.xml new file mode 100644 index 000000000..9a3fae5cd --- /dev/null +++ b/src/test/resources/regression/issue336.xml @@ -0,0 +1,13 @@ + + + + + acme-app + + bar + + + + \ No newline at end of file