Skip to content

Commit

Permalink
Merge pull request #1516 from snyk/fix/dont-create-json-if-you-dont-n…
Browse files Browse the repository at this point in the history
…eed-it

Don't create JSON if we don't need it
  • Loading branch information
maxjeffos committed Nov 11, 2020
2 parents f075a13 + f6d7217 commit 312ca91
Show file tree
Hide file tree
Showing 6 changed files with 1,126 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/cli/commands/test/formatters/format-test-results.ts
Expand Up @@ -26,6 +26,7 @@ import { formatDockerBinariesIssues } from './docker';
import { createSarifOutputForContainers } from '../sarif-output';
import { createSarifOutputForIac } from '../iac-output';
import { isNewVuln, isVulnFixable } from '../vuln-helpers';
import { jsonStringifyLargeObject } from '../../../../lib/json';

export function formatJsonOutput(jsonData) {
const jsonDataClone = _.cloneDeep(jsonData);
Expand All @@ -46,29 +47,29 @@ export function extractDataToSendFromResults(
options: Options,
): OutputDataTypes {
let sarifData = {};
let stringifiedSarifData = '';
if (options.sarif || options['sarif-file-output']) {
sarifData = !options.iac
? createSarifOutputForContainers(results)
: createSarifOutputForIac(results);
stringifiedSarifData = jsonStringifyLargeObject(sarifData);
}

const stringifiedJsonData = JSON.stringify(
formatJsonOutput(jsonData),
null,
2,
);
const stringifiedSarifData = JSON.stringify(sarifData, null, 2);
let stringifiedJsonData = '';
if (options.json || options['json-file-output']) {
stringifiedJsonData = jsonStringifyLargeObject(formatJsonOutput(jsonData));
}

const dataToSend = options.sarif ? sarifData : jsonData;
const stringifiedData = options.sarif
? stringifiedSarifData
: stringifiedJsonData;

return {
stdout: dataToSend,
stringifiedData,
stringifiedJsonData,
stringifiedSarifData,
stdout: dataToSend, // this is for the human-readable stdout output and is set (but not used) even if --json or --sarif is set
stringifiedData, // this will be used to display either the Snyk or SARIF format JSON to stdout if --json or --sarif is set
stringifiedJsonData, // this will be used for the --json-file-output=<file.json> option
stringifiedSarifData, // this will be used for the --sarif-file-output=<file.json> option
};
}

Expand Down
20 changes: 20 additions & 0 deletions src/lib/json.ts
@@ -0,0 +1,20 @@
const debug = require('debug')('snyk-json');

/**
* Attempt to json-stringify an object which is potentially very large and might exceed the string limit.
* If it does exceed the string limit, try again without pretty-print to hopefully come out below the string limit.
* @param obj the object from which you want to get a JSON string
*/
export function jsonStringifyLargeObject(obj: any): string {
let res = '';
try {
// first try pretty-print
res = JSON.stringify(obj, null, 2);
return res;
} catch (err) {
// if that doesn't work, try non-pretty print
debug('JSON.stringify failed - trying again without pretty print', err);
res = JSON.stringify(obj);
return res;
}
}

0 comments on commit 312ca91

Please sign in to comment.