CycloneDX / CycloneDX/cyclonedx-core-java

JSON serialized Property Value containing multiple spaces get "squashed"

Ouverte
#583 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Java
Étoiles
120
Forks
90
Merge moyen
12 h 43 min
PR mergées (30 j)
18

Description

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

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par BomJsonGenerator, JsonParser et la reproduction Issue583 fournie, puis comparez les chemins de sérialisation JSON et XML pour les valeurs de Property. Exécutez la reproduction et vérifiez que plusieurs espaces consécutifs sont conservés lors de la sérialisation et de l'analyse JSON, comme dans la valeur XML.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
java
Domaine
backend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.