Skip to content

Commit 12003f8

Browse files
rule name fix
1 parent bd7456a commit 12003f8

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

tools/spectral/ipa/__tests__/getResponseCodeShouldBe200OK.test.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,40 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
6767
},
6868
},
6969
},
70+
'/resource3/{id}': {
71+
get: {
72+
responses: {
73+
200: {},
74+
201: {},
75+
400: {},
76+
500: {},
77+
},
78+
},
79+
},
7080
},
7181
},
7282
errors: [
7383
{
74-
code: 'xgen-IPA-104-get-method-response-code-should-be-200-OK',
75-
message: 'The HTTP response status code for get operations should be 200 OK. http://go/ipa/104',
84+
code: 'xgen-IPA-104-get-method-response-code-is-200-OK',
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',
7687
path: ['paths', '/resource1/{id}', 'get'],
7788
severity: DiagnosticSeverity.Warning,
7889
},
7990
{
80-
code: 'xgen-IPA-104-get-method-response-code-should-be-200-OK',
81-
message: 'The HTTP response status code for get operations should be 200 OK. http://go/ipa/104',
91+
code: 'xgen-IPA-104-get-method-response-code-is-200-OK',
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',
8294
path: ['paths', '/resource2/{id}', 'get'],
8395
severity: DiagnosticSeverity.Warning,
8496
},
97+
{
98+
code: 'xgen-IPA-104-get-method-response-code-is-200-OK',
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+
},
85104
],
86105
},
87106
{
@@ -96,7 +115,7 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
96115
500: {},
97116
},
98117
'x-xgen-IPA-exception': {
99-
'xgen-IPA-104-get-method-response-code-should-be-200-OK': 'reason',
118+
'xgen-IPA-104-get-method-response-code-is-200-OK': 'reason',
100119
},
101120
},
102121
},
@@ -107,7 +126,7 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
107126
500: {},
108127
},
109128
'x-xgen-IPA-exception': {
110-
'xgen-IPA-104-get-method-response-code-should-be-200-OK': 'reason',
129+
'xgen-IPA-104-get-method-response-code-is-200-OK': 'reason',
111130
},
112131
},
113132
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ rules:
2424
function: 'getMethodReturnsSingleResource'
2525

2626
xgen-IPA-104-get-method-response-code-is-200-OK:
27-
description: 'The HTTP response status code for GET operations should be 200 OK. http://go/ipa/104'
27+
description: 'The Get method must return a 200 OK response. http://go/ipa/104'
2828
message: '{{error}} http://go/ipa/104'
2929
severity: warn
3030
given: '$.paths[*].get'

tools/spectral/ipa/rulesets/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +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-OK | The HTTP response status code for GET operations should be 200 OK. http://go/ipa/104 | warn |
35+
| xgen-IPA-104-get-method-response-code-is-200-OK | The Get method must return a 200 OK response. http://go/ipa/104 | warn |
3636

3737
### IPA-109
3838

@@ -58,3 +58,5 @@ For rule definitions, see [IPA-123.yaml](https://github.com/mongodb/openapi/blob
5858
| Rule Name | Description | Severity |
5959
| ------------------------------------------------- | ------------------------------------------------------- | -------- |
6060
| xgen-IPA-123-enum-values-must-be-upper-snake-case | Enum values must be UPPER_SNAKE_CASE. http://go/ipa/123 | warn |
61+
62+

tools/spectral/ipa/rulesets/functions/getResponseCodeShouldBe200OK.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@ import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
33

44
const RULE_NAME = 'xgen-IPA-104-get-method-response-code-is-200-OK';
5-
const ERROR_MESSAGE = 'The HTTP response status code for GET operations should be 200 OK.';
5+
const ERROR_MESSAGE =
6+
'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code.';
67

78
export default (input, _, { path }) => {
8-
console.log(input);
9-
109
if (hasException(input, RULE_NAME)) {
1110
collectException(input, RULE_NAME, path);
1211
return;
1312
}
1413

1514
if (input['responses']) {
1615
const responses = input['responses'];
16+
17+
// If there is no 200 response, return a violation
1718
if (!responses['200']) {
1819
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
1920
}
21+
22+
// If there are other 2xx responses that are not 200, return a violation
23+
if (Object.keys(responses).some((key) => key.startsWith('2') && key !== '200')) {
24+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
25+
}
2026
}
21-
console.log('adoption');
27+
2228
collectAdoption(path, RULE_NAME);
2329
};

tools/spectral/ipa/rulesets/functions/utils/exceptions.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export const EXCEPTION_EXTENSION = 'x-xgen-IPA-exception';
99
*/
1010
export function hasException(object, ruleName) {
1111
if (object[EXCEPTION_EXTENSION]) {
12-
console.log('exception');
1312
return Object.keys(object[EXCEPTION_EXTENSION]).includes(ruleName);
1413
}
1514
return false;

0 commit comments

Comments
 (0)