Skip to content

Commit 320b534

Browse files
authored
CLOUDP-271997: request no body (#489)
1 parent 8faa5ff commit 320b534

File tree

6 files changed

+108
-2
lines changed

6 files changed

+108
-2
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-108-delete-request-no-body', [
5+
{
6+
name: 'valid DELETE without body',
7+
document: {
8+
paths: {
9+
'/resource/{id}': {
10+
delete: {
11+
responses: {
12+
204: {},
13+
},
14+
},
15+
},
16+
},
17+
},
18+
errors: [],
19+
},
20+
{
21+
name: 'invalid DELETE with body',
22+
document: {
23+
paths: {
24+
'/resource/{id}': {
25+
delete: {
26+
requestBody: {
27+
content: {
28+
'application/vnd.atlas.2024-08-05+json': {
29+
schema: { type: 'object' },
30+
},
31+
},
32+
},
33+
responses: {
34+
204: {},
35+
},
36+
},
37+
},
38+
},
39+
},
40+
errors: [
41+
{
42+
code: 'xgen-IPA-108-delete-request-no-body',
43+
message: 'DELETE method should not have a request body. http://go/ipa/108',
44+
path: ['paths', '/resource/{id}', 'delete'],
45+
severity: DiagnosticSeverity.Warning,
46+
},
47+
],
48+
},
49+
{
50+
name: 'valid with exception',
51+
document: {
52+
paths: {
53+
'/resource/{id}': {
54+
delete: {
55+
'x-xgen-IPA-exception': {
56+
'xgen-IPA-108-delete-request-no-body': 'Bulk delete operation',
57+
},
58+
requestBody: {
59+
content: {
60+
'application/json': {
61+
schema: { type: 'object' },
62+
},
63+
},
64+
},
65+
},
66+
},
67+
},
68+
},
69+
errors: [],
70+
},
71+
]);

tools/spectral/ipa/rulesets/IPA-108.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ rules:
2626
then:
2727
function: deleteMethod404Response
2828

29+
xgen-IPA-108-delete-request-no-body:
30+
description: DELETE method must not have request body. http://go/ipa/108
31+
message: '{{error}} http://go/ipa/108'
32+
severity: warn
33+
given: $.paths[*].delete
34+
then:
35+
function: deleteMethodNoRequestBody
36+
2937
functions:
3038
- deleteMethodResponseShouldNotHaveSchema
3139
- deleteMethod204Response
40+
- deleteMethodNoRequestBody
3241
- deleteMethod404Response

tools/spectral/ipa/rulesets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ For rule definitions, see [IPA-108.yaml](https://github.com/mongodb/openapi/blob
5555
| xgen-IPA-108-delete-response-should-be-empty | Delete method response should not have schema reference to object. http://go/ipa/108 | warn |
5656
| xgen-IPA-108-delete-method-return-204-response | DELETE method must return 204 No Content. http://go/ipa/108 | warn |
5757
| 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 |
58+
| xgen-IPA-108-delete-request-no-body | DELETE method must not have request body. http://go/ipa/108 | warn |
5859

5960
### IPA-109
6061

tools/spectral/ipa/rulesets/functions/deleteMethod204Response.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const ERROR_MESSAGE = 'DELETE method should return 204 No Content status code.';
1212
* @param {object} context - The context object containing the path
1313
*/
1414
export default (input, _, { path }) => {
15-
const responses = input.responses;
1615
if (hasException(input, RULE_NAME)) {
1716
collectException(input, RULE_NAME, path);
1817
return;
1918
}
2019

20+
const responses = input.responses;
2121
if (!responses || !responses['204']) {
2222
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
2323
}

tools/spectral/ipa/rulesets/functions/deleteMethod404Response.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const ERROR_MESSAGE = 'DELETE method should include 404 status code for not foun
1212
* @param {object} context - The context object containing the path
1313
*/
1414
export default (input, _, { path }) => {
15-
const responses = input.responses;
1615
if (hasException(input, RULE_NAME)) {
1716
collectException(input, RULE_NAME, path);
1817
return;
1918
}
2019

20+
const responses = input.responses;
2121
if (!responses || !responses['404']) {
2222
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
2323
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
2+
import { hasException } from './utils/exceptions.js';
3+
4+
const RULE_NAME = 'xgen-IPA-108-delete-request-no-body';
5+
const ERROR_MESSAGE = 'DELETE method should not have a request body.';
6+
7+
/**
8+
* Delete method should not have a request body
9+
*
10+
* @param {object} input - The delete operation object
11+
* @param {object} _ - Unused
12+
* @param {object} context - The context object containing the path
13+
*/
14+
export default (input, _, { path }) => {
15+
if (hasException(input, RULE_NAME)) {
16+
collectException(input, RULE_NAME, path);
17+
return;
18+
}
19+
20+
const requestBody = input.requestBody;
21+
if (requestBody) {
22+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
23+
}
24+
return collectAdoption(path, RULE_NAME);
25+
};

0 commit comments

Comments
 (0)