-
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 5 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
95 changes: 95 additions & 0 deletions
95
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,95 @@ | ||
| 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', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| 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: | ||
| 'DELETE method should return an empty response. The response should not have a schema property and reference to models. http://go/ipa/108', | ||
| path: ['paths', '/resource/{id}', 'delete'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'valid with exception', | ||
| document: { | ||
| paths: { | ||
| '/resource/{id}': { | ||
| delete: { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-108-delete-response-should-be-empty': 'Legacy API', | ||
| }, | ||
| responses: { | ||
| 204: { | ||
| content: { | ||
| 'application/vnd.atlas.2023-01-01+json': { | ||
| 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 | ||
| 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
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { collectAdoption, collectAndReturnViolation, collectException } 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 and reference to models.'; | ||
|
|
||
| /** | ||
| * 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 }) => { | ||
| const deleteOp = input; | ||
| if (!deleteOp.responses || deleteOp.responses.length === 0) { | ||
| return; | ||
| } | ||
wtrocki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (hasException(deleteOp, RULE_NAME)) { | ||
| collectException(deleteOp, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| const responses = deleteOp.responses; | ||
| if (responses && responses['204']) { | ||
| const successResponse = responses['204']; | ||
| if (successResponse.content) { | ||
| for (const contentType of Object.keys(successResponse.content)) { | ||
| // Check if the response has a schema property | ||
| if (successResponse.content[contentType] && successResponse.content[contentType].schema) { | ||
| return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE); | ||
| } | ||
| } | ||
| } | ||
| collectAdoption(path, RULE_NAME); | ||
| } | ||
lovisaberggren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
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.