Skip to content

Commit dba8cf2

Browse files
CLOUDP-304931: Adds xgen-IPA-105-resource-has-list (#515)
1 parent 2a761db commit dba8cf2

File tree

4 files changed

+145
-0
lines changed

4 files changed

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
functions:
55
- listResponseCodeShouldBe200OK
66
- listMethodHasNoRequestBody
7+
- eachResourceHasListMethod
78

89
rules:
910
xgen-IPA-105-list-method-response-code-is-200:
@@ -20,3 +21,11 @@ rules:
2021
given: '$.paths[*].get'
2122
then:
2223
function: 'listMethodHasNoRequestBody'
24+
xgen-IPA-105-resource-has-list:
25+
description: 'APIs must provide a List method for resources. http://go/ipa/105'
26+
message: '{{error}} http://go/ipa/105'
27+
severity: warn
28+
given: '$.paths'
29+
then:
30+
field: '@key'
31+
function: 'eachResourceHasListMethod'

tools/spectral/ipa/rulesets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ For rule definitions, see [IPA-105.yaml](https://github.com/mongodb/openapi/blob
4646
| --------------------------------------------- | ------------------------------------------------------------------ | -------- |
4747
| xgen-IPA-105-list-method-response-code-is-200 | The List method must return a 200 OK response. http://go/ipa/105 | warn |
4848
| xgen-IPA-105-list-method-no-request-body | The List method request must not include a body. http://go/ipa/105 | warn |
49+
| xgen-IPA-105-resource-has-list | APIs must provide a List method for resources. http://go/ipa/105 | warn |
4950

5051
### IPA-106
5152

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

0 commit comments

Comments
 (0)