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
12 changes: 9 additions & 3 deletions lib/sarifreport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <set>
#include <sstream>

static const char sarifVersion[] = "2.1.0";
static const char sarifSchema[] = "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json";

void SarifReport::addFinding(ErrorMessage msg)
{
mFindings.push_back(std::move(msg));
Expand Down Expand Up @@ -180,11 +183,14 @@ std::string SarifReport::serialize(std::string productName) const
version.erase(version.find(' '), std::string::npos);

picojson::object doc;
doc["version"] = picojson::value("2.1.0");
doc["$schema"] = picojson::value("https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json");
doc["$schema"] = picojson::value(sarifSchema);
doc["runs"] = serializeRuns(productName, version);

return picojson::value(doc).serialize(true);
// Insert "version" property at the start.
// From SARIF specification (https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790730):
// Although the order in which properties appear in a JSON object value is not semantically significant, the version property SHOULD appear first.

return "{\n \"version\": \"" + std::string(sarifVersion) + "\"," + picojson::value(doc).serialize(true).substr(1);
}

std::string SarifReport::sarifSeverity(const ErrorMessage& errmsg)
Expand Down
4 changes: 4 additions & 0 deletions test/testsarifreport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class TestSarifReport : public TestFixture
ASSERT_EQUALS("2.1.0", root.at("version").get<std::string>());
ASSERT(root.at("$schema").get<std::string>().find("sarif-schema-2.1.0") != std::string::npos);

// From SARIF specification (https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790730):
// Although the order in which properties appear in a JSON object value is not semantically significant, the version property SHOULD appear first.
ASSERT_EQUALS("{\n \"version\": \"2.1.0\"", sarif.substr(0,22));

const picojson::array& runs = root.at("runs").get<picojson::array>();
ASSERT_EQUALS(1U, runs.size());

Expand Down
Loading