Skip to content

Commit dcc403e

Browse files
CLOUDP-271993: Add rule xgen-IPA-105-list-method-no-request-body
1 parent 698e772 commit dcc403e

File tree

4 files changed

+170
-3
lines changed

4 files changed

+170
-3
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-105-list-method-no-request-body', [
5+
{
6+
name: 'valid list without body',
7+
document: {
8+
paths: {
9+
'/resource': {
10+
get: {
11+
responses: {
12+
200: {},
13+
},
14+
},
15+
},
16+
},
17+
},
18+
errors: [],
19+
},
20+
{
21+
name: 'rule ignores singleton and Get',
22+
document: {
23+
paths: {
24+
'/resource': {
25+
get: {
26+
responses: {
27+
200: {},
28+
},
29+
},
30+
},
31+
'/resource/{id}': {
32+
get: {
33+
responses: {
34+
200: {},
35+
},
36+
requestBody: {
37+
content: {
38+
'application/vnd.atlas.2024-08-05+json': {
39+
schema: { type: 'object' },
40+
},
41+
},
42+
},
43+
},
44+
},
45+
'/resource/{id}/singleton': {
46+
get: {
47+
responses: {
48+
200: {},
49+
},
50+
requestBody: {
51+
content: {
52+
'application/vnd.atlas.2024-08-05+json': {
53+
schema: { type: 'object' },
54+
},
55+
},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
errors: [],
62+
},
63+
{
64+
name: 'invalid list with body',
65+
document: {
66+
paths: {
67+
'/resource': {
68+
get: {
69+
responses: {
70+
200: {},
71+
},
72+
requestBody: {
73+
content: {
74+
'application/vnd.atlas.2024-08-05+json': {
75+
schema: { type: 'object' },
76+
},
77+
},
78+
},
79+
},
80+
},
81+
},
82+
},
83+
errors: [
84+
{
85+
code: 'xgen-IPA-105-list-method-no-request-body',
86+
message: 'The List method must not include a request body. http://go/ipa/105',
87+
path: ['paths', '/resource', 'get'],
88+
severity: DiagnosticSeverity.Warning,
89+
},
90+
],
91+
},
92+
{
93+
name: 'invalid with exception',
94+
document: {
95+
paths: {
96+
'/resource': {
97+
get: {
98+
'x-xgen-IPA-exception': {
99+
'xgen-IPA-105-list-method-no-request-body': 'reason',
100+
},
101+
responses: {
102+
200: {},
103+
},
104+
requestBody: {
105+
content: {
106+
'application/vnd.atlas.2024-08-05+json': {
107+
schema: { type: 'object' },
108+
},
109+
},
110+
},
111+
},
112+
},
113+
},
114+
},
115+
errors: [],
116+
},
117+
]);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
functions:
55
- listResponseCodeShouldBe200OK
6+
- listMethodHasNoRequestBody
67

78
rules:
89
xgen-IPA-105-list-method-response-code-is-200:
@@ -12,3 +13,10 @@ rules:
1213
given: '$.paths[*].get'
1314
then:
1415
function: 'listResponseCodeShouldBe200OK'
16+
xgen-IPA-105-list-method-no-request-body:
17+
description: 'The List method request must not include a body. http://go/ipa/105'
18+
message: '{{error}} http://go/ipa/105'
19+
severity: warn
20+
given: '$.paths[*].get'
21+
then:
22+
function: 'listMethodHasNoRequestBody'

tools/spectral/ipa/rulesets/README.md

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

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

45-
| Rule Name | Description | Severity |
46-
| --------------------------------------------- | ---------------------------------------------------------------- | -------- |
47-
| xgen-IPA-105-list-method-response-code-is-200 | The List method must return a 200 OK response. http://go/ipa/105 | warn |
45+
| Rule Name | Description | Severity |
46+
| --------------------------------------------- | ------------------------------------------------------------------ | -------- |
47+
| xgen-IPA-105-list-method-response-code-is-200 | The List method must return a 200 OK response. http://go/ipa/105 | warn |
48+
| xgen-IPA-105-list-method-no-request-body | The List method request must not include a body. http://go/ipa/105 | warn |
4849

4950
### IPA-106
5051

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
import {
4+
getResourcePathItems,
5+
isResourceCollectionIdentifier,
6+
isSingletonResource,
7+
} from './utils/resourceEvaluation.js';
8+
9+
const RULE_NAME = 'xgen-IPA-105-list-method-no-request-body';
10+
const ERROR_MESSAGE = 'The List method must not include a request body.';
11+
12+
export default (input, _, { path, documentInventory }) => {
13+
const resourcePath = path[1];
14+
const oas = documentInventory.resolved;
15+
16+
if (
17+
!isResourceCollectionIdentifier(resourcePath) ||
18+
(isResourceCollectionIdentifier(resourcePath) && isSingletonResource(getResourcePathItems(resourcePath, oas.paths)))
19+
) {
20+
return;
21+
}
22+
23+
if (hasException(input, RULE_NAME)) {
24+
collectException(input, RULE_NAME, path);
25+
return;
26+
}
27+
28+
const errors = checkViolationsAndReturnErrors(input, path);
29+
30+
if (errors.length !== 0) {
31+
return collectAndReturnViolation(path, RULE_NAME, errors);
32+
}
33+
collectAdoption(path, RULE_NAME);
34+
};
35+
36+
function checkViolationsAndReturnErrors(getOperationObject, path) {
37+
if (getOperationObject.requestBody) {
38+
return [{ path, message: ERROR_MESSAGE }];
39+
}
40+
return [];
41+
}

0 commit comments

Comments
 (0)