Skip to content

Commit 9e8cb9f

Browse files
authored
fix(ipa): Update override logic to be more lenient (#857)
1 parent 9379954 commit 9e8cb9f

File tree

8 files changed

+48
-43
lines changed

8 files changed

+48
-43
lines changed

tools/spectral/ipa/__tests__/utils/validations/validateOperationIdAndReturnErrors.test.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ describe('tools/spectral/ipa/rulesets/functions/utils/validations/validateOperat
202202
['paths', '/some/{id}/resource/{resourceId}/long/{id}/childResource/{id}', 'get']
203203
)
204204
).toHaveLength(0);
205+
206+
// valid override on short opID
207+
expect(
208+
validateOperationIdAndReturnErrors(
209+
'get',
210+
'/some/{id}/resource/{resourceId}',
211+
{
212+
operationId: 'getSomeResource',
213+
'x-xgen-operation-id-override': 'getResource',
214+
},
215+
['paths', '/some/{id}/resource/{resourceId}', 'get']
216+
)
217+
).toHaveLength(0);
205218
});
206219

207220
it('should return errors for invalid operation ID', () => {
@@ -258,26 +271,6 @@ describe('tools/spectral/ipa/rulesets/functions/utils/validations/validateOperat
258271
]);
259272
});
260273

261-
it('should return errors for valid operation ID with unnecessary override', () => {
262-
expect(
263-
validateOperationIdAndReturnErrors(
264-
'get',
265-
'/some/{id}/resource/{resourceId}',
266-
{
267-
operationId: 'getSomeResource',
268-
'x-xgen-operation-id-override': 'getResource',
269-
},
270-
['paths', '/some/{id}/resource/{resourceId}', 'get']
271-
)
272-
).toEqual([
273-
{
274-
path: ['paths', '/some/{id}/resource/{resourceId}', 'get', 'x-xgen-operation-id-override'],
275-
message:
276-
"Please remove the 'x-xgen-operation-id-override' extension from the operation. The Operation ID already has a valid length (<=4 words).",
277-
},
278-
]);
279-
});
280-
281274
it('should return errors for operation ID override with wrong verb', () => {
282275
expect(
283276
validateOperationIdAndReturnErrors(

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from './utils/collectionUtils.js';
77
import { hasException } from './utils/exceptions.js';
88
import { getResourcePathItems, isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
9-
import { hasCustomMethodOverride, hasMethodVerbOverride } from './utils/extensions.js';
9+
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
1010
import { isInvalidGetMethod } from './utils/methodLogic.js';
1111
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';
1212

@@ -32,6 +32,9 @@ export default (input, { methodName }, { path, documentInventory }) => {
3232
}
3333

3434
try {
35+
if (hasMethodVerbOverride(input, methodName)) {
36+
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
37+
}
3538
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);
3639

3740
if (errors.length > 0) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from './utils/collectionUtils.js';
88
import { getResourcePathItems, isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
99
import { isInvalidListMethod } from './utils/methodLogic.js';
10-
import { hasCustomMethodOverride, hasMethodVerbOverride } from './utils/extensions.js';
10+
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
1111
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';
1212

1313
const RULE_NAME = 'xgen-IPA-105-valid-operation-id';
@@ -31,6 +31,10 @@ export default (input, { methodName }, { path, documentInventory }) => {
3131
return;
3232
}
3333

34+
if (hasMethodVerbOverride(input, methodName)) {
35+
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
36+
}
37+
3438
try {
3539
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);
3640

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
handleInternalError,
77
} from './utils/collectionUtils.js';
88
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
9-
import { hasCustomMethodOverride } from './utils/extensions.js';
9+
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
1010
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';
1111

1212
const RULE_NAME = 'xgen-IPA-106-valid-operation-id';
@@ -23,6 +23,10 @@ export default (input, { methodName }, { path }) => {
2323
return;
2424
}
2525

26+
if (hasMethodVerbOverride(input, methodName)) {
27+
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
28+
}
29+
2630
try {
2731
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);
2832

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
handleInternalError,
77
} from './utils/collectionUtils.js';
88
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
9-
import { hasCustomMethodOverride } from './utils/extensions.js';
9+
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
1010
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';
1111

1212
const RULE_NAME = 'xgen-IPA-107-valid-operation-id';
@@ -23,6 +23,10 @@ export default (input, { methodName }, { path }) => {
2323
return;
2424
}
2525

26+
if (hasMethodVerbOverride(input, methodName)) {
27+
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
28+
}
29+
2630
try {
2731
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);
2832

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
handleInternalError,
77
} from './utils/collectionUtils.js';
88
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
9-
import { hasCustomMethodOverride } from './utils/extensions.js';
9+
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
1010
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';
1111

1212
const RULE_NAME = 'xgen-IPA-108-valid-operation-id';
@@ -23,6 +23,10 @@ export default (input, { methodName }, { path }) => {
2323
return;
2424
}
2525

26+
if (hasMethodVerbOverride(input, methodName)) {
27+
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
28+
}
29+
2630
try {
2731
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);
2832

tools/spectral/ipa/rulesets/functions/utils/extensions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function hasCustomMethodOverride(object) {
1919
* @returns {boolean} true if the object has the extension with the given verb, otherwise false
2020
*/
2121
export function hasMethodVerbOverride(object, verb) {
22-
return hasVerbOverride(object) && object[VERB_OVERRIDE_EXTENSION].verb === verb;
22+
return hasVerbOverride(object) && object[VERB_OVERRIDE_EXTENSION].verb.startsWith(verb);
2323
}
2424

2525
/**

tools/spectral/ipa/rulesets/functions/utils/validations/validateOperationIdAndReturnErrors.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ const TOO_LONG_OP_ID_ERROR_MESSAGE =
88
"The Operation ID is longer than 4 words. Please add an '" +
99
OPERATION_ID_OVERRIDE_EXTENSION +
1010
"' extension to the operation with a shorter operation ID.";
11-
const REMOVE_OP_ID_OVERRIDE_ERROR_MESSAGE =
12-
"Please remove the '" +
13-
OPERATION_ID_OVERRIDE_EXTENSION +
14-
"' extension from the operation. The Operation ID already has a valid length (<=4 words).";
1511

1612
/**
1713
* Validates the operationId of an operation object and returns errors if it does not match the expected format. Also validates that the operationId override, if present, follows the expected rules.
@@ -37,26 +33,23 @@ export function validateOperationIdAndReturnErrors(methodName, resourcePath, ope
3733
}
3834

3935
const operationIdOverridePath = path.concat([OPERATION_ID_OVERRIDE_EXTENSION]);
40-
if (numberOfWords(operationId) > 4) {
41-
if (!hasOperationIdOverride(operationObject)) {
42-
errors.push({
43-
path: operationIdPath,
44-
message: TOO_LONG_OP_ID_ERROR_MESSAGE + " For example: '" + shortenOperationId(expectedOperationId) + "'.",
45-
});
46-
return errors;
47-
}
36+
if (numberOfWords(operationId) > 4 && !hasOperationIdOverride(operationObject)) {
37+
errors.push({
38+
path: operationIdPath,
39+
message: TOO_LONG_OP_ID_ERROR_MESSAGE + " For example: '" + shortenOperationId(expectedOperationId) + "'.",
40+
});
41+
return errors;
42+
}
43+
44+
if (hasOperationIdOverride(operationObject)) {
4845
const overrideErrors = validateOperationIdOverride(
4946
operationIdOverridePath,
5047
getOperationIdOverride(operationObject),
5148
expectedOperationId
5249
);
5350
errors.push(...overrideErrors);
54-
} else if (hasOperationIdOverride(operationObject)) {
55-
errors.push({
56-
path: operationIdOverridePath,
57-
message: REMOVE_OP_ID_OVERRIDE_ERROR_MESSAGE,
58-
});
5951
}
52+
6053
return errors;
6154
}
6255

0 commit comments

Comments
 (0)