Skip to content

Commit c37eb53

Browse files
committed
CLOUDP-271997- IPA 108 Delete 404 status code is returned when trying to delete a non-existing IPA
1 parent 4a5518e commit c37eb53

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 missing 404',
25+
document: {
26+
paths: {
27+
'/resource/{id}': {
28+
delete: {
29+
responses: {
30+
204: {},
31+
},
32+
},
33+
},
34+
},
35+
},
36+
errors: [
37+
{
38+
code: 'xgen-IPA-108-delete-include-404-response',
39+
message: 'DELETE method should include 404 status code for not found resources http://go/ipa/108',
40+
path: ['paths', '/resource/{id}', 'delete'],
41+
severity: DiagnosticSeverity.Warning,
42+
},
43+
],
44+
},
45+
{
46+
name: 'valid with exception',
47+
document: {
48+
paths: {
49+
'/resource/{id}': {
50+
delete: {
51+
'x-xgen-IPA-exception': {
52+
'xgen-IPA-108-delete-include-404-response': 'Idempotent delete',
53+
},
54+
responses: {
55+
204: {},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
errors: [],
62+
},
63+
]);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ 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
23+
message: '{{error}} http://go/ipa/108'
24+
severity: warn
25+
given: $.paths[*].delete
26+
then:
27+
function: deleteMethod404Response
28+
29+
xgen-IPA-108-delete-request-no-body:
30+
description: DELETE method must not have request body
31+
message: '{{error}} http://go/ipa/108'
32+
severity: warn
33+
given: $.paths[*].delete
34+
then:
35+
function: deleteMethodNoRequestBody
36+
2137
functions:
2238
- deleteMethodResponseShouldNotHaveSchema
2339
- deleteMethod204Response
40+
- deleteMethodNoRequestBody
41+
- deleteMethod404Response
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-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 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['404']) {
25+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
26+
}
27+
28+
collectAdoption(path, RULE_NAME);
29+
};

0 commit comments

Comments
 (0)