Skip to content

Commit

Permalink
fix(ibm-api-symmetry): print info logs in coherent order
Browse files Browse the repository at this point in the history
The code to determine if one schema is a graph fragment of the other
uses a depth-first algorithm that prints a log, if it determines the
schema to violate the graph fragment pattern, with the reason behind
the violation. Due to the depth-first nature of the algorithm, the
logs are currently printed in depth first order, which is not as
coherent for the user to read.

This change introduces a stack to collect the logs during processing
and print them in reverse order afterwards, to give the user a better
sense of what happened during the processing.

Signed-off-by: Dustin Popp <dpopp07@gmail.com>
  • Loading branch information
dpopp07 committed Jan 24, 2025
1 parent 9d0986f commit 2a68a5e
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/ruleset/src/functions/api-symmetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const {
let ruleId;
let logger;

// The graph fragment check is depth-first. Use this stack to
// print the relevant info logs in a sane order.
const infoLogStack = [];

/**
* The implementation for this rule makes assumptions that are dependent on the
* presence of the following other rules:
Expand Down Expand Up @@ -175,7 +179,7 @@ function checkForGraphFragmentPattern(
schema => getSchemaType(variant) === getSchemaType(schema)
)
) {
logger.info(
infoLogStack.push(
`${ruleId}: variant and canonical schemas are different types`
);
result = false;
Expand All @@ -199,7 +203,7 @@ function checkForGraphFragmentPattern(
)
)
) {
logger.info(
infoLogStack.push(
`${ruleId}: variant is array with schema that is not a graph fragment of canonical items schema`
);
result = false;
Expand All @@ -223,7 +227,7 @@ function checkForGraphFragmentPattern(
)
)
) {
logger.info(
infoLogStack.push(
`${ruleId}: variant is dictionary with an additionalProperties schema that is not a graph fragment of canonical`
);
result = false;
Expand Down Expand Up @@ -259,7 +263,7 @@ function checkForGraphFragmentPattern(
)
)
) {
logger.info(
infoLogStack.push(
`${ruleId}: variant is dictionary with a patternProperties schema that is not a graph fragment of canonical`
);
result = false;
Expand Down Expand Up @@ -302,7 +306,7 @@ function checkForGraphFragmentPattern(
// Note: Prototype schemas are allowed to define writeOnly properties
// that don't exist on the canonical schema.
if (!valid && !(considerWriteOnly && prop.writeOnly)) {
logger.info(
infoLogStack.push(
propExistsSomewhere
? `${ruleId}: nested object property ${name} is not a graph fragment of canonical property ${name}`
: `${ruleId}: property '${name}' does not exist on the canonical schema`
Expand All @@ -325,7 +329,7 @@ function checkForGraphFragmentPattern(
true
)
) {
logger.info(
infoLogStack.push(
`${ruleId}: variant schema applicator '${applicator}' is not a graph fragment of the canonical schema`
);
result = false;
Expand Down Expand Up @@ -410,6 +414,12 @@ function checkForGraphFragmentPattern(
false
);

// Print the logs gathered within the `isGraphFragment` function,
// in reverse order - it will be coherent for the user.
while (infoLogStack.length) {
logger.info(infoLogStack.pop());
}

logger.debug(
`${ruleId}: isGraphFragment() returned [${variantIsGraphFragment},${variantOmitsProperties}]`
);
Expand Down

0 comments on commit 2a68a5e

Please sign in to comment.