Skip to content

Commit 0f7c74c

Browse files
CLOUDP-271991: IPA-104: Validate for Get methods the response is 200
1 parent 50357b8 commit 0f7c74c

File tree

3 files changed

+139
-0
lines changed

3 files changed

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

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

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

44
functions:
55
- eachResourceHasGetMethod
6+
- getResponseCodeShouldBe200OK
67

78
rules:
89
xgen-IPA-104-resource-has-GET:
@@ -13,3 +14,12 @@ rules:
1314
then:
1415
field: '@key'
1516
function: 'eachResourceHasGetMethod'
17+
18+
xgen-IPA-104-GET-response-code-should-be-200-OK:
19+
description: 'GET method response code should be 200 OK. http://go/ipa/104'
20+
message: '{{error}} http://go/ipa/104'
21+
severity: warn
22+
given: '$.paths'
23+
then:
24+
field: '@key'
25+
function: 'getResponseCodeShouldBe200OK'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
4+
const RULE_NAME = 'xgen-IPA-104-GET-response-code-should-be-200-OK';
5+
const ERROR_MESSAGE = 'GET method response code should be 200 OK.';
6+
7+
export default (input, _, { path, documentInventory }) => {
8+
9+
const oas = documentInventory.resolved;
10+
11+
if (hasException(oas.paths[input], RULE_NAME)) {
12+
collectException(oas.paths[input], RULE_NAME, path);
13+
return;
14+
}
15+
16+
let pathItem = oas.paths[input];
17+
if(!pathItem.get) {
18+
return;
19+
}
20+
21+
if(pathItem.get && pathItem.get.responses) {
22+
const responses = pathItem.get.responses;
23+
if(!responses['200']) {
24+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
25+
}
26+
}
27+
28+
collectAdoption(path, RULE_NAME);
29+
};

0 commit comments

Comments
 (0)