-
Couldn't load subscription status.
- Fork 14
CLOUDP-271997: IPA 108 - delete method schema #483
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
8 commits
Select commit
Hold shift + click to select a range
a5d9157
CLOUDP-271997: IPA 108 - delete method schema
wtrocki 4c12229
Apply suggestions from code review
wtrocki c0502df
Apply suggestions from code review
wtrocki 07d6d7a
fix: format source code
wtrocki 3432d84
fix: adoption for 204 only
wtrocki ad75fa4
fix: formalize format
wtrocki a61e37f
fix: change output format for method
wtrocki ce8c140
fix: follow specification for rule creation
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
Empty file.
98 changes: 98 additions & 0 deletions
98
tools/spectral/ipa/__tests__/deleteMethodResponseShouldNotHaveSchema.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,98 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-108-delete-response-should-be-empty', [ | ||
| { | ||
| name: 'valid DELETE with void 204', | ||
| document: { | ||
| paths: { | ||
| '/resource/{id}': { | ||
| delete: { | ||
| responses: { | ||
| 204: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'valid DELETE with void 204 versioned', | ||
| document: { | ||
| paths: { | ||
| '/resource/{id}': { | ||
| delete: { | ||
| responses: { | ||
| 204: { | ||
| description: 'No Content', | ||
| content: { | ||
| 'application/vnd.atlas.2023-01-01+json': { | ||
| 'x-xgen-version': '2023-01-01', | ||
| }, | ||
| 'application/vnd.atlas.2023-03-01+json': { | ||
| 'x-xgen-version': '2023-01-01', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid DELETE with non-void 204', | ||
| document: { | ||
| paths: { | ||
| '/resource/{id}': { | ||
| delete: { | ||
| responses: { | ||
| 204: { | ||
| content: { | ||
| 'application/vnd.atlas.2023-01-01+json': { | ||
| schema: { type: 'object' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-108-delete-response-should-be-empty', | ||
| message: | ||
| 'Error found for application/vnd.atlas.2023-01-01+json: DELETE method should return an empty response. The response should not have a schema property. http://go/ipa/108', | ||
| path: ['paths', '/resource/{id}', 'delete', 'responses', '204'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'valid with exception', | ||
| document: { | ||
| paths: { | ||
| '/resource/{id}': { | ||
| delete: { | ||
| responses: { | ||
| 204: { | ||
| 'application/vnd.atlas.2023-01-01+json': { | ||
|
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. ❤️ |
||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-108-delete-response-should-be-empty': 'Legacy API', | ||
| }, | ||
| content: { | ||
| schema: { type: 'object' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # IPA-108: Delete | ||
| # http://go/ipa/108 | ||
|
|
||
| rules: | ||
| xgen-IPA-108-delete-response-should-be-empty: | ||
| description: Delete method response should not have schema reference to object. http://go/ipa/108 | ||
| message: '{{error}} http://go/ipa/108' | ||
| severity: warn | ||
| given: $.paths[*].delete.responses[204] | ||
| then: | ||
| function: deleteMethodResponseShouldNotHaveSchema | ||
|
|
||
| functions: | ||
| - deleteMethodResponseShouldNotHaveSchema |
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
58 changes: 58 additions & 0 deletions
58
tools/spectral/ipa/rulesets/functions/deleteMethodResponseShouldNotHaveSchema.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,58 @@ | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { | ||
| collectAdoption, | ||
| collectAndReturnViolation, | ||
| collectException, | ||
| handleInternalError, | ||
| } from './utils/collectionUtils.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-108-delete-response-should-be-empty'; | ||
| const ERROR_MESSAGE = 'DELETE method should return an empty response. The response should not have a schema property.'; | ||
|
|
||
| /** | ||
| * Delete method should return an empty response | ||
| * @param {object} input - The delete operation object | ||
| * @param {object} _ - Unused | ||
| * @param {object} context - The context object containing the path | ||
| */ | ||
| export default (input, _, { path }) => { | ||
| // 1. Handle exception on OpenAPI schema | ||
| if (hasException(input, RULE_NAME)) { | ||
| collectException(input, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| // 2. Validation | ||
| const errors = checkViolations(input, path); | ||
| if (errors.length > 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, errors); | ||
| } | ||
|
|
||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| /** | ||
| * Check if the operation has validation issues | ||
| * @param {object} input - The object to vefify | ||
| * @param {object} jsonPathArray - The jsonPathArray covering location in the OpenAPI schema | ||
| * @return {Array<string>} - errors array () | ||
| */ | ||
| function checkViolations(input, jsonPathArray) { | ||
| const errors = []; | ||
| try { | ||
| const successResponse = input; | ||
| if (successResponse.content) { | ||
| for (const contentType of Object.keys(successResponse.content)) { | ||
| if (successResponse.content[contentType] && successResponse.content[contentType].schema) { | ||
| errors.push({ | ||
| path: jsonPathArray, | ||
| message: `Error found for ${contentType}: ${ERROR_MESSAGE}`, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } catch (e) { | ||
| handleInternalError(RULE_NAME, jsonPathArray, e); | ||
| } | ||
| return 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
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.