Skip to content

Commit a5d9157

Browse files
committed
CLOUDP-271997: IPA 108 - delete method schema
1 parent 1b297b7 commit a5d9157

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-108-delete-response-should-be-empty', [
5+
{
6+
name: 'valid DELETE with void 204',
7+
document: {
8+
paths: {
9+
'/resource/{id}': {
10+
delete: {
11+
responses: {
12+
204: {},
13+
},
14+
},
15+
},
16+
},
17+
},
18+
errors: [],
19+
},
20+
{
21+
name: 'valid DELETE with void 204',
22+
document: {
23+
paths: {
24+
'/resource/{id}': {
25+
delete: {
26+
responses: {
27+
204: {
28+
description: 'No Content',
29+
content: {
30+
'application/vnd.atlas.2023-01-01+json': {
31+
'x-xgen-version': '2023-01-01',
32+
},
33+
},
34+
},
35+
},
36+
},
37+
},
38+
},
39+
},
40+
errors: [],
41+
},
42+
{
43+
name: 'invalid DELETE with non-void 204',
44+
document: {
45+
paths: {
46+
'/resource/{id}': {
47+
delete: {
48+
responses: {
49+
204: {
50+
content: {
51+
'application/vnd.atlas.2023-01-01+json': {
52+
schema: { type: 'object' },
53+
},
54+
},
55+
},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
errors: [
62+
{
63+
code: 'xgen-IPA-108-delete-response-should-be-empty',
64+
message:
65+
'DELETE method should return an empty response. The response should not have a schema property and reference to models http://go/ipa/108',
66+
path: ['paths', '/resource/{id}', 'delete'],
67+
severity: DiagnosticSeverity.Warning,
68+
},
69+
],
70+
},
71+
{
72+
name: 'valid with exception',
73+
document: {
74+
paths: {
75+
'/resource/{id}': {
76+
delete: {
77+
'x-xgen-IPA-exception': {
78+
'xgen-IPA-108-delete-response-should-be-empty': 'Legacy API',
79+
},
80+
responses: {
81+
204: {
82+
content: {
83+
'application/vnd.atlas.2023-01-01+json': {
84+
schema: { type: 'object' },
85+
},
86+
},
87+
},
88+
},
89+
},
90+
},
91+
},
92+
},
93+
errors: [],
94+
},
95+
]);

tools/spectral/ipa/ipa-spectral.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extends:
66
- ./rulesets/IPA-113.yaml
77
- ./rulesets/IPA-123.yaml
88
- ./rulesets/IPA-106.yaml
9+
- ./rulesets/IPA-108.yaml
910

1011
overrides:
1112
- files:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# IPA-108: Delete
2+
# http://go/ipa/108
3+
4+
rules:
5+
xgen-IPA-108-delete-response-should-be-empty:
6+
description: Delete method response should not have schema reference to object http://go/ipa/108
7+
message: '{{error}} http://go/ipa/108'
8+
severity: warn
9+
given: $.paths[*].delete
10+
then:
11+
function: deleteMethodResponseShouldNotHaveSchema
12+
13+
functions:
14+
- deleteMethodResponseShouldNotHaveSchema

tools/spectral/ipa/rulesets/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ For rule definitions, see [IPA-106.yaml](https://github.com/mongodb/openapi/blob
4242
| ------------------------------------------------------------------ | -------------------------------------------------------------------------------- | -------- |
4343
| xgen-IPA-106-create-method-request-body-is-request-suffixed-object | The Create method request should be a Request suffixed object. http://go/ipa/106 | warn |
4444

45+
### IPA-108
46+
47+
For rule definitions, see [IPA-108.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-108.yaml).
48+
49+
| Rule Name | Description | Severity |
50+
| -------------------------------------------- | ----------------------------------------------------------------------------------- | -------- |
51+
| xgen-IPA-108-delete-response-should-be-empty | Delete method response should not have schema reference to object http://go/ipa/108 | warn |
52+
4553
### IPA-109
4654

4755
For rule definitions, see [IPA-109.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-109.yaml).
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
4+
const RULE_NAME = 'xgen-IPA-108-delete-response-should-be-empty';
5+
const ERROR_MESSAGE =
6+
'DELETE method should return an empty response. The response should not have a schema property and reference to models';
7+
8+
/**
9+
* Delete method should return an empty response
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+
17+
if (hasException(deleteOp, RULE_NAME)) {
18+
collectException(deleteOp, RULE_NAME, path);
19+
return;
20+
}
21+
22+
const responses = deleteOp.responses || {};
23+
for (const [status, response] of Object.entries(responses)) {
24+
if (status === '204' && response.content) {
25+
for (const contentType of Object.keys(response.content)) {
26+
if (response.content[contentType].schema) {
27+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
28+
}
29+
}
30+
}
31+
}
32+
33+
collectAdoption(path, RULE_NAME);
34+
};

0 commit comments

Comments
 (0)