CycloneDX / CycloneDX/cyclonedx-core-java

JSON serialized Property Value containing multiple spaces get "squashed"

Aperta
#583 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
120
Fork
90
Merge medio
12h 43m
PR unite (30g)
18

Descrizione

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;
}
}
}
```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.