Skip to content

Commit e3135cf

Browse files
CLOUDP-271992: Add rule xgen-IPA-105-list-method-response-code-is-200
1 parent c34095e commit e3135cf

File tree

4 files changed

+232
-3
lines changed

4 files changed

+232
-3
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-105-list-method-response-code-is-200', [
5+
{
6+
name: 'valid methods',
7+
document: {
8+
paths: {
9+
'/resource': {
10+
get: {
11+
responses: {
12+
200: {},
13+
400: {},
14+
500: {},
15+
},
16+
},
17+
},
18+
'/resource/{id}': {
19+
get: {
20+
responses: {
21+
400: {},
22+
500: {},
23+
},
24+
},
25+
},
26+
'/resource/{id}:customMethod': {
27+
get: {
28+
responses: {
29+
400: {},
30+
500: {},
31+
},
32+
},
33+
},
34+
'/resource/{id}/singleton': {
35+
get: {
36+
responses: {
37+
400: {},
38+
500: {},
39+
},
40+
},
41+
},
42+
},
43+
},
44+
errors: [],
45+
},
46+
{
47+
name: 'invalid methods',
48+
document: {
49+
paths: {
50+
'/resourceOne': { get: { responses: {} } },
51+
'/resourceTwo': {
52+
get: {
53+
responses: {
54+
201: {},
55+
400: {},
56+
500: {},
57+
},
58+
},
59+
},
60+
'/resourceThree': {
61+
get: {
62+
responses: {
63+
400: {},
64+
500: {},
65+
},
66+
},
67+
},
68+
'/resourceFour': {
69+
get: {
70+
responses: {
71+
200: {},
72+
201: {},
73+
400: {},
74+
500: {},
75+
},
76+
},
77+
},
78+
},
79+
},
80+
errors: [
81+
{
82+
code: 'xgen-IPA-105-list-method-response-code-is-200',
83+
message:
84+
'The List method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/105',
85+
path: ['paths', '/resourceOne', 'get'],
86+
severity: DiagnosticSeverity.Warning,
87+
},
88+
{
89+
code: 'xgen-IPA-105-list-method-response-code-is-200',
90+
message:
91+
'The List method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/105',
92+
path: ['paths', '/resourceTwo', 'get'],
93+
severity: DiagnosticSeverity.Warning,
94+
},
95+
{
96+
code: 'xgen-IPA-105-list-method-response-code-is-200',
97+
message:
98+
'The List method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/105',
99+
path: ['paths', '/resourceThree', 'get'],
100+
severity: DiagnosticSeverity.Warning,
101+
},
102+
{
103+
code: 'xgen-IPA-105-list-method-response-code-is-200',
104+
message:
105+
'The List method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code. http://go/ipa/105',
106+
path: ['paths', '/resourceFour', 'get'],
107+
severity: DiagnosticSeverity.Warning,
108+
},
109+
],
110+
},
111+
{
112+
name: 'invalid method with exception',
113+
document: {
114+
paths: {
115+
'/resourceOne': {
116+
get: {
117+
'x-xgen-IPA-exception': {
118+
'xgen-IPA-105-list-method-response-code-is-200': 'reason',
119+
},
120+
responses: {},
121+
},
122+
},
123+
'/resourceTwo': {
124+
get: {
125+
'x-xgen-IPA-exception': {
126+
'xgen-IPA-105-list-method-response-code-is-200': 'reason',
127+
},
128+
responses: {
129+
201: {},
130+
400: {},
131+
500: {},
132+
},
133+
},
134+
},
135+
'/resourceThree': {
136+
get: {
137+
'x-xgen-IPA-exception': {
138+
'xgen-IPA-105-list-method-response-code-is-200': 'reason',
139+
},
140+
responses: {
141+
400: {},
142+
500: {},
143+
},
144+
},
145+
},
146+
'/resourceFour': {
147+
get: {
148+
'x-xgen-IPA-exception': {
149+
'xgen-IPA-105-list-method-response-code-is-200': 'reason',
150+
},
151+
responses: {
152+
200: {},
153+
201: {},
154+
400: {},
155+
500: {},
156+
},
157+
},
158+
},
159+
},
160+
},
161+
errors: [],
162+
},
163+
]);

tools/spectral/ipa/ipa-spectral.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
extends:
2+
- ./rulesets/IPA-005.yaml
23
- ./rulesets/IPA-102.yaml
34
- ./rulesets/IPA-104.yaml
4-
- ./rulesets/IPA-005.yaml
5+
- ./rulesets/IPA-105.yaml
6+
- ./rulesets/IPA-106.yaml
7+
- ./rulesets/IPA-108.yaml
58
- ./rulesets/IPA-109.yaml
69
- ./rulesets/IPA-113.yaml
710
- ./rulesets/IPA-123.yaml
8-
- ./rulesets/IPA-106.yaml
9-
- ./rulesets/IPA-108.yaml
1011

1112
overrides:
1213
- files:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# IPA-105: List
2+
# http://go/ipa/105
3+
4+
functions:
5+
- listResponseCodeShouldBe200OK
6+
7+
rules:
8+
xgen-IPA-105-list-method-response-code-is-200:
9+
description: 'The List method must return a 200 OK response. http://go/ipa/105'
10+
message: '{{error}} http://go/ipa/105'
11+
severity: warn
12+
given: '$.paths[*].get'
13+
then:
14+
function: 'listResponseCodeShouldBe200OK'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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-response-code-is-200';
10+
const ERROR_MESSAGE =
11+
'The List method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code.';
12+
13+
export default (input, _, { path, documentInventory }) => {
14+
const resourcePath = path[1];
15+
const oas = documentInventory.resolved;
16+
17+
if (
18+
!isResourceCollectionIdentifier(resourcePath) ||
19+
(isResourceCollectionIdentifier(resourcePath) && isSingletonResource(getResourcePathItems(resourcePath, oas.paths)))
20+
) {
21+
return;
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+
return collectAdoption(path, RULE_NAME);
34+
};
35+
36+
function checkViolationsAndReturnErrors(input, path) {
37+
if (input['responses']) {
38+
const responses = input['responses'];
39+
40+
// If there is no 200 response, return a violation
41+
if (!responses['200']) {
42+
return [{ path, message: ERROR_MESSAGE }];
43+
}
44+
45+
// If there are other 2xx responses that are not 200, return a violation
46+
if (Object.keys(responses).some((key) => key.startsWith('2') && key !== '200')) {
47+
return [{ path, message: ERROR_MESSAGE }];
48+
}
49+
}
50+
return [];
51+
}

0 commit comments

Comments
 (0)