Skip to content

Commit e50ef70

Browse files
committed
fix: spec for IPA 108
1 parent b1351c4 commit e50ef70

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

tools/spectral/ipa/__tests__/deleteMethod404Response.test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,44 @@ testRule('xgen-IPA-108-delete-include-404-response', [
2020
},
2121
errors: [],
2222
},
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+
},
2361
{
2462
name: 'invalid DELETE missing 404',
2563
document: {
@@ -36,7 +74,7 @@ testRule('xgen-IPA-108-delete-include-404-response', [
3674
errors: [
3775
{
3876
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',
77+
message: 'DELETE method should include 404 status code for not found resources. http://go/ipa/108',
4078
path: ['paths', '/resource/{id}', 'delete'],
4179
severity: DiagnosticSeverity.Warning,
4280
},

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

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

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-
3729
functions:
3830
- deleteMethodResponseShouldNotHaveSchema
3931
- deleteMethod204Response
40-
- deleteMethodNoRequestBody
4132
- deleteMethod404Response

tools/spectral/ipa/rulesets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ For rule definitions, see [IPA-108.yaml](https://github.com/mongodb/openapi/blob
5454
| ---------------------------------------------- | ------------------------------------------------------------------------------------ | -------- |
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 |
57+
| xgen-IPA-108-delete-include-404-response | DELETE method must include 404 response and return it when resource not found | warn |
5758

5859
### IPA-109
5960

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { collectAdoption, collectAndReturnViolation, collectException } from './
22
import { hasException } from './utils/exceptions.js';
33

44
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';
5+
const ERROR_MESSAGE = 'DELETE method should include 404 status code for not found resources.';
66

77
/**
88
* Delete method should include 404 status code for not found resources
@@ -12,18 +12,14 @@ 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 deleteOp = input;
16-
if (!deleteOp) return;
17-
18-
if (hasException(deleteOp, RULE_NAME)) {
19-
collectException(deleteOp, RULE_NAME, path);
15+
const responses = input.responses;
16+
if (hasException(input, RULE_NAME)) {
17+
collectException(input, RULE_NAME, path);
2018
return;
2119
}
2220

23-
const responses = deleteOp.responses || {};
24-
if (!responses['404']) {
21+
if (!responses || !responses['404']) {
2522
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
2623
}
27-
28-
collectAdoption(path, RULE_NAME);
24+
return collectAdoption(path, RULE_NAME);
2925
};

0 commit comments

Comments
 (0)