-
Notifications
You must be signed in to change notification settings - Fork 98
feat(ibm-operationid-naming-convention): extend operationid naming check #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
41ad3ce
22127c6
8009b75
4a8a675
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||||||||||||
|
|
@@ -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'], | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
|
|
@@ -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 | ||||||||||||||||
| const pluralVerbs = ['list', 'replace', 'set', 'delete', 'remove', 'unset']; | ||||||||||||||||
|
|
||||||||||||||||
| switch (httpMethod) { | ||||||||||||||||
| case 'get': | ||||||||||||||||
| if (isResourceOriented) { | ||||||||||||||||
|
|
@@ -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 | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| 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)); | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| 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 }; | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| else correctIds.push(correctId); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return { checkPassed: false, correctIds, operationId }; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return { checkPassed: true }; | ||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.