Skip to content

Commit eb97c1e

Browse files
wtrockilovisaberggrenyelizhenden-mdb
authored
CLOUDP-271997- IPA-108 delete should return 204 response (#487)
Co-authored-by: Lovisa Berggren <[email protected]> Co-authored-by: Yeliz Henden <[email protected]>
1 parent 3b6b3fb commit eb97c1e

File tree

4 files changed

+142
-3
lines changed

4 files changed

+142
-3
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 with no responses',
24+
document: {
25+
paths: {
26+
'/resource/{id}': {
27+
delete: {
28+
responses: {},
29+
},
30+
},
31+
},
32+
},
33+
errors: [
34+
{
35+
code: 'xgen-IPA-108-delete-method-return-204-response',
36+
message: 'DELETE method should return 204 No Content status code. http://go/ipa/108',
37+
path: ['paths', '/resource/{id}', 'delete'],
38+
severity: DiagnosticSeverity.Warning,
39+
},
40+
],
41+
},
42+
{
43+
name: 'invalid DELETE with no responses',
44+
document: {
45+
paths: {
46+
'/resource/{id}': {
47+
delete: {},
48+
},
49+
},
50+
},
51+
errors: [
52+
{
53+
code: 'xgen-IPA-108-delete-method-return-204-response',
54+
message: 'DELETE method should return 204 No Content status code. http://go/ipa/108',
55+
path: ['paths', '/resource/{id}', 'delete'],
56+
severity: DiagnosticSeverity.Warning,
57+
},
58+
],
59+
},
60+
{
61+
name: 'invalid DELETE missing 204',
62+
document: {
63+
paths: {
64+
'/resource/{id}': {
65+
delete: {
66+
responses: {
67+
200: {
68+
description: 'Resource deleted',
69+
},
70+
},
71+
},
72+
},
73+
},
74+
},
75+
errors: [
76+
{
77+
code: 'xgen-IPA-108-delete-method-return-204-response',
78+
message: 'DELETE method should return 204 No Content status code. http://go/ipa/108',
79+
path: ['paths', '/resource/{id}', 'delete'],
80+
severity: DiagnosticSeverity.Warning,
81+
},
82+
],
83+
},
84+
{
85+
name: 'valid with exception',
86+
document: {
87+
paths: {
88+
'/resource/{id}': {
89+
delete: {
90+
'x-xgen-IPA-exception': {
91+
'xgen-IPA-108-delete-method-return-204-response': 'Legacy API',
92+
},
93+
responses: {
94+
200: {
95+
description: 'Resource deleted',
96+
},
97+
},
98+
},
99+
},
100+
},
101+
},
102+
errors: [],
103+
},
104+
]);

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

tools/spectral/ipa/rulesets/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ For rule definitions, see [IPA-106.yaml](https://github.com/mongodb/openapi/blob
5050

5151
For rule definitions, see [IPA-108.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-108.yaml).
5252

53-
| Rule Name | Description | Severity |
54-
| -------------------------------------------- | ------------------------------------------------------------------------------------ | -------- |
55-
| xgen-IPA-108-delete-response-should-be-empty | Delete method response should not have schema reference to object. http://go/ipa/108 | warn |
53+
| Rule Name | Description | Severity |
54+
| ---------------------------------------------- | ------------------------------------------------------------------------------------ | -------- |
55+
| xgen-IPA-108-delete-response-should-be-empty | Delete method response should not have schema reference to object. http://go/ipa/108 | warn |
56+
| xgen-IPA-108-delete-method-return-204-response | DELETE method must return 204 No Content. http://go/ipa/108 | warn |
5657

5758
### IPA-109
5859

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-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 responses = input.responses;
16+
if (hasException(input, RULE_NAME)) {
17+
collectException(input, RULE_NAME, path);
18+
return;
19+
}
20+
21+
if (!responses || !responses['204']) {
22+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
23+
}
24+
return collectAdoption(path, RULE_NAME);
25+
};

0 commit comments

Comments
 (0)