CycloneDX / CycloneDX/cyclonedx-core-java

JSON serialized Property Value containing multiple spaces get "squashed"

Offen
#583 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Java
Sterne
120
Forks
90
Ø Merge
12 Std. 43 Min.
Gemergte PRs (30 T.)
18

Beschreibung

If a Property Value containing multiple consecutive spaces, eg.
```
"A property value containing multiple spaces!"
```
is serialized with JSON, the spaces get squashed, should they be preserved??

XML Serialization is correctly preserving the spaces.

Test Case:
```
import org.cyclonedx.exception.GeneratorException;
import org.cyclonedx.generators.json.BomJsonGenerator;
import org.cyclonedx.generators.xml.BomXmlGenerator;
import org.cyclonedx.model.Bom;
import org.cyclonedx.model.Component;
import org.cyclonedx.model.Property;
import org.cyclonedx.model.Metadata;
import org.cyclonedx.parsers.JsonParser;
import org.cyclonedx.parsers.XmlParser;
import org.cyclonedx.Version;
import java.io.FileWriter;
import java.io.FileReader;
import java.util.List;
import java.util.LinkedList;
import java.util.UUID;

public final class Issue583 {

public static void main(final String[] args) {
try {
Bom bom = new Bom();
bom.setSerialNumber("urn:uuid:" + UUID.randomUUID());

// Component test with Property containing multiple spaces
Component comp1 = new Component();
comp1.setType(Component.Type.APPLICATION);
comp1.setName("COMP 1");
comp1.setVersion("v1");

Property prop1 = new Property();
prop1.setName("PROP1");
prop1.setValue("A property value containing multiple spaces!");
comp1.addProperty(prop1);

bom.addComponent(comp1);

// Serialize...
writeJSONfile(bom, "Issue583_SBOM.json");
writeXMLfile(bom, "Issue583_SBOM.xml");

// Deserialize...
Bom bomJson = readJSONfile("Issue583_SBOM.json");
Bom bomXml = readXMLfile("Issue583_SBOM.xml");

// Check json and xml Property value is the same?
String jsonValue = bomJson.getComponents().get(0).getProperties().get(0).getValue();
String xmlValue = bomXml.getComponents().get(0).getProperties().get(0).getValue();

System.out.println("JSON Property value = "+jsonValue);
System.out.println("XML Property value = "+xmlValue);

if (!jsonValue.equals(xmlValue)) {
System.out.println("ERROR: JSON != XML Property value");
System.exit(1);
} else {
System.out.println("SUCCESS: JSON == XML Property value");
}
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}

static String generateBomJson(final Bom bom) throws GeneratorException {
BomJsonGenerator bomGen = new BomJsonGenerator(bom, Version.VERSION_16);
String json = bomGen.toJsonString();
return json;
}

static String generateBomXml(final Bom bom) throws GeneratorException {
BomXmlGenerator bomGen = new BomXmlGenerator(bom, Version.VERSION_16);
String xml = bomGen.toXmlString();
return xml;
}

// Writes the BOM object to the specified file.
static void writeJSONfile(final Bom bom, final String fileName) {
FileWriter file;
try {
String json = generateBomJson(bom);

file = new FileWriter(fileName);
file.write(json);
file.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

// Writes the BOM object to the specified XML file.
static void writeXMLfile(final Bom bom, final String fileName) {
FileWriter file;
try {
String xml = generateBomXml(bom);

file = new FileWriter(fileName);
file.write(xml);
file.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

// Returns a parsed BOM object from the specified file.
static Bom readJSONfile(final String fileName) {
Bom bom = null;
try {
FileReader reader = new FileReader(fileName);
JsonParser parser = new JsonParser();
bom = parser.parse(reader);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
return bom;
}
}

// Returns a parsed BOM object from the specified file.
static Bom readXMLfile(final String fileName) {
Bom bom = null;
try {
FileReader reader = new FileReader(fileName);
XmlParser parser = new XmlParser();
bom = parser.parse(reader);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
return bom;
}
}
}
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Start with BomJsonGenerator, JsonParser, and the supplied Issue583 reproduction, then compare the JSON and XML serialization paths for Property values. Run the reproduction and verify that multiple consecutive spaces survive JSON serialization and parsing, matching the XML value.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
java
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.