-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-304963: IPA-114: Errors (check for 404 codes for parameterized endpoints) #640
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 all commits
Commits
Show all changes
3 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
110 changes: 110 additions & 0 deletions
110
tools/spectral/ipa/__tests__/IPA114ParameterizedPathsHave404NotFound.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,110 @@ | ||
| import testRule from './__helpers__/testRule.js'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-114-parameterized-paths-have-404-not-found', [ | ||
| { | ||
| name: 'valid parameterized path with 404 response', | ||
| document: { | ||
| paths: { | ||
| '/resources/{resourceId}': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| 404: { description: 'Not Found' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid parameterized path missing 404 response', | ||
| document: { | ||
| paths: { | ||
| '/resources/{resourceId}': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-114-parameterized-paths-have-404-not-found', | ||
| message: 'Parameterized path must define a 404 response.', | ||
| path: ['paths', '/resources/{resourceId}', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'non-parameterized path without 404 response (valid)', | ||
| document: { | ||
| paths: { | ||
| '/resources': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'parameterized path with multiple parameters and 404 response', | ||
| document: { | ||
| paths: { | ||
| '/resources/{resourceId}/items/{itemId}': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| 404: { description: 'Not Found' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'no responses', | ||
| document: { | ||
| paths: { | ||
| '/resources/{resourceId}': { | ||
| get: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-114-parameterized-paths-have-404-not-found', | ||
| message: 'Parameterized path must define a 404 response.', | ||
| path: ['paths', '/resources/{resourceId}', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'with exception', | ||
| document: { | ||
| paths: { | ||
| '/resources/{resourceId}': { | ||
| get: { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-114-parameterized-paths-have-404-not-found': 'Reason', | ||
| }, | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| 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
73 changes: 73 additions & 0 deletions
73
tools/spectral/ipa/rulesets/functions/IPA114ParameterizedPathsHave404NotFound.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,73 @@ | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { | ||
| collectAdoption, | ||
| collectAndReturnViolation, | ||
| collectException, | ||
| handleInternalError, | ||
| } from './utils/collectionUtils.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-114-parameterized-paths-have-404-not-found'; | ||
| const ERROR_MESSAGE = `Parameterized path must define a 404 response.`; | ||
| /** | ||
| * Validates that paths with parameters include a 404 response | ||
| * | ||
| * @param {object} input - The operation object to check | ||
| * @param {object} _ - Rule options (unused) | ||
| * @param {object} context - The context object containing path and document information | ||
| */ | ||
| export default (input, _, { path }) => { | ||
| // Path components: [paths, pathName, methodName, ...] | ||
| const pathName = path[1]; | ||
|
|
||
| const pathParamRegex = /{[^{}]+}/; | ||
| if (!pathParamRegex.test(pathName)) { | ||
| return; | ||
| } | ||
|
|
||
| // Check for exception at operation level | ||
| if (hasException(input, RULE_NAME)) { | ||
| collectException(input, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| const errors = checkViolationsAndReturnErrors(input.responses, path); | ||
| if (errors.length > 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, errors); | ||
| } | ||
|
|
||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| /** | ||
| * Check for violations in response structure | ||
| * | ||
| * @param {object} responses - The responses object to validate | ||
| * @param {Array} path - Path to the responses in the document | ||
| * @returns {Array} - Array of error objects | ||
| */ | ||
| function checkViolationsAndReturnErrors(responses, path) { | ||
| try { | ||
| if (!responses) { | ||
| return [ | ||
| { | ||
| path, | ||
| message: ERROR_MESSAGE, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| // Check for 404 Not Found response | ||
| if (!responses['404']) { | ||
| return [ | ||
| { | ||
| path, | ||
| message: ERROR_MESSAGE, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| 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.
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.
[Outside PR] Delete has it's own rule covering 404. If if introduce generic rule (which I think makes sense) we should remove dedicated 404 rule for delete. Is that planned? WDYT?
Uh oh!
There was an error while loading. Please reload this page.
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.
[Question] Separately - what is the purpose of mentioning all of the methods here.
I guess you wanted to exclude exceptions? If yes the same filtering can be done in the code.
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.
Yes, I wanted to exclude exceptions and all the stuff other than HTTP verbs. I agree that it can be handled in the code, but it is widely used approach at the moment, so I followed it
We can remove the dedicated 404 rule for delete, as you said it will be covered by this one. I will share the PR for removing it in a separate PR, if that is okay
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.
Thank you.