Skip to content

Commit d210b0f

Browse files
address the comments
1 parent 12003f8 commit d210b0f

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import testRule from './__helpers__/testRule';
22
import { DiagnosticSeverity } from '@stoplight/types';
33

4-
testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
4+
testRule('xgen-IPA-104-get-method-response-code-is-200', [
55
{
66
name: 'valid methods',
77
document: {
88
paths: {
99
'/resource': {
1010
get: {
1111
responses: {
12-
200: {},
1312
400: {},
1413
500: {},
1514
},
@@ -27,7 +26,6 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
2726
'/resource/{id}:customMethod': {
2827
get: {
2928
responses: {
30-
200: {},
3129
400: {},
3230
500: {},
3331
},
@@ -36,7 +34,6 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
3634
'/singleton': {
3735
get: {
3836
responses: {
39-
200: {},
4037
400: {},
4138
500: {},
4239
},
@@ -50,6 +47,7 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
5047
name: 'invalid methods',
5148
document: {
5249
paths: {
50+
'/resource1': { get: { responses: {}}},
5351
'/resource1/{id}': {
5452
get: {
5553
responses: {
@@ -59,6 +57,7 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
5957
},
6058
},
6159
},
60+
'/resource2': { get: { responses: {} }},
6261
'/resource2/{id}': {
6362
get: {
6463
responses: {
@@ -67,6 +66,7 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
6766
},
6867
},
6968
},
69+
'/resource3': { get: { responses: {}}},
7070
'/resource3/{id}': {
7171
get: {
7272
responses: {
@@ -81,21 +81,21 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
8181
},
8282
errors: [
8383
{
84-
code: 'xgen-IPA-104-get-method-response-code-is-200-OK',
84+
code: 'xgen-IPA-104-get-method-response-code-is-200',
8585
message:
8686
'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',
8787
path: ['paths', '/resource1/{id}', 'get'],
8888
severity: DiagnosticSeverity.Warning,
8989
},
9090
{
91-
code: 'xgen-IPA-104-get-method-response-code-is-200-OK',
91+
code: 'xgen-IPA-104-get-method-response-code-is-200',
9292
message:
9393
'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',
9494
path: ['paths', '/resource2/{id}', 'get'],
9595
severity: DiagnosticSeverity.Warning,
9696
},
9797
{
98-
code: 'xgen-IPA-104-get-method-response-code-is-200-OK',
98+
code: 'xgen-IPA-104-get-method-response-code-is-200',
9999
message:
100100
'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',
101101
path: ['paths', '/resource3/{id}', 'get'],
@@ -107,6 +107,7 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
107107
name: 'invalid method with exception',
108108
document: {
109109
paths: {
110+
'/resource1': { get: {responses: {}}},
110111
'/resource1/{id}': {
111112
get: {
112113
responses: {
@@ -115,18 +116,19 @@ testRule('xgen-IPA-104-get-method-response-code-is-200-OK', [
115116
500: {},
116117
},
117118
'x-xgen-IPA-exception': {
118-
'xgen-IPA-104-get-method-response-code-is-200-OK': 'reason',
119+
'xgen-IPA-104-get-method-response-code-is-200': 'reason',
119120
},
120121
},
121122
},
123+
'/resource2': { get: {responses: {}}},
122124
'/resource2/{id}': {
123125
get: {
124126
responses: {
125127
400: {},
126128
500: {},
127129
},
128130
'x-xgen-IPA-exception': {
129-
'xgen-IPA-104-get-method-response-code-is-200-OK': 'reason',
131+
'xgen-IPA-104-get-method-response-code-is-200': 'reason',
130132
},
131133
},
132134
},

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

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

26-
xgen-IPA-104-get-method-response-code-is-200-OK:
26+
xgen-IPA-104-get-method-response-code-is-200:
2727
description: 'The Get method must return a 200 OK response. http://go/ipa/104'
2828
message: '{{error}} http://go/ipa/104'
2929
severity: warn

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { hasException } from './utils/exceptions.js';
22
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
import {
4+
isChild,
5+
isCustomMethod,
6+
} from './utils/resourceEvaluation.js';
37

4-
const RULE_NAME = 'xgen-IPA-104-get-method-response-code-is-200-OK';
8+
const RULE_NAME = 'xgen-IPA-104-get-method-response-code-is-200';
59
const ERROR_MESSAGE =
610
'The Get method must return a 200 OK response. This method either lacks a 200 OK response or defines a different 2xx status code.';
711

812
export default (input, _, { path }) => {
13+
const resourcePath = path[1];
14+
15+
if (isCustomMethod(resourcePath) || !isChild(resourcePath)) {
16+
return;
17+
}
18+
919
if (hasException(input, RULE_NAME)) {
1020
collectException(input, RULE_NAME, path);
1121
return;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { schemaIsArray, schemaIsPaginated } from './schemaUtils.js';
2+
13
/**
24
* Returns a list of all successful response schemas for the passed operation, i.e. for any 2xx response.
35
*
@@ -29,3 +31,7 @@ export function getAllSuccessfulResponseSchemas(operationObject) {
2931
});
3032
return result;
3133
}
34+
35+
export function returnsSingleResource(schema) {
36+
return !schemaIsArray(schema) && !schemaIsPaginated(schema);
37+
}

0 commit comments

Comments
 (0)