Skip to content

Commit abbf2b1

Browse files
author
Sophia Marie Terry
committed
Update opID override error case logic and tests
1 parent 09cc5a6 commit abbf2b1

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
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/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)