Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 12 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
"micromatch": "^4.0.8",
"jsonpath-plus": "^10.3.0",
"rollup": "2.79.2"
},
"dependencies": {
"inflected": "^2.1.0"
}
}
62 changes: 46 additions & 16 deletions packages/ruleset/src/functions/operationid-naming-convention.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const { each, merge, pickBy, reduce } = require('lodash');
const { operationMethods } = require('../utils');
const inflected = require('inflected');

module.exports = function (rootDocument) {
return operationIdNamingConvention(rootDocument);
Expand Down Expand Up @@ -58,19 +59,21 @@ function operationIdNamingConvention(resolvedSpec) {
p.startsWith(op.pathKey + '/{')
);

const { checkPassed, verbs } = operationIdPassedConventionCheck(
isResourceOriented,
op['opKey'],
op.operationId,
pathEndsWithParam,
numParamRefs
);
const { checkPassed, correctIds, operationId } =
operationIdPassedConventionCheck(
isResourceOriented,
op['opKey'],
op.operationId,
pathEndsWithParam,
numParamRefs,
op.pathKey
);

if (checkPassed === false) {
errors.push({
message: `operationIds should follow naming convention: operationId verb should be ${verbs.join(
message: `operationIds should follow naming convention: operationId should be ${correctIds.join(
' or '
)}`,
)} but it's ${operationId} instead`,
path: [...op.path, 'operationId'],
});
}
Expand Down Expand Up @@ -127,20 +130,25 @@ function operationIdNamingConvention(resolvedSpec) {
* @param {string} operationId the operation's operationId
* @param {boolean} pathEndsWithParam a flag that indicates whether or not the path ends with a path parameter reference
* @param {number} numParamRefs the number of path parameter references in the path
* @param {string} fullPath the full path of the operation
* @returns
*/
function operationIdPassedConventionCheck(
isResourceOriented,
httpMethod,
operationId,
pathEndsWithParam,
numParamRefs
numParamRefs,
fullPath
) {
// Useful for debugging.
// console.log(`Debug: ${httpMethod} ${isResourceOriented} ${pathEndsWithParam} ${numParamRefs} ${operationId}`);

const verbs = [];

// Verbs where pluralization can happen in the operationId based on the path

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// Verbs where pluralization can happen in the operationId based on the path
// Verbs where pluralization can happen in the operationId based on the path.

const pluralVerbs = ['list', 'replace', 'set', 'delete', 'remove', 'unset'];

switch (httpMethod) {
case 'get':
if (isResourceOriented) {
Expand Down Expand Up @@ -188,13 +196,35 @@ function operationIdPassedConventionCheck(
break;
}

// If we have a non-empty list of acceptable verbs, then make sure
// that the operationId starts with one of them.
// If we have an acceptable verb, then make sure
// that the operationId starts with that verb
// and that the rest of the operation id matches the path according to the naming conventions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// If we have an acceptable verb, then make sure
// that the operationId starts with that verb
// and that the rest of the operation id matches the path according to the naming conventions
// If we have an acceptable verb, then make sure
// that the operationId starts with that verb
// and that the rest of the operation id matches
// the path according to the naming conventions.

if (verbs.length > 0) {
const checkPassed = verbs
.map(verb => operationId.startsWith(verb))
.some(v => v);
return { checkPassed, verbs };
const convertedPath = fullPath
.replace(/^\/+/, '')
.split('/')
.filter(part => !/^\{.*\}$/.test(part));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
.filter(part => !/^\{.*\}$/.test(part));
.filter(part => !part.startsWith('{') && !part.endsWith('}'));


//singularize the words in the path according to the naming conventions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
//singularize the words in the path according to the naming conventions
// Singularize the words in the path according to the naming conventions.

for (let i = 0; i < convertedPath.length; i++) {
if (
i !== convertedPath.length - 1 ||
!pluralVerbs.some(verb => verbs.includes(verb)) ||
pathEndsWithParam
)
convertedPath[i] = inflected.singularize(convertedPath[i]);
}

const correctIds = [];

for (let i = 0; i < verbs.length; i++) {
const correctId = verbs[i] + '_' + convertedPath.join('_');
if (correctId === operationId)
return { checkPassed: true, correctId, operationId };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
return { checkPassed: true, correctId, operationId };
return { checkPassed: true };

else correctIds.push(correctId);
}

return { checkPassed: false, correctIds, operationId };
}

return { checkPassed: true };
Expand Down
Loading