Skip to content

Commit c539741

Browse files
authored
fix(ibm-major-version-in-path): skip path checks if there are no paths (#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 <dpopp07@gmail.com>
1 parent bbcf9bf commit c539741

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

packages/ruleset/src/functions/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 - 2024 IBM Corporation.
2+
* Copyright 2017 - 2025 IBM Corporation.
33
* SPDX-License-Identifier: Apache2.0
44
*/
55

@@ -13,7 +13,6 @@ module.exports = {
1313
arrayResponses: require('./array-responses'),
1414
avoidMultipleTypes: require('./avoid-multiple-types'),
1515
binarySchemas: require('./binary-schemas'),
16-
checkMajorVersion: require('./check-major-version'),
1716
circularRefs: require('./circular-refs'),
1817
collectionArrayProperty: require('./collection-array-property'),
1918
consecutivePathSegments: require('./consecutive-path-segments'),
@@ -26,6 +25,7 @@ module.exports = {
2625
etagHeaderExists: require('./etag-header-exists'),
2726
inlineSchemas: require('./inline-schemas'),
2827
integerAttributes: require('./integer-attributes'),
28+
majorVersionInPath: require('./major-version-in-path'),
2929
mergePatchProperties: require('./merge-patch-properties'),
3030
noAmbiguousPaths: require('./no-ambiguous-paths'),
3131
noNullableProperties: require('./no-nullable-properties'),

packages/ruleset/src/functions/check-major-version.js packages/ruleset/src/functions/major-version-in-path.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 - 2023 IBM Corporation.
2+
* Copyright 2017 - 2025 IBM Corporation.
33
* SPDX-License-Identifier: Apache2.0
44
*/
55

@@ -99,6 +99,13 @@ function checkMajorVersion(apiDef) {
9999
const paths = apiDef['paths'];
100100
if (paths && typeof paths === 'object') {
101101
const urls = Object.keys(paths);
102+
103+
if (!urls.length) {
104+
logger.debug(`${ruleId}: no path strings to check - "paths" is empty`);
105+
106+
return [];
107+
}
108+
102109
const versions = urls.map(url => getVersion(url));
103110

104111
if (versions.length > 1 && !versions.every(v => v === versions[0])) {
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* Copyright 2017 - 2023 IBM Corporation.
2+
* Copyright 2017 - 2025 IBM Corporation.
33
* SPDX-License-Identifier: Apache2.0
44
*/
55

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

99
module.exports = {
1010
description:
@@ -14,6 +14,6 @@ module.exports = {
1414
given: '$',
1515
severity: 'warn',
1616
then: {
17-
function: checkMajorVersion,
17+
function: majorVersionInPath,
1818
},
1919
};

packages/ruleset/test/rules/major-version-in-path.test.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 - 2024 IBM Corporation.
2+
* Copyright 2017 - 2025 IBM Corporation.
33
* SPDX-License-Identifier: Apache2.0
44
*/
55

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

69+
it('should not error when there are no paths', async () => {
70+
const testDocument = makeCopy(rootDocument);
71+
testDocument.paths = {};
72+
73+
const results = await testRule(ruleId, rule, testDocument);
74+
75+
expect(results).toHaveLength(0);
76+
});
77+
6978
it('should error when servers have urls with different versions', async () => {
7079
const testDocument = makeCopy(rootDocument);
7180
testDocument.servers = [

0 commit comments

Comments
 (0)