Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions tools/spectral/ipa/__tests__/deleteMethodNoRequestBody.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-108-delete-request-no-body', [
{
name: 'valid DELETE without body',
document: {
paths: {
'/resource/{id}': {
delete: {
responses: {
204: {},
},
},
},
},
},
errors: [],
},
{
name: 'invalid DELETE with body',
document: {
paths: {
'/resource/{id}': {
delete: {
requestBody: {
content: {
'application/vnd.atlas.2024-08-05+json': {
schema: { type: 'object' },
},
},
},
responses: {
204: {},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-108-delete-request-no-body',
message: 'DELETE method should not have a request body. 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-request-no-body': 'Bulk delete operation',
},
requestBody: {
content: {
'application/json': {
schema: { type: 'object' },
},
},
},
},
},
},
},
errors: [],
},
]);
9 changes: 9 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-108.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ rules:
then:
function: deleteMethod404Response

xgen-IPA-108-delete-request-no-body:
description: DELETE method must not have request body. http://go/ipa/108
message: '{{error}} http://go/ipa/108'
severity: warn
given: $.paths[*].delete
then:
function: deleteMethodNoRequestBody

functions:
- deleteMethodResponseShouldNotHaveSchema
- deleteMethod204Response
- deleteMethodNoRequestBody
- deleteMethod404Response
1 change: 1 addition & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ For rule definitions, see [IPA-108.yaml](https://github.com/mongodb/openapi/blob
| xgen-IPA-108-delete-response-should-be-empty | Delete method response should not have schema reference to object. http://go/ipa/108 | warn |
| xgen-IPA-108-delete-method-return-204-response | DELETE method must return 204 No Content. http://go/ipa/108 | warn |
| xgen-IPA-108-delete-include-404-response | DELETE method must include 404 response and return it when resource not found. http://go/ipa/108 | warn |
| xgen-IPA-108-delete-request-no-body | DELETE method must not have request body. http://go/ipa/108 | warn |

### IPA-109

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const ERROR_MESSAGE = 'DELETE method should return 204 No Content status code.';
* @param {object} context - The context object containing the path
*/
export default (input, _, { path }) => {
const responses = input.responses;
if (hasException(input, RULE_NAME)) {
collectException(input, RULE_NAME, path);
return;
}

const responses = input.responses;
if (!responses || !responses['204']) {
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const ERROR_MESSAGE = 'DELETE method should include 404 status code for not foun
* @param {object} context - The context object containing the path
*/
export default (input, _, { path }) => {
const responses = input.responses;
if (hasException(input, RULE_NAME)) {
collectException(input, RULE_NAME, path);
return;
}

const responses = input.responses;
if (!responses || !responses['404']) {
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
import { hasException } from './utils/exceptions.js';

const RULE_NAME = 'xgen-IPA-108-delete-request-no-body';
const ERROR_MESSAGE = 'DELETE method should not have a request body.';

/**
* Delete method should not have a request body
*
* @param {object} input - The delete operation object
* @param {object} _ - Unused
* @param {object} context - The context object containing the path
*/
export default (input, _, { path }) => {
if (hasException(input, RULE_NAME)) {
collectException(input, RULE_NAME, path);
return;
}

const requestBody = input.requestBody;
if (requestBody) {
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
}
return collectAdoption(path, RULE_NAME);
};
Loading