From 293c004b435a9b445a7e46ad5ab6b41c840813a1 Mon Sep 17 00:00:00 2001 From: Dustin Popp Date: Thu, 23 Jan 2025 15:03:36 -0600 Subject: [PATCH] fix: ingore violations from invalid locations of an api definition Some extensions in an API definition, like those providing SDK-related metadata, are not valid places to check for rule violations. Since some built-in, Spectral rules use broad JSON paths for their rules, it is possible for them to report false positives within extension objects, even when the extension objects contain information that is completely unrelated to the rule (like the typed-enum rule). To avoid this in a generic way, this change filters out any rule violations that occur within our internal, SDK-related metadata extension and provides for any potential locations that may be added in the future. Signed-off-by: Dustin Popp --- packages/validator/src/spectral/index.js | 13 +++++++++++++ .../mock-files/oas31/err-and-warn.yaml | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/packages/validator/src/spectral/index.js b/packages/validator/src/spectral/index.js index e10bae1a..14eb0e8a 100644 --- a/packages/validator/src/spectral/index.js +++ b/packages/validator/src/spectral/index.js @@ -71,6 +71,12 @@ function convertResults(spectralResults, { config, logger }) { continue; } + // Ingore any rule violations that occur in sections of the spec that are + // not valid locations for API validation. + if (invalidLocation(r.path)) { + continue; + } + const severity = convertSpectralSeverity(r.severity); // only collect errors when "errors only" is true if (errorsOnly && r.severity > 0) { @@ -224,3 +230,10 @@ function convertSpectralSeverity(s) { const mapping = { 0: 'error', 1: 'warning', 2: 'info', 3: 'hint' }; return mapping[s]; } + +function invalidLocation(pathArray) { + // The 'x-sdk-apiref' extension is an IBM internal section for providing + // SDK-related metadata. It is not appropriate for the validator to consider. + const invalidLocations = ['x-sdk-apiref']; + return invalidLocations.some(l => pathArray.includes(l)); +} diff --git a/packages/validator/test/cli-validator/mock-files/oas31/err-and-warn.yaml b/packages/validator/test/cli-validator/mock-files/oas31/err-and-warn.yaml index 41ce5a8f..4cd92001 100644 --- a/packages/validator/test/cli-validator/mock-files/oas31/err-and-warn.yaml +++ b/packages/validator/test/cli-validator/mock-files/oas31/err-and-warn.yaml @@ -210,3 +210,10 @@ components: UnusedString: description: string type: string + +x-sdk-apiref: + type: int64 + enum: + - this would trigger spectrals 'typed-enum' rule + - without our custom filter for invalid openapi locations + - this just tests that no warning is emitted for the 'x-sdk-apiref' location