Skip to content

Commit 4a5518e

Browse files
committed
CLOUDP-271997- IPA-108 delete should not return 204 response
1 parent 4c12229 commit 4a5518e

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-108-delete-method-return-204-response', [
5+
{
6+
name: 'valid DELETE with 204',
7+
document: {
8+
paths: {
9+
'/resource/{id}': {
10+
delete: {
11+
responses: {
12+
204: {
13+
description: 'Resource deleted',
14+
},
15+
},
16+
},
17+
},
18+
},
19+
},
20+
errors: [],
21+
},
22+
{
23+
name: 'invalid DELETE missing 204',
24+
document: {
25+
paths: {
26+
'/resource/{id}': {
27+
delete: {
28+
responses: {
29+
200: {
30+
description: 'Resource deleted',
31+
},
32+
},
33+
},
34+
},
35+
},
36+
},
37+
errors: [
38+
{
39+
code: 'xgen-IPA-108-delete-method-return-204-response',
40+
message: 'DELETE method should return 204 No Content status code http://go/ipa/108',
41+
path: ['paths', '/resource/{id}', 'delete'],
42+
severity: DiagnosticSeverity.Warning,
43+
},
44+
],
45+
},
46+
{
47+
name: 'valid with exception',
48+
document: {
49+
paths: {
50+
'/resource/{id}': {
51+
delete: {
52+
'x-xgen-IPA-exception': {
53+
'xgen-IPA-108-delete-method-return-204-response': 'Legacy API',
54+
},
55+
responses: {
56+
200: {
57+
description: 'Resource deleted',
58+
},
59+
},
60+
},
61+
},
62+
},
63+
},
64+
errors: [],
65+
},
66+
]);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,14 @@ rules:
1010
then:
1111
function: deleteMethodResponseShouldNotHaveSchema
1212

13+
xgen-IPA-108-delete-method-return-204-response:
14+
description: DELETE method must return 204 No Content. http://go/ipa/108
15+
message: '{{error}} http://go/ipa/108'
16+
severity: warn
17+
given: $.paths[*].delete
18+
then:
19+
function: deleteMethod204Response
20+
1321
functions:
1422
- deleteMethodResponseShouldNotHaveSchema
23+
- deleteMethod204Response
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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-method-return-204-response';
5+
const ERROR_MESSAGE = 'DELETE method should return 204 No Content status code';
6+
7+
/**
8+
* Delete method should return 204 No Content status code
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+
const deleteOp = input;
16+
if (!deleteOp) return;
17+
18+
if (hasException(deleteOp, RULE_NAME)) {
19+
collectException(deleteOp, RULE_NAME, path);
20+
return;
21+
}
22+
23+
const responses = deleteOp.responses || {};
24+
if (!responses['204']) {
25+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
26+
}
27+
28+
collectAdoption(path, RULE_NAME);
29+
};

0 commit comments

Comments
 (0)