XmlParser.parse(InputStream) and parse(Reader) leave Bom.getSpecVersion() null

Open Beginner friendly
#1,001 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java, xml
Domain
backend

Research direction

The bug is in src/main/java/org/cyclonedx/parsers/XmlParser.java: parse(File) and parse(byte[]) call identifySchemaVersion and injectSchemaVersion to set specVersion, while parse(InputStream) and parse(Reader) (around lines 108-125) return MAPPER.readValue(..., Bom.class) directly. Start by reading those four methods and run the Repro snippet from the issue. Done when the stream and reader paths print 1.5 and Bom.equals matches the byte[] result.

Written by the indexing model from the issue text.

Description

Parsing a CycloneDX XML BOM with XmlParser.parse(InputStream) or XmlParser.parse(Reader) leaves Bom.getSpecVersion() as null, whereas XmlParser.parse(byte[]) and XmlParser.parse(File) populate it (for example, "1.5") on the same XML input.

Save pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>repro</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.cyclonedx</groupId>
      <artifactId>cyclonedx-core-java</artifactId>
      <version>13.5.0</version>
    </dependency>
  </dependencies>
</project>

Save src/main/java/Repro.java:

import org.cyclonedx.model.Bom;
import org.cyclonedx.parsers.XmlParser;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

public class Repro {
    public static void main(String[] args) throws Exception {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<bom xmlns=\"http://cyclonedx.org/schema/bom/1.5\" version=\"1\">"
            + "<components><component type=\"library\"><name>log4j-core</name><version>2.17.0</version></component></components>"
            + "</bom>";
        XmlParser parser = new XmlParser();
        Bom fromBytes = parser.parse(xml.getBytes(StandardCharsets.UTF_8));
        Bom fromStream = parser.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
        Bom fromReader = parser.parse(new StringReader(xml));
        System.out.println("fromBytes.specVersion=" + fromBytes.getSpecVersion());
        System.out.println("fromStream.specVersion=" + fromStream.getSpecVersion());
        System.out.println("fromReader.specVersion=" + fromReader.getSpecVersion());
    }
}

Run:

mvn -q compile exec:java -Dexec.mainClass=Repro

Actual output:

fromBytes.specVersion=1.5
fromStream.specVersion=null
fromReader.specVersion=null

Expected output: all three calls print 1.5, matching Bom.getSpecVersion() (which documents that specVersion is populated when parsing via Parser and provides no setter) as well as XmlParser.parse(File), XmlParser.parse(byte[]), and JsonParser. Because Bom.equals includes specVersion, fromStream.equals(fromBytes) and fromReader.equals(fromBytes) also evaluate to false.

Tested with cyclonedx-core-java 13.5.0 and master (e591b11c6a).

In src/main/java/org/cyclonedx/parsers/XmlParser.java, parse(File) and parse(byte[]) extract the root namespace version with identifySchemaVersion(...) and set Bom.specVersion via injectSchemaVersion(...), whereas parse(InputStream) and parse(Reader) (lines 108-125) return MAPPER.readValue(..., Bom.class) directly.

Credit: https://ucsbplse.github.io/

Dominant language
Java
Stars
124
Forks
97
Avg merge
2d 14h
Merged PRs (30d)
11

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from CycloneDX/cyclonedx-core-java

All issues in CycloneDX/cyclonedx-core-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.