Skip to content

Commit 50fa6db

Browse files
author
Sophia Marie Terry
committed
CLOUDP-328959: Added early returns and removed reference to custom methods
1 parent fcef3e4 commit 50fa6db

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js';
3-
import { isCustomMethodIdentifier, getCustomMethodName, stripCustomMethodName } from './utils/resourceEvaluation.js';
3+
import { isCustomMethodIdentifier, isResourceCollectionIdentifier, isSingletonResource, getResourcePathItems } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
55

66
const RULE_NAME = 'xgen-IPA-106-valid-operation-id';
77
const ERROR_MESSAGE =
88
'Invalid OperationID. The Operation ID must start with the verb “create” and should be followed by a noun or compound noun. The noun(s) in the Operation ID should be the collection identifiers from the resource identifier in singular form.';
99

10-
export default (input, _, { path }) => {
11-
let resourcePath = path[1];
12-
let methodName = 'create';
10+
export default (input, _, { path, documentInventory }) => {
11+
const resourcePath = path[1];
12+
const oas = documentInventory.resolved;
13+
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
14+
const methodName = 'create';
15+
16+
const isResourceCollection = isResourceCollectionIdentifier(resourcePath) && !isSingletonResource(resourcePaths);
17+
if (isCustomMethodIdentifier(resourcePath) || !isResourceCollection) {
18+
return;
19+
}
1320

1421
if (hasException(input, RULE_NAME)) {
1522
collectException(input, RULE_NAME, path);
@@ -18,11 +25,6 @@ export default (input, _, { path }) => {
1825

1926
// TODO detect custom method extension - CLOUDP-306294
2027

21-
if (isCustomMethodIdentifier(resourcePath)) {
22-
methodName = getCustomMethodName(resourcePath);
23-
resourcePath = stripCustomMethodName(resourcePath);
24-
}
25-
2628
const expectedOperationID = generateOperationID(methodName, resourcePath);
2729
if (expectedOperationID !== input.operationId) {
2830
const errors = [

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js';
3-
import { isCustomMethodIdentifier, getCustomMethodName, stripCustomMethodName } from './utils/resourceEvaluation.js';
3+
import { isSingleResourceIdentifier, isResourceCollectionIdentifier, isSingletonResource, getResourcePathItems } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
55

66
const RULE_NAME = 'xgen-IPA-107-valid-operation-id';
77
const ERROR_MESSAGE =
88
'Invalid OperationID. The Operation ID must start with the verb “update” and should be followed by a noun or compound noun. The noun(s) in the Operation ID should be the collection identifiers from the resource identifier in singular form. For singleton resources - the last noun may be in plural form.';
99

10-
export default (input, _, { path }) => {
11-
let resourcePath = path[1];
12-
let methodName = 'update';
10+
export default (input, _, { path, documentInventory }) => {
11+
const resourcePath = path[1];
12+
const oas = documentInventory.resolved;
13+
const resourcePaths = getResourcePathItems(resourcePath, oas.paths);
14+
const methodName = 'update';
15+
16+
if (
17+
!isSingleResourceIdentifier(resourcePath) &&
18+
!(isResourceCollectionIdentifier(resourcePath) && isSingletonResource(resourcePaths))
19+
) {
20+
return;
21+
}
1322

1423
if (hasException(input, RULE_NAME)) {
1524
collectException(input, RULE_NAME, path);
@@ -18,11 +27,6 @@ export default (input, _, { path }) => {
1827

1928
// TODO detect custom method extension - CLOUDP-306294
2029

21-
if (isCustomMethodIdentifier(resourcePath)) {
22-
methodName = getCustomMethodName(resourcePath);
23-
resourcePath = stripCustomMethodName(resourcePath);
24-
}
25-
2630
const expectedOperationID = generateOperationID(methodName, resourcePath);
2731
if (expectedOperationID !== input.operationId) {
2832
const errors = [

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js';
3-
import { isCustomMethodIdentifier, getCustomMethodName, stripCustomMethodName } from './utils/resourceEvaluation.js';
3+
import { isSingleResourceIdentifier } from './utils/resourceEvaluation.js';
44
import { generateOperationID } from './utils/operationIdGeneration.js';
55

66
const RULE_NAME = 'xgen-IPA-108-valid-operation-id';
77
const ERROR_MESSAGE =
88
'Invalid OperationID. The Operation ID must start with the verb “delete” and should be followed by a noun or compound noun. The noun(s) in the Operation ID should be the collection identifiers from the resource identifier in singular form.';
99

10-
export default (input, _, { path }) => {
11-
let resourcePath = path[1];
12-
let methodName = 'delete';
10+
export default (input, _, { path, }) => {
11+
const resourcePath = path[1];
12+
const methodName = 'delete';
13+
14+
if (!isSingleResourceIdentifier(resourcePath)) {
15+
return;
16+
}
1317

1418
if (hasException(input, RULE_NAME)) {
1519
collectException(input, RULE_NAME, path);
@@ -18,11 +22,6 @@ export default (input, _, { path }) => {
1822

1923
// TODO detect custom method extension - CLOUDP-306294
2024

21-
if (isCustomMethodIdentifier(resourcePath)) {
22-
methodName = getCustomMethodName(resourcePath);
23-
resourcePath = stripCustomMethodName(resourcePath);
24-
}
25-
2625
const expectedOperationID = generateOperationID(methodName, resourcePath);
2726
if (expectedOperationID !== input.operationId) {
2827
const errors = [

0 commit comments

Comments
 (0)