-
Couldn't load subscription status.
- Fork 14
CLOUDP-304929: xgen-IPA-102-collection-identifier-camelCase #499
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 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
60308e4
CLOUDP-304929: camel case
wtrocki a9152e2
fix: reuse method
wtrocki d4ce211
fix: remove function
wtrocki 7751d68
fix: review comments
wtrocki f36dce5
fix: example description
wtrocki c4f3b26
Apply suggestions from Lovisa code review
wtrocki 7459c18
fix: extra unit tests and dedicated handling for slashes in input
wtrocki 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
136 changes: 136 additions & 0 deletions
136
tools/spectral/ipa/__tests__/collectionIdentifierCamelCase.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,136 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-102-collection-identifier-camelCase', [ | ||
| { | ||
| name: 'valid camelCase identifiers', | ||
| document: { | ||
| paths: { | ||
| '/api/v2/atlas/test': {}, | ||
| '/users': {}, | ||
| '/resourceGroups': {}, | ||
| '/userProfiles': {}, | ||
| '/api/v1/test': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'valid camelCase with path parameters', | ||
| document: { | ||
| paths: { | ||
| '/resourceGroups/{groupId}': {}, | ||
| '/users/{userId}/userProfiles': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'valid paths with custom methods (only checking identifier part)', | ||
| document: { | ||
| paths: { | ||
| '/resources:any_Custom_Method': {}, | ||
| '/users/{userId}/data/:AnyOtherMethod': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid PascalCase instead of camelCase', | ||
| document: { | ||
| paths: { | ||
| '/Resources': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-102-collection-identifier-camelCase', | ||
| message: | ||
| "Collection identifiers must be in camelCase. Path segment 'Resources' in path '/Resources' is not in camelCase. http://go/ipa/102", | ||
| path: ['paths', '/Resources'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid with snake_case instead of camelCase', | ||
| document: { | ||
| paths: { | ||
| '/resource_groups': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-102-collection-identifier-camelCase', | ||
| message: | ||
| "Collection identifiers must be in camelCase. Path segment 'resource_groups' in path '/resource_groups' is not in camelCase. http://go/ipa/102", | ||
| path: ['paths', '/resource_groups'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid with kebab-case instead of camelCase', | ||
| document: { | ||
| paths: { | ||
| '/resource-groups': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-102-collection-identifier-camelCase', | ||
| message: | ||
| "Collection identifiers must be in camelCase. Path segment 'resource-groups' in path '/resource-groups' is not in camelCase. http://go/ipa/102", | ||
| path: ['paths', '/resource-groups'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid resource path with invalid casing but valid custom method', | ||
| document: { | ||
| paths: { | ||
| '/Resources:createResource': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-102-collection-identifier-camelCase', | ||
| message: | ||
| "Collection identifiers must be in camelCase. Path segment 'Resources' in path '/Resources:createResource' is not in camelCase. http://go/ipa/102", | ||
| path: ['paths', '/Resources:createResource'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid with consecutive uppercase letters', | ||
| document: { | ||
| paths: { | ||
| '/resourcesAPI': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-102-collection-identifier-camelCase', | ||
| message: | ||
| "Collection identifiers must be in camelCase. Path segment 'resourcesAPI' in path '/resourcesAPI' is not in camelCase. http://go/ipa/102", | ||
| path: ['paths', '/resourcesAPI'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'valid with path-level exception', | ||
| document: { | ||
| paths: { | ||
| '/resource_groups': { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-102-collection-identifier-camelCase': 'Legacy API path that cannot be changed', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| ]); | ||
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
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
100 changes: 100 additions & 0 deletions
100
tools/spectral/ipa/rulesets/functions/collectionIdentifierCamelCase.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,100 @@ | ||
| import { | ||
| collectAdoption, | ||
| collectAndReturnViolation, | ||
| collectException, | ||
| handleInternalError, | ||
| } from './utils/collectionUtils.js'; | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { isPathParam } from './utils/componentUtils.js'; | ||
| import { casing } from '@stoplight/spectral-functions'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-102-collection-identifier-camelCase'; | ||
| const ERROR_MESSAGE = 'Collection identifiers must be in camelCase.'; | ||
|
|
||
| /** | ||
| * Checks if collection identifiers in paths follow camelCase convention | ||
| * | ||
| * @param {object} input - The path key from the OpenAPI spec | ||
| * @param {object} options - Rule configuration options | ||
| * @param {object} context - The context object containing the path and documentInventory | ||
| */ | ||
| export default (input, options, { path, documentInventory }) => { | ||
| const oas = documentInventory.resolved; | ||
| const pathKey = input; | ||
|
|
||
| // Check for exception at the path level | ||
| if (hasException(oas.paths[input], RULE_NAME)) { | ||
| collectException(oas.paths[input], RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| // Extract ignored values from options | ||
| const ignoredValues = options?.ignoredValues || []; | ||
|
|
||
| const violations = checkViolations(pathKey, path, ignoredValues); | ||
| if (violations.length > 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, violations); | ||
| } | ||
|
|
||
| return collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| function checkViolations(pathKey, path, ignoredValues = []) { | ||
| const violations = []; | ||
| try { | ||
| const pathSegments = pathKey.split('/'); | ||
|
|
||
| pathSegments.forEach((segment) => { | ||
| // Skip path parameter validation if it matches the expected format | ||
| if (isPathParam(segment)) { | ||
| // Extract parameter name without brackets | ||
| const paramName = segment.slice(1, segment.indexOf('}')); | ||
| // Check if it's a valid camelCase parameter name | ||
| if (casing(paramName, { type: 'camel', disallowDigits: true })) { | ||
| violations.push({ | ||
| message: `${ERROR_MESSAGE} Path parameter '${paramName}' in path '${pathKey}' is not in camelCase.`, | ||
| path: [...path, pathKey], | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Skip validation for ignored values | ||
| if (ignoredValues.includes(segment)) { | ||
| return; | ||
| } | ||
|
|
||
| // For regular path segments, check if they contain custom method indicators | ||
| // If they do, only validate the identifier part (before the colon) | ||
| const colonIndex = segment.indexOf(':'); | ||
| let identifier = segment; | ||
|
|
||
| if (colonIndex !== -1) { | ||
| // Only check the identifier part before the colon | ||
| identifier = segment.substring(0, colonIndex); | ||
| } | ||
|
|
||
| // Skip empty identifiers | ||
| if (identifier.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip validation for ignored values at the identifier level too | ||
| if (ignoredValues.includes(identifier)) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if it's in camelCase using the casing function | ||
| if (casing(identifier, { type: 'camel', disallowDigits: true })) { | ||
| violations.push({ | ||
| message: `${ERROR_MESSAGE} Path segment '${identifier}' in path '${pathKey}' is not in camelCase.`, | ||
| path: [...path, pathKey], | ||
| }); | ||
| } | ||
| }); | ||
| } catch (e) { | ||
| handleInternalError(RULE_NAME, [...path, pathKey], e); | ||
| } | ||
|
|
||
| return violations; | ||
| } |
11 changes: 11 additions & 0 deletions
11
tools/spectral/ipa/rulesets/functions/utils/schemaUtils.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
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.
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.
Q: If the path is for example
/resource_groups/{id}/resource_keys, will there be two errors, one forresource_groupsand one forresource_keys?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.
Single violation with 2 errors.
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.
Cool, could you add a test case for it?
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.
Extended multiple test cases.