Skip to content

Commit ea69c29

Browse files
author
Sophia Marie Terry
committed
CLOUDP-306294: Refactored and commented opID related utilities
1 parent 3879992 commit ea69c29

File tree

8 files changed

+73
-37
lines changed

8 files changed

+73
-37
lines changed

tools/spectral/ipa/rulesets/functions/IPA104ValidOperationID.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { generateOperationID } from './utils/operationIdGeneration.js';
22
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
33
import { hasException } from './utils/exceptions.js';
44
import { getResourcePathItems, isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
5-
import { isGetOverride, isLegacyCustomMethod, isListOverride } from './utils/extensions.js';
5+
import { hasCustomMethodOverride, hasMethodVerbOverride } from './utils/extensions.js';
66
import { invalidGetMethod } from './utils/methodLogic.js';
77

88
const RULE_NAME = 'xgen-IPA-104-valid-operation-id';
@@ -14,10 +14,10 @@ export default (input, { methodName }, { path, documentInventory }) => {
1414
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
1515

1616
if (
17-
isLegacyCustomMethod(input) ||
17+
hasCustomMethodOverride(input) ||
1818
isCustomMethodIdentifier(resourcePath) ||
19-
isListOverride(input) ||
20-
(invalidGetMethod(resourcePath, resourcePaths) && !isGetOverride(input))
19+
hasMethodVerbOverride(input, "list") ||
20+
(invalidGetMethod(resourcePath, resourcePaths) && !hasMethodVerbOverride(input, methodName))
2121
) {
2222
return;
2323
}

tools/spectral/ipa/rulesets/functions/IPA105ValidOperationID.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { collectAdoption, collectAndReturnViolation, collectException } from './
33
import { getResourcePathItems, isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
55
import { invalidListMethod } from './utils/methodLogic.js';
6-
import { isLegacyCustomMethod, isGetOverride, isListOverride } from './utils/extensions.js';
6+
import { hasCustomMethodOverride, hasMethodVerbOverride } from './utils/extensions.js';
77

88
const RULE_NAME = 'xgen-IPA-105-valid-operation-id';
99
const ERROR_MESSAGE = 'Invalid OperationID.';
@@ -14,10 +14,10 @@ export default (input, { methodName }, { path, documentInventory }) => {
1414
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
1515

1616
if (
17-
isLegacyCustomMethod(input) ||
17+
hasCustomMethodOverride(input) ||
1818
isCustomMethodIdentifier(resourcePath) ||
19-
isGetOverride(input) ||
20-
(invalidListMethod(resourcePath, resourcePaths) && !isListOverride(input))
19+
hasMethodVerbOverride(input, 'get') ||
20+
(invalidListMethod(resourcePath, resourcePaths) && !hasMethodVerbOverride(input, methodName))
2121
) {
2222
return;
2323
}

tools/spectral/ipa/rulesets/functions/IPA106ValidOperationID.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js';
33
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
5-
import { isLegacyCustomMethod } from './utils/extensions.js';
5+
import { hasCustomMethodOverride } from './utils/extensions.js';
66

77
const RULE_NAME = 'xgen-IPA-106-valid-operation-id';
88
const ERROR_MESSAGE = 'Invalid OperationID.';
99

1010
export default (input, { methodName }, { path }) => {
1111
const resourcePath = path[1];
1212

13-
if (isLegacyCustomMethod(input) || isCustomMethodIdentifier(resourcePath)) {
13+
if (hasCustomMethodOverride(input) || isCustomMethodIdentifier(resourcePath)) {
1414
return;
1515
}
1616

tools/spectral/ipa/rulesets/functions/IPA107ValidOperationID.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js';
33
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
5-
import { isLegacyCustomMethod } from './utils/extensions.js';
5+
import { hasCustomMethodOverride } from './utils/extensions.js';
66

77
const RULE_NAME = 'xgen-IPA-107-valid-operation-id';
88
const ERROR_MESSAGE = 'Invalid OperationID.';
99

1010
export default (input, { methodName }, { path }) => {
1111
const resourcePath = path[1];
1212

13-
if (isCustomMethodIdentifier(resourcePath) || isLegacyCustomMethod(input)) {
13+
if (isCustomMethodIdentifier(resourcePath) || hasCustomMethodOverride(input)) {
1414
return;
1515
}
1616

tools/spectral/ipa/rulesets/functions/IPA108ValidOperationID.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js';
33
import { isCustomMethodIdentifier, isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
5-
import { isLegacyCustomMethod } from './utils/extensions.js';
5+
import { hasCustomMethodOverride } from './utils/extensions.js';
66

77
const RULE_NAME = 'xgen-IPA-108-valid-operation-id';
88
const ERROR_MESSAGE = 'Invalid OperationID.';
99

1010
export default (input, { methodName }, { path }) => {
1111
const resourcePath = path[1];
1212

13-
if (isCustomMethodIdentifier(resourcePath) || isLegacyCustomMethod(input)) {
13+
if (isCustomMethodIdentifier(resourcePath) || hasCustomMethodOverride(input)) {
1414
return;
1515
}
1616

tools/spectral/ipa/rulesets/functions/IPA109ValidOperationID.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js';
33
import { isCustomMethodIdentifier, getCustomMethodName, stripCustomMethodName } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
5-
import { hasMethodWithVerbOverride, isLegacyCustomMethod } from './utils/extensions.js';
5+
import { hasMethodWithVerbOverride, hasCustomMethodOverride } from './utils/extensions.js';
66

77
const RULE_NAME = 'xgen-IPA-109-valid-operation-id';
88
const ERROR_MESSAGE = 'Invalid OperationID.';
@@ -48,7 +48,7 @@ export default (input, _, { path }) => {
4848
for (let i = 0; i < methods.length; i++) {
4949
let obj = input[methods[i]];
5050
const operationId = obj.operationId;
51-
if (isLegacyCustomMethod(obj)) {
51+
if (hasCustomMethodOverride(obj)) {
5252
expectedOperationID = generateOperationID(obj['x-xgen-method-verb-override'].verb, resourcePath);
5353
if (operationId !== expectedOperationID) {
5454
errors.push({
Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,57 @@
11
export const VERB_OVERRIDE_EXTENSION = 'x-xgen-method-verb-override';
22

3-
// for endpoint
3+
/**
4+
* Checks if the endpoint has a method with an extension "x-xgen-method-verb-override"
5+
*
6+
* @param object the object to evaluate
7+
* @returns {boolean} true if the object has the extension, otherwise false
8+
*/
49
export function hasMethodWithVerbOverride(endpoint) {
510
const keys = Object.keys(endpoint);
611
for (let i = 0; i < keys.length; i++) {
7-
if (endpoint[keys[i]][VERB_OVERRIDE_EXTENSION]) {
12+
if (hasVerbOverride(endpoint[keys[i]])) {
813
return true;
914
}
1015
}
1116
return false;
1217
}
1318

14-
// for method
15-
export function hasVerbOverride(object) {
16-
if (!object[VERB_OVERRIDE_EXTENSION]) {
17-
return false;
18-
}
19-
return true;
20-
}
21-
22-
// for method
23-
export function isLegacyCustomMethod(object) {
19+
/**
20+
* Checks if the object has an extension "x-xgen-method-verb-override" with the customMethod boolean set to true
21+
*
22+
* @param object the object to evaluate
23+
* @returns {boolean} true if the object has an extension with customMethod=True, otherwise false
24+
*/
25+
export function hasCustomMethodOverride(object) {
2426
if (hasVerbOverride(object)) {
2527
return object[VERB_OVERRIDE_EXTENSION].customMethod;
2628
}
2729
return false;
2830
}
2931

30-
export function isGetOverride(object) {
32+
/**
33+
* Checks if the object has an extension "x-xgen-method-verb-override" with the verb set to a specific verb
34+
*
35+
* @param object the object to evaluate
36+
* @param verb the verb to inspect the extension for
37+
* @returns {boolean} true if the object has the extension with the given verb, otherwise false
38+
*/
39+
export function hasMethodVerbOverride(object, verb) {
3140
if (hasVerbOverride(object)) {
32-
return object[VERB_OVERRIDE_EXTENSION].verb === 'get';
41+
return object[VERB_OVERRIDE_EXTENSION].verb === verb;
3342
}
3443
return false;
3544
}
3645

37-
export function isListOverride(object) {
38-
if (hasVerbOverride(object)) {
39-
return object[VERB_OVERRIDE_EXTENSION].verb === 'list';
46+
/**
47+
* Checks if the object has an extension "x-xgen-method-verb-override"
48+
*
49+
* @param object the object to evaluate
50+
* @returns {boolean} true if the object has the extension, otherwise false
51+
*/
52+
function hasVerbOverride(object) {
53+
if (!object[VERB_OVERRIDE_EXTENSION]) {
54+
return false;
4055
}
41-
return false;
42-
}
56+
return true;
57+
}
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
import { isResourceCollectionIdentifier, isSingletonResource } from './resourceEvaluation';
22
import { isPathParam } from './resourceEvaluation';
33

4+
/**
5+
* Checks whether the get method at a given path is valid
6+
*
7+
* @param resourcePath the resource path to inspect
8+
* @param resourcePaths the resource paths generated by getResourcePathItems
9+
* @returns true if the resourcePath has an invalid list method, false otherwise
10+
*/
411
export function invalidGetMethod(resourcePath, resourcePaths) {
512
return (
6-
!isPathParam(resourcePath.split('/').pop()) &&
13+
!lastIdentifierIsPathParam(resourcePath) &&
714
!(isResourceCollectionIdentifier(resourcePath) && isSingletonResource(resourcePaths))
815
);
916
}
1017

18+
/**
19+
* Checks whether the list method at a given path is valid
20+
*
21+
* @param resourcePath the resource path to inspect
22+
* @param resourcePaths the resource paths generated by getResourcePathItems
23+
* @returns true if the resourcePath has an invalid list method, false otherwise
24+
*/
1125
export function invalidListMethod(resourcePath, resourcePaths) {
12-
return isPathParam(resourcePath.split('/').pop()) || isSingletonResource(resourcePaths);
26+
return lastIdentifierIsPathParam(resourcePath) || isSingletonResource(resourcePaths);
1327
}
28+
29+
function lastIdentifierIsPathParam(resourceIdentifier) {
30+
if (resourceIdentifier.includes('.')) {
31+
resourceIdentifier = resourceIdentifier.substring(0, resourceIdentifier.lastIndexOf('.'));
32+
}
33+
return isPathParam(resourceIdentifier.split('/').pop());
34+
}

0 commit comments

Comments
 (0)