Skip to content

Commit 8faa5ff

Browse files
CLOUDP-271997- IPA 108 Delete 404 status code is returned (#488)
Co-authored-by: Lovisa Berggren <[email protected]>
1 parent c5d6e82 commit 8faa5ff

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-108-delete-include-404-response', [
5+
{
6+
name: 'valid DELETE with 404',
7+
document: {
8+
paths: {
9+
'/resource/{id}': {
10+
delete: {
11+
responses: {
12+
204: {},
13+
404: {
14+
description: 'Resource not found',
15+
},
16+
},
17+
},
18+
},
19+
},
20+
},
21+
errors: [],
22+
},
23+
{
24+
name: 'invalid DELETE with no responses',
25+
document: {
26+
paths: {
27+
'/resource/{id}': {
28+
delete: {},
29+
},
30+
},
31+
},
32+
errors: [
33+
{
34+
code: 'xgen-IPA-108-delete-include-404-response',
35+
message: 'DELETE method should include 404 status code for not found resources. http://go/ipa/108',
36+
path: ['paths', '/resource/{id}', 'delete'],
37+
severity: DiagnosticSeverity.Warning,
38+
},
39+
],
40+
},
41+
{
42+
name: 'invalid empty responses',
43+
document: {
44+
paths: {
45+
'/resource/{id}': {
46+
delete: {
47+
responses: {},
48+
},
49+
},
50+
},
51+
},
52+
errors: [
53+
{
54+
code: 'xgen-IPA-108-delete-include-404-response',
55+
message: 'DELETE method should include 404 status code for not found resources. http://go/ipa/108',
56+
path: ['paths', '/resource/{id}', 'delete'],
57+
severity: DiagnosticSeverity.Warning,
58+
},
59+
],
60+
},
61+
{
62+
name: 'invalid DELETE missing 404',
63+
document: {
64+
paths: {
65+
'/resource/{id}': {
66+
delete: {
67+
responses: {
68+
204: {},
69+
},
70+
},
71+
},
72+
},
73+
},
74+
errors: [
75+
{
76+
code: 'xgen-IPA-108-delete-include-404-response',
77+
message: 'DELETE method should include 404 status code for not found resources. http://go/ipa/108',
78+
path: ['paths', '/resource/{id}', 'delete'],
79+
severity: DiagnosticSeverity.Warning,
80+
},
81+
],
82+
},
83+
{
84+
name: 'valid with exception',
85+
document: {
86+
paths: {
87+
'/resource/{id}': {
88+
delete: {
89+
'x-xgen-IPA-exception': {
90+
'xgen-IPA-108-delete-include-404-response': 'Idempotent delete',
91+
},
92+
responses: {
93+
204: {},
94+
},
95+
},
96+
},
97+
},
98+
},
99+
errors: [],
100+
},
101+
]);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ rules:
1818
then:
1919
function: deleteMethod204Response
2020

21+
xgen-IPA-108-delete-include-404-response:
22+
description: DELETE method must include 404 response and return it when resource not found. http://go/ipa/108
23+
message: '{{error}} http://go/ipa/108'
24+
severity: warn
25+
given: $.paths[*].delete
26+
then:
27+
function: deleteMethod404Response
28+
2129
functions:
2230
- deleteMethodResponseShouldNotHaveSchema
2331
- deleteMethod204Response
32+
- deleteMethod404Response

tools/spectral/ipa/rulesets/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ 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 |
56-
| xgen-IPA-108-delete-method-return-204-response | DELETE method must return 204 No Content. 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 |
57+
| 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 |
5758

5859
### IPA-109
5960

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-include-404-response';
5+
const ERROR_MESSAGE = 'DELETE method should include 404 status code for not found resources.';
6+
7+
/**
8+
* Delete method should include 404 status code for not found resources
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['404']) {
22+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
23+
}
24+
return collectAdoption(path, RULE_NAME);
25+
};

0 commit comments

Comments
 (0)