-
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
Changes from 6 commits
a5d9157
4c12229
c0502df
07d6d7a
3432d84
ad75fa4
a61e37f
ce8c140
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'], | ||
| 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: [], | ||
| }, | ||
| ]); | ||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| 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.'; | ||
|
|
||
| /** | ||
| * 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. Filter out not relevant use cases that should not lead to adoption. | ||
| const deleteOp = input; | ||
| if (!deleteOp.responses || deleteOp.responses.length === 0) { | ||
| return; | ||
| } | ||
wtrocki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
wtrocki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 2. Handle exception on OpenAPI schema | ||
| if (hasException(deleteOp, RULE_NAME)) { | ||
| collectException(deleteOp, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| // 3. Validation | ||
| const errors = checkViolations(deleteOp.responses); | ||
| if (errors) { | ||
| return collectAndReturnViolation(path, RULE_NAME, errors); | ||
| } | ||
|
|
||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| /** | ||
| * Check if the operation has validation issues | ||
| * @param {object} input - The object to vefify | ||
| * @return {Array<string>|undefined} - The content types that have a schema | ||
wtrocki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| function checkViolations(input) { | ||
| try { | ||
| if (input && input['204']) { | ||
| const successResponse = input['204']; | ||
| if (successResponse.content) { | ||
| const errors = []; | ||
| for (const contentType of Object.keys(successResponse.content)) { | ||
| if (successResponse.content[contentType] && successResponse.content[contentType].schema) { | ||
| errors.push({ | ||
| message: `Error found for ${contentType}: ${ERROR_MESSAGE}`, | ||
| }); | ||
| } | ||
| } | ||
| return errors.length > 0 ? errors : undefined; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| return ['Internal Rule Error without reporting violation' + e]; | ||
|
||
| } | ||
| // No errors returning undefined | ||
| return undefined; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.