Skip to content

Commit e64b541

Browse files
CLOUDP-271991: IPA-104: Validate for Get methods the response is 200 (#462)
1 parent 5db921b commit e64b541

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-104-get-method-response-code-is-200', [
5+
{
6+
name: 'valid methods',
7+
document: {
8+
paths: {
9+
'/resource': {
10+
get: {
11+
responses: {
12+
400: {},
13+
500: {},
14+
},
15+
},
16+
},
17+
'/resource/{id}': {
18+
get: {
19+
responses: {
20+
200: {},
21+
400: {},
22+
500: {},
23+
},
24+
},
25+
},
26+
'/resource/{id}:customMethod': {
27+
get: {
28+
responses: {
29+
400: {},
30+
500: {},
31+
},
32+
},
33+
},
34+
'/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+
'/resource1': { get: { responses: {} } },
51+
'/resource1/{id}': {
52+
get: {
53+
responses: {
54+
201: {},
55+
400: {},
56+
500: {},
57+
},
58+
},
59+
},
60+
'/resource2': { get: { responses: {} } },
61+
'/resource2/{id}': {
62+
get: {
63+
responses: {
64+
400: {},
65+
500: {},
66+
},
67+
},
68+
},
69+
'/resource3': { get: { responses: {} } },
70+
'/resource3/{id}': {
71+
get: {
72+
responses: {
73+
200: {},
74+
201: {},
75+
400: {},
76+
500: {},
77+
},
78+
},
79+
},
80+
},
81+
},
82+
errors: [
83+
{
84+
code: 'xgen-IPA-104-get-method-response-code-is-200',
85+
message:
86+
'The Get 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/104',
87+
path: ['paths', '/resource1/{id}', 'get'],
88+
severity: DiagnosticSeverity.Warning,
89+
},
90+
{
91+
code: 'xgen-IPA-104-get-method-response-code-is-200',
92+
message:
93+
'The Get 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/104',
94+
path: ['paths', '/resource2/{id}', 'get'],
95+
severity: DiagnosticSeverity.Warning,
96+
},
97+
{
98+
code: 'xgen-IPA-104-get-method-response-code-is-200',
99+
message:
100+
'The Get 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/104',
101+
path: ['paths', '/resource3/{id}', 'get'],
102+
severity: DiagnosticSeverity.Warning,
103+
},
104+
],
105+
},
106+
{
107+
name: 'invalid method with exception',
108+
document: {
109+
paths: {
110+
'/resource1': { get: { responses: {} } },
111+
'/resource1/{id}': {
112+
get: {
113+
responses: {
114+
201: {},
115+
400: {},
116+
500: {},
117+
},
118+
'x-xgen-IPA-exception': {
119+
'xgen-IPA-104-get-method-response-code-is-200': 'reason',
120+
},
121+
},
122+
},
123+
'/resource2': { get: { responses: {} } },
124+
'/resource2/{id}': {
125+
get: {
126+
responses: {
127+
400: {},
128+
500: {},
129+
},
130+
'x-xgen-IPA-exception': {
131+
'xgen-IPA-104-get-method-response-code-is-200': 'reason',
132+
},
133+
},
134+
},
135+
},
136+
},
137+
errors: [],
138+
},
139+
]);

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

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

89
rules:
910
xgen-IPA-104-resource-has-GET:
@@ -21,3 +22,11 @@ rules:
2122
given: '$.paths[*].get'
2223
then:
2324
function: 'getMethodReturnsSingleResource'
25+
26+
xgen-IPA-104-get-method-response-code-is-200:
27+
description: 'The Get method must return a 200 OK response. http://go/ipa/104'
28+
message: '{{error}} http://go/ipa/104'
29+
severity: warn
30+
given: '$.paths[*].get'
31+
then:
32+
function: 'getResponseCodeShouldBe200OK'

tools/spectral/ipa/rulesets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ For rule definitions, see [IPA-104.yaml](https://github.com/mongodb/openapi/blob
3232
| ----------------------------------------------- | ----------------------------------------------------------------------------------------- | -------- |
3333
| xgen-IPA-104-resource-has-GET | APIs must provide a get method for resources. http://go/ipa/104 | warn |
3434
| xgen-IPA-104-get-method-returns-single-resource | The purpose of the get method is to return data from a single resource. http://go/ipa/104 | warn |
35+
| xgen-IPA-104-get-method-response-code-is-200 | The Get method must return a 200 OK response. http://go/ipa/104 | warn |
3536

3637
### IPA-109
3738

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
import { isChild, isCustomMethod } from './utils/resourceEvaluation.js';
4+
5+
const RULE_NAME = 'xgen-IPA-104-get-method-response-code-is-200';
6+
const ERROR_MESSAGE =
7+
'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code.';
8+
9+
export default (input, _, { path }) => {
10+
const resourcePath = path[1];
11+
12+
if (isCustomMethod(resourcePath) || !isChild(resourcePath)) {
13+
return;
14+
}
15+
16+
if (hasException(input, RULE_NAME)) {
17+
collectException(input, RULE_NAME, path);
18+
return;
19+
}
20+
21+
if (input['responses']) {
22+
const responses = input['responses'];
23+
24+
// If there is no 200 response, return a violation
25+
if (!responses['200']) {
26+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
27+
}
28+
29+
// If there are other 2xx responses that are not 200, return a violation
30+
if (Object.keys(responses).some((key) => key.startsWith('2') && key !== '200')) {
31+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
32+
}
33+
}
34+
35+
collectAdoption(path, RULE_NAME);
36+
};

0 commit comments

Comments
 (0)