Skip to content

Commit

Permalink
fix(ibm-major-version-in-path): skip path checks if there are no paths (
Browse files Browse the repository at this point in the history
#727)

* fix(ibm-major-version-in-path): skip path checks if there are no paths

This rule checks both the `servers` entries and the path strings in an API
definition. If an API definition contains no path strings in the `paths`
object, this rule would return a false positive. This adds logic to skip
the path string checks if there are none to check.

Signed-off-by: Dustin Popp <[email protected]>
  • Loading branch information
dpopp07 authored Feb 7, 2025
1 parent bbcf9bf commit c539741
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/ruleset/src/functions/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017 - 2024 IBM Corporation.
* Copyright 2017 - 2025 IBM Corporation.
* SPDX-License-Identifier: Apache2.0
*/

Expand All @@ -13,7 +13,6 @@ module.exports = {
arrayResponses: require('./array-responses'),
avoidMultipleTypes: require('./avoid-multiple-types'),
binarySchemas: require('./binary-schemas'),
checkMajorVersion: require('./check-major-version'),
circularRefs: require('./circular-refs'),
collectionArrayProperty: require('./collection-array-property'),
consecutivePathSegments: require('./consecutive-path-segments'),
Expand All @@ -26,6 +25,7 @@ module.exports = {
etagHeaderExists: require('./etag-header-exists'),
inlineSchemas: require('./inline-schemas'),
integerAttributes: require('./integer-attributes'),
majorVersionInPath: require('./major-version-in-path'),
mergePatchProperties: require('./merge-patch-properties'),
noAmbiguousPaths: require('./no-ambiguous-paths'),
noNullableProperties: require('./no-nullable-properties'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017 - 2023 IBM Corporation.
* Copyright 2017 - 2025 IBM Corporation.
* SPDX-License-Identifier: Apache2.0
*/

Expand Down Expand Up @@ -99,6 +99,13 @@ function checkMajorVersion(apiDef) {
const paths = apiDef['paths'];
if (paths && typeof paths === 'object') {
const urls = Object.keys(paths);

if (!urls.length) {
logger.debug(`${ruleId}: no path strings to check - "paths" is empty`);

return [];
}

const versions = urls.map(url => getVersion(url));

if (versions.length > 1 && !versions.every(v => v === versions[0])) {
Expand Down
6 changes: 3 additions & 3 deletions packages/ruleset/src/rules/major-version-in-path.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Copyright 2017 - 2023 IBM Corporation.
* Copyright 2017 - 2025 IBM Corporation.
* SPDX-License-Identifier: Apache2.0
*/

const { oas3 } = require('@stoplight/spectral-formats');
const { checkMajorVersion } = require('../functions');
const { majorVersionInPath } = require('../functions');

module.exports = {
description:
Expand All @@ -14,6 +14,6 @@ module.exports = {
given: '$',
severity: 'warn',
then: {
function: checkMajorVersion,
function: majorVersionInPath,
},
};
11 changes: 10 additions & 1 deletion packages/ruleset/test/rules/major-version-in-path.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017 - 2024 IBM Corporation.
* Copyright 2017 - 2025 IBM Corporation.
* SPDX-License-Identifier: Apache2.0
*/

Expand Down Expand Up @@ -66,6 +66,15 @@ describe(`Spectral rule: ${ruleId}`, () => {
expect(results).toHaveLength(0);
});

it('should not error when there are no paths', async () => {
const testDocument = makeCopy(rootDocument);
testDocument.paths = {};

const results = await testRule(ruleId, rule, testDocument);

expect(results).toHaveLength(0);
});

it('should error when servers have urls with different versions', async () => {
const testDocument = makeCopy(rootDocument);
testDocument.servers = [
Expand Down

0 comments on commit c539741

Please sign in to comment.