Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public String toXmlString() throws GeneratorException {
return toXML(bom, true);
}

public String toXmlString(boolean prettyPrint) throws GeneratorException {
return toXML(bom, prettyPrint);
}

/**
* Creates a text representation of a CycloneDX BoM Document. This method calls {@link #toXmlString()} and will return
* an empty string if {@link #toXmlString()} throws an exception. It's preferred to call {@link #toXmlString()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ private void serializerJson(List<Property> properties, JsonGenerator jsonGenerat
private static void serializeXml(final List<Property> properties, final ToXmlGenerator xmlGenerator)
throws IOException
{
xmlGenerator.writeStartArray();
// NB - In this context Jackson already knows we're producing <property> elements so no need to explicitly try and
// write the property field name otherwise we end up with malformed XML with double wrapped <property> elements
for (Property property : properties) {
SerializerUtils.serializeProperty("property", property, xmlGenerator);
SerializerUtils.serializeProperty(null, property, xmlGenerator);
}
xmlGenerator.writeEndArray();
Comment thread
nscuro marked this conversation as resolved.
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import org.apache.commons.lang3.StringUtils;
import org.cyclonedx.Version;
import org.cyclonedx.model.ExternalReference;
import org.cyclonedx.model.Hash;
Expand Down Expand Up @@ -141,7 +142,9 @@ public static List<Hash> filterHashesByVersion(List<Hash> hashes, Version versio
}

public static void serializeProperty(String propertyName, Property prop, ToXmlGenerator xmlGenerator) throws IOException {
xmlGenerator.writeFieldName(propertyName);
if (StringUtils.isNotBlank(propertyName)) {
xmlGenerator.writeFieldName(propertyName);
}
xmlGenerator.writeStartObject();
xmlGenerator.setNextIsAttribute(true);
xmlGenerator.writeFieldName("name");
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/cyclonedx/BomJsonGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public void schema13MultipleDependenciesJsonTest() throws Exception {

static Stream<Arguments> testData() {
return Stream.of(
Arguments.of(Version.VERSION_17, "/1.7/valid-bom-1.7.xml"),
Arguments.of(Version.VERSION_16, "/1.6/valid-bom-1.6.xml"),
Arguments.of(Version.VERSION_15, "/bom-1.5.xml"),
Arguments.of(Version.VERSION_14, "/bom-1.4.xml"),
Expand All @@ -165,6 +166,13 @@ public void testJsonGeneration(Version version, String bomXmlPath)
File file = writeToFile(generator.toJsonString());
JsonParser parser = new JsonParser();
assertTrue(parser.isValid(file, version));

// Verify that with pretty printing disabled the output JSON is smaller
long prettyPrintedSize = file.length();
file = writeToFile(generator.toJsonString(false));
assertTrue(parser.isValid(file, version));
assertTrue(file.length() < prettyPrintedSize,
"Non pretty-printed output size should be smaller than pretty printed output size");
}

@Test
Expand Down
24 changes: 16 additions & 8 deletions src/test/java/org/cyclonedx/BomXmlGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public void schema12GenerationWithPedigreeDataTest() throws Exception {

static Stream<Arguments> testData() {
return Stream.of(
Arguments.of(Version.VERSION_17, "/1.7/valid-bom-1.7.json"),
Arguments.of(Version.VERSION_16, "/1.6/valid-bom-1.6.json"),
Arguments.of(Version.VERSION_15, "/bom-1.5.json"),
Arguments.of(Version.VERSION_14, "/bom-1.4.json"),
Expand All @@ -191,17 +192,24 @@ static Stream<Arguments> testData() {

@ParameterizedTest
@MethodSource("testData")
public void testXmlGeneration(Version version, String bomXmlPath)
public void testXmlGeneration(Version version, String bomJsonPath)
throws Exception
{
Bom bom = createCommonJsonBom(bomXmlPath);
BomJsonGenerator generator = BomGeneratorFactory.createJson(version, bom);
Bom bom = createCommonJsonBom(bomJsonPath);
BomXmlGenerator generator = BomGeneratorFactory.createXml(version, bom);

assertEquals(version, generator.getSchemaVersion());

File file = writeToFile(generator.toJsonString());
JsonParser parser = new JsonParser();
File file = writeToFile(generator.toXmlString());
XmlParser parser = new XmlParser();
assertTrue(parser.isValid(file, version));

// Verify that with pretty printing disabled the output XML is smaller
long prettyPrintedSize = file.length();
file = writeToFile(generator.toXmlString(false));
assertTrue(parser.isValid(file, version));
assertTrue(file.length() < prettyPrintedSize,
"Non pretty-printed output size should be smaller than pretty printed output size");
}

@Test
Expand Down Expand Up @@ -825,10 +833,10 @@ public void testIssue571() throws Exception {
component.setType(Type.APPLICATION);
bom.getMetadata().getToolChoice().getComponents().add(component);

BomJsonGenerator generator = BomGeneratorFactory.createJson(version, bom);
File loadedFile = writeToFile(generator.toJsonString());
BomXmlGenerator generator = BomGeneratorFactory.createXml(version, bom);
File loadedFile = writeToFile(generator.toXmlString());

JsonParser parser = new JsonParser();
XmlParser parser = new XmlParser();
assertTrue(parser.isValid(loadedFile, version));
}

Expand Down
6 changes: 5 additions & 1 deletion src/test/resources/bom-1.5.json
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@
"timeEnd": "2023-01-01T00:00:00+10:00",
"workspaces": [
{
"bom-ref": "workspace-1",
"bom-ref": "workspace-2",
"uid": "workspace-1",
"name": "My workspace",
"aliases": [ "default-workspace" ],
Expand Down Expand Up @@ -942,6 +942,10 @@
{
"name": "Foo",
"value": "Bar"
},
{
"name": "Another",
"value": "Bar"
}
]
}
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/bom-1.5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@
</runtimeTopology>
<properties>
<property name="Foo">Bar</property>
<property name="Another">Bar</property>
</properties>
</workflow>
</workflows>
Expand Down
Loading