-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-328283: Implemented Operation ID Generation utility #801
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a45b72b
CLOUDP-328283: Initial implementation of OperationID generation
d1f7979
CLOUDP-328283: Debug for legacy custom method use case
b492674
CLOUDP-328283: Added tests for Operation ID generation
28375bd
CLOUDP-328283: Switched to ember-inflector package, enabled new tests
2e80644
CLOUDP-328283: revert change to jest transformation config
0ccb325
CLOUDP-328283: Fixed dependency conflict
a902bff
Merge branch 'main' into CLOUDP-328283
sphterry 7852f59
CLOUDP-328283: Prettier
acfac94
CLOUDP-328283: Apply suggestions from code review
sphterry 053d70f
CLOUDP-328283: Amend deriveActionVerb, remove casing checks
41d56b4
CLOUDP-328283: Add new tests, debug for multiple word method names
a4b9e96
Merge branch 'main' into CLOUDP-328283
sphterry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| { | ||
| "presets": ["@babel/preset-env"], | ||
| "plugins": [ | ||
| "babel-plugin-transform-import-meta"] | ||
| "babel-plugin-transform-import-meta", | ||
| "@babel/plugin-transform-modules-commonjs"] | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tools/spectral/ipa/__tests__/utils/operationIdGeneration.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { describe, expect, it } from '@jest/globals'; | ||
| import { generateOperationID } from '../../rulesets/functions/utils/operationIdGeneration'; | ||
|
|
||
| describe('tools/spectral/ipa/utils/operationIdGeneration.js', () => { | ||
| it('should singularize all nouns', () => { | ||
| expect(generateOperationID('create', '/groups/{groupId}/clusters')).toEqual('createGroupCluster'); | ||
| expect(generateOperationID('delete', '/groups/{groupId}/clusters/{clusterName}')).toEqual('deleteGroupCluster'); | ||
| expect(generateOperationID('get', '/groups/{groupId}/clusters/{clusterName}')).toEqual('getGroupCluster'); | ||
| expect(generateOperationID('update', '/groups/{groupId}/clusters/{clusterName}')).toEqual('updateGroupCluster'); | ||
| expect(generateOperationID('pause', '/groups/{groupId}/clusters/{clusterName}')).toEqual('pauseGroupCluster'); | ||
| }); | ||
|
|
||
| it('should leave the final noun as is', () => { | ||
| expect(generateOperationID('list', '/groups/{groupId}/clusters')).toEqual('listGroupClusters'); | ||
| expect(generateOperationID('get', '/groups/{groupId}/settings')).toEqual('getGroupSettings'); | ||
| expect(generateOperationID('update', '/groups/{groupId}/settings')).toEqual('updateGroupSettings'); | ||
| expect(generateOperationID('search', '/groups/{groupId}/clusters')).toEqual('searchGroupClusters'); | ||
| expect( | ||
| generateOperationID( | ||
| 'get', | ||
| '/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements' | ||
| ) | ||
| ).toEqual('getGroupClusterCollStatMeasurements'); | ||
| expect(generateOperationID('grant', '/api/atlas/v2/groups/{groupId}/access')).toEqual('grantGroupAccess'); | ||
| }); | ||
|
|
||
| it('should split camelCase method names', () => { | ||
| expect(generateOperationID('addNode', '/groups/{groupId}/clusters/{clusterName}')).toEqual('addGroupClusterNode'); | ||
| }); | ||
|
|
||
| it('should accomodate legacy custom methods', () => { | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries')).toEqual( | ||
| 'restartGroupClusterPrimaries' | ||
| ); | ||
| }); | ||
| }); | ||
62 changes: 62 additions & 0 deletions
62
tools/spectral/ipa/rulesets/functions/utils/operationIdGeneration.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { singularize } from 'ember-inflector'; | ||
| import { isPathParam, removePrefix, isSingleResourceIdentifier } from './resourceEvaluation.js'; | ||
| import { casing } from '@stoplight/spectral-functions'; | ||
|
|
||
| /** | ||
| * Returns IPA Compliant Operation ID. | ||
| * | ||
| * @param method the standard method name (create, update, get etc.), custom method name, or empty string (only for legacy custom methods) | ||
| * @param path the path for the endpoint | ||
| */ | ||
| export function generateOperationID(method, path) { | ||
| let resourceIdentifier = removePrefix(path); | ||
| let nouns = resourceIdentifier | ||
| .split('/') | ||
| .filter((id) => id.length > 0 && !isPathParam(id)) | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .map((noun) => capitalize(noun)); | ||
|
|
||
| // legacy custom method - use end of path as custom method name | ||
| if (method === '') { | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| method = path.split('/').pop(); | ||
| nouns.pop(); | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| let verb = deriveActionVerb(method); | ||
|
|
||
| // if custom method name is multiple words, add trailing nouns to the operation ID | ||
| if (!casing(method, { type: 'camel' }) && method.length > verb.length) { | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| nouns.push(method.slice(verb.length)); | ||
| } | ||
|
|
||
| let opID = verb; | ||
| for (let i = 0; i < nouns.length - 1; i++) { | ||
| opID += singularize(nouns[i]); | ||
| } | ||
|
|
||
| // singularize final noun, dependent on resource identifier | ||
| if (isSingleResourceIdentifier(resourceIdentifier) || verb == 'create') { | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| nouns[nouns.length - 1] = singularize(nouns[nouns.length - 1]); | ||
| } | ||
|
|
||
| opID += nouns.pop(); | ||
|
|
||
| return opID; | ||
| } | ||
|
|
||
| /** | ||
| * Derives action verb from custom method name. Returns standard method names as is. | ||
| * | ||
| * @param method the custom method name | ||
| */ | ||
| function deriveActionVerb(method) { | ||
| // custom method name is camelCase return first word (assumed verb) | ||
| if (!casing(method, { type: 'camel' })) { | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return method.match(/[A-Z]?[a-z]+/g)[0]; | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return method; | ||
| } | ||
|
|
||
| function capitalize(val) { | ||
| return String(val).charAt(0).toUpperCase() + String(val).slice(1); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.