Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ describe('tools/spectral/ipa/rulesets/functions/utils/validations/validateOperat
['paths', '/some/{id}/resource/{resourceId}/long/{id}/childResource/{id}', 'get']
)
).toHaveLength(0);

// valid override on short opID
expect(
validateOperationIdAndReturnErrors(
'get',
'/some/{id}/resource/{resourceId}',
{
operationId: 'getSomeResource',
'x-xgen-operation-id-override': 'getResource',
},
['paths', '/some/{id}/resource/{resourceId}', 'get']
)
).toHaveLength(0);
});

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

it('should return errors for valid operation ID with unnecessary override', () => {
expect(
validateOperationIdAndReturnErrors(
'get',
'/some/{id}/resource/{resourceId}',
{
operationId: 'getSomeResource',
'x-xgen-operation-id-override': 'getResource',
},
['paths', '/some/{id}/resource/{resourceId}', 'get']
)
).toEqual([
{
path: ['paths', '/some/{id}/resource/{resourceId}', 'get', 'x-xgen-operation-id-override'],
message:
"Please remove the 'x-xgen-operation-id-override' extension from the operation. The Operation ID already has a valid length (<=4 words).",
},
]);
});

it('should return errors for operation ID override with wrong verb', () => {
expect(
validateOperationIdAndReturnErrors(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from './utils/collectionUtils.js';
import { hasException } from './utils/exceptions.js';
import { getResourcePathItems, isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
import { hasCustomMethodOverride, hasMethodVerbOverride } from './utils/extensions.js';
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
import { isInvalidGetMethod } from './utils/methodLogic.js';
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';

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

try {
if (hasMethodVerbOverride(input, methodName)) {
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
}
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);

if (errors.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from './utils/collectionUtils.js';
import { getResourcePathItems, isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
import { isInvalidListMethod } from './utils/methodLogic.js';
import { hasCustomMethodOverride, hasMethodVerbOverride } from './utils/extensions.js';
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';

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

if (hasMethodVerbOverride(input, methodName)) {
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
}

try {
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
handleInternalError,
} from './utils/collectionUtils.js';
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
import { hasCustomMethodOverride } from './utils/extensions.js';
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';

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

if (hasMethodVerbOverride(input, methodName)) {
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
}

try {
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
handleInternalError,
} from './utils/collectionUtils.js';
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
import { hasCustomMethodOverride } from './utils/extensions.js';
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';

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

if (hasMethodVerbOverride(input, methodName)) {
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
}

try {
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
handleInternalError,
} from './utils/collectionUtils.js';
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
import { hasCustomMethodOverride } from './utils/extensions.js';
import { hasCustomMethodOverride, hasMethodVerbOverride, VERB_OVERRIDE_EXTENSION } from './utils/extensions.js';
import { validateOperationIdAndReturnErrors } from './utils/validations/validateOperationIdAndReturnErrors.js';

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

if (hasMethodVerbOverride(input, methodName)) {
methodName = input[VERB_OVERRIDE_EXTENSION].verb;
}

try {
const errors = validateOperationIdAndReturnErrors(methodName, resourcePath, input, path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function hasCustomMethodOverride(object) {
* @returns {boolean} true if the object has the extension with the given verb, otherwise false
*/
export function hasMethodVerbOverride(object, verb) {
return hasVerbOverride(object) && object[VERB_OVERRIDE_EXTENSION].verb === verb;
return hasVerbOverride(object) && object[VERB_OVERRIDE_EXTENSION].verb.startsWith(verb);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nit] Can you update the description of the function as well?

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ const TOO_LONG_OP_ID_ERROR_MESSAGE =
"The Operation ID is longer than 4 words. Please add an '" +
OPERATION_ID_OVERRIDE_EXTENSION +
"' extension to the operation with a shorter operation ID.";
const REMOVE_OP_ID_OVERRIDE_ERROR_MESSAGE =
"Please remove the '" +
OPERATION_ID_OVERRIDE_EXTENSION +
"' extension from the operation. The Operation ID already has a valid length (<=4 words).";

/**
* 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.
Expand All @@ -37,26 +33,23 @@ export function validateOperationIdAndReturnErrors(methodName, resourcePath, ope
}

const operationIdOverridePath = path.concat([OPERATION_ID_OVERRIDE_EXTENSION]);
if (numberOfWords(operationId) > 4) {
if (!hasOperationIdOverride(operationObject)) {
errors.push({
path: operationIdPath,
message: TOO_LONG_OP_ID_ERROR_MESSAGE + " For example: '" + shortenOperationId(expectedOperationId) + "'.",
});
return errors;
}
if (numberOfWords(operationId) > 4 && !hasOperationIdOverride(operationObject)) {
errors.push({
path: operationIdPath,
message: TOO_LONG_OP_ID_ERROR_MESSAGE + " For example: '" + shortenOperationId(expectedOperationId) + "'.",
});
return errors;
}

if (hasOperationIdOverride(operationObject)) {
const overrideErrors = validateOperationIdOverride(
operationIdOverridePath,
getOperationIdOverride(operationObject),
expectedOperationId
);
errors.push(...overrideErrors);
} else if (hasOperationIdOverride(operationObject)) {
errors.push({
path: operationIdOverridePath,
message: REMOVE_OP_ID_OVERRIDE_ERROR_MESSAGE,
});
}

return errors;
}

Expand Down
Loading