-
Couldn't load subscription status.
- Fork 14
CLOUDP-304948: IPA 109: Custom Methods : The HTTP URI must use a colon(:) character followed by the custom method name #555
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
103 changes: 103 additions & 0 deletions
103
tools/spectral/ipa/__tests__/IPA109CustomMethodIdentifierFormat.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,103 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-109-custom-method-identifier-format', [ | ||
| { | ||
| name: 'valid custom method formats', | ||
| document: { | ||
| paths: { | ||
| '/resource:customMethod': {}, | ||
| '/resource/{resourceId}:customMethod': {}, | ||
| '/resource/{resourceId}/subresource:customMethod': {}, | ||
| '/resource/{resourceId}/subresource/{resourceId}:customMethod': {}, | ||
| '/resource/subresource/{resourceId}:customMethod': {}, | ||
| '/resource/{resourceId}/subresource/{resourceId}/nested:customMethod': {}, | ||
| '/resource/{resourceId}/subresource/{resourceId}/nested/{resourceId}:customMethod': {}, | ||
| '/resource/subresource/nested/{resourceId}:customMethod': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid custom method formats not validated by this rule', | ||
| document: { | ||
| paths: { | ||
| '/resources:custom&method': {}, | ||
| '/resources:custom/method': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid custom method with exception', | ||
| document: { | ||
| paths: { | ||
| '/resources/:customMethod': { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-109-custom-method-identifier-format': 'exception reason', | ||
| }, | ||
| }, | ||
| '/resources:{resourceId}:customMethod': { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-109-custom-method-identifier-format': 'exception reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid custom method formats', | ||
| document: { | ||
| paths: { | ||
| '/resources/:customMethod': {}, | ||
| '/resources:{resourceId}:customMethod': {}, | ||
| '/resources/:{resourceId}:customMethod': {}, | ||
| '/:customMethod': {}, | ||
| '/resources/{resourceId}/:customMethod': {}, | ||
| '/resources/{resourceId}:custom::Method': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-identifier-format', | ||
| message: | ||
| "The path /resources/:customMethod contains a '/' before a custom method. Custom methods should not start with a '/'.", | ||
| path: ['paths', '/resources/:customMethod'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-identifier-format', | ||
| message: 'Multiple colons found in "/resources:{resourceId}:customMethod"', | ||
| path: ['paths', '/resources:{resourceId}:customMethod'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-identifier-format', | ||
| message: 'Multiple colons found in "/resources/:{resourceId}:customMethod".', | ||
| path: ['paths', '/resources/:{resourceId}:customMethod'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-identifier-format', | ||
| message: | ||
| "The path /:customMethod contains a '/' before a custom method. Custom methods should not start with a '/'.", | ||
| path: ['paths', '/:customMethod'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-identifier-format', | ||
| message: | ||
| "The path /resources/{resourceId}/:customMethod contains a \'/\' before a custom method. Custom methods should not start with a \'/\'.", | ||
| path: ['paths', '/resources/{resourceId}/:customMethod'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-identifier-format', | ||
| message: 'Multiple colons found in "/resources/{resourceId}:custom::Method".', | ||
| path: ['paths', '/resources/{resourceId}:custom::Method'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); |
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
76 changes: 76 additions & 0 deletions
76
tools/spectral/ipa/rulesets/functions/IPA109CustomMethodIdentifierFormat.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,76 @@ | ||
| import { | ||
| collectAdoption, | ||
| collectAndReturnViolation, | ||
| collectException, | ||
| handleInternalError, | ||
| } from './utils/collectionUtils.js'; | ||
| import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js'; | ||
| import { hasException } from './utils/exceptions.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-109-custom-method-identifier-format'; | ||
|
|
||
| /** | ||
| * Validates custom method identifiers follow the correct format pattern. | ||
| * Custom methods should be defined using a colon character followed by the method name. | ||
| * Valid formats: /resources/{resourceId}:customMethod or /resources:customMethod | ||
| * | ||
| * @param {object} input - The path string being evaluated | ||
| * @param {object} _ - Unused | ||
| * @param {object} context - The context object containing path and document information | ||
| */ | ||
| export default (input, _, { path }) => { | ||
| let pathKey = path[1]; | ||
|
|
||
| if (!isCustomMethodIdentifier(pathKey)) { | ||
| return; | ||
| } | ||
|
|
||
| if (hasException(input, RULE_NAME)) { | ||
| collectException(input, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| const errors = checkViolationsAndReturnErrors(pathKey, path); | ||
| if (errors.length !== 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, errors); | ||
| } | ||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| function checkViolationsAndReturnErrors(pathKey, path) { | ||
| try { | ||
| // Check for multiple colons | ||
| const colonCount = (pathKey.match(/:/g) || []).length; | ||
| if (colonCount > 1) { | ||
| return [{ path, message: `Multiple colons found in "${pathKey}".` }]; | ||
| } | ||
|
|
||
| // Check for slash before colon | ||
| const invalidSlashBeforeColonPattern = /\/:/; | ||
|
|
||
| if (invalidSlashBeforeColonPattern.test(pathKey)) { | ||
| return [ | ||
| { | ||
| path, | ||
| message: `The path ${pathKey} contains a '/' before a custom method. Custom methods should not start with a '/'.`, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| // Check for invalid character before colon | ||
| // The character before colon should be either an alphabetical character or a closing curly brace '}' | ||
| const beforeColonMatch = pathKey.match(/([^a-zA-Z}]):/); | ||
| if (beforeColonMatch && beforeColonMatch[1] !== '') { | ||
| return [ | ||
| { | ||
| path, | ||
| message: `Invalid character '${beforeColonMatch[1]}' before colon in "${pathKey}".`, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| return []; | ||
| } catch (e) { | ||
| handleInternalError(RULE_NAME, path, e); | ||
| } | ||
| } | ||
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.