Skip to content

Commit b365638

Browse files
CLOUDP-271994: IPA-106: Validate Create methods accepts no query params (#485)
1 parent 9d80e48 commit b365638

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
const componentSchemas = {
5+
schemas: {
6+
Schema: {
7+
type: 'object',
8+
},
9+
},
10+
parameters: {
11+
QueryParam: {
12+
name: 'query-param',
13+
in: 'query',
14+
schema: {
15+
type: 'string',
16+
},
17+
},
18+
PathParam: {
19+
name: 'resource-id',
20+
in: 'path',
21+
schema: {
22+
type: 'string',
23+
},
24+
},
25+
},
26+
};
27+
28+
testRule('xgen-IPA-106-create-method-should-not-have-query-parameters', [
29+
{
30+
name: 'valid methods',
31+
document: {
32+
components: componentSchemas,
33+
paths: {
34+
'/resource': {
35+
post: {
36+
parameters: [
37+
{
38+
name: 'header-param',
39+
in: 'header',
40+
schema: { type: 'string' },
41+
},
42+
{
43+
name: 'resource-id',
44+
in: 'path',
45+
schema: {
46+
$ref: '#/components/schemas/Schema',
47+
},
48+
},
49+
{
50+
$ref: '#/components/parameters/PathParam',
51+
},
52+
],
53+
},
54+
},
55+
'/resource2': {
56+
post: {
57+
parameters: [],
58+
},
59+
},
60+
},
61+
},
62+
errors: [],
63+
},
64+
{
65+
name: 'invalid methods',
66+
document: {
67+
components: componentSchemas,
68+
paths: {
69+
'/resource': {
70+
post: {
71+
parameters: [
72+
{
73+
name: 'filter',
74+
in: 'query',
75+
schema: { type: 'string' },
76+
},
77+
],
78+
},
79+
},
80+
'/resource2': {
81+
post: {
82+
parameters: [
83+
{
84+
name: 'header-param',
85+
in: 'header',
86+
schema: { type: 'string' },
87+
},
88+
{
89+
$ref: '#/components/parameters/QueryParam',
90+
},
91+
],
92+
},
93+
},
94+
},
95+
},
96+
errors: [
97+
{
98+
code: 'xgen-IPA-106-create-method-should-not-have-query-parameters',
99+
message: 'Create operations should not have query parameters. http://go/ipa/106',
100+
path: ['paths', '/resource', 'post'],
101+
severity: DiagnosticSeverity.Warning,
102+
},
103+
{
104+
code: 'xgen-IPA-106-create-method-should-not-have-query-parameters',
105+
message: 'Create operations should not have query parameters. http://go/ipa/106',
106+
path: ['paths', '/resource2', 'post'],
107+
severity: DiagnosticSeverity.Warning,
108+
},
109+
],
110+
},
111+
{
112+
name: 'invalid methods with exceptions',
113+
document: {
114+
components: componentSchemas,
115+
paths: {
116+
'/resource': {
117+
post: {
118+
parameters: [
119+
{
120+
name: 'filter',
121+
in: 'query',
122+
schema: { type: 'string' },
123+
},
124+
],
125+
'x-xgen-IPA-exception': {
126+
'xgen-IPA-106-create-method-should-not-have-query-parameters': 'Reason',
127+
},
128+
},
129+
},
130+
'/resource2': {
131+
post: {
132+
parameters: [
133+
{
134+
$ref: '#/components/parameters/QueryParam',
135+
},
136+
],
137+
'x-xgen-IPA-exception': {
138+
'xgen-IPA-106-create-method-should-not-have-query-parameters': 'Reason',
139+
},
140+
},
141+
},
142+
},
143+
},
144+
errors: [],
145+
},
146+
]);

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

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

44
functions:
55
- createMethodRequestBodyIsRequestSuffixedObject
6+
- createMethodShouldNotHaveQueryParameters
67

78
rules:
89
xgen-IPA-106-create-method-request-body-is-request-suffixed-object:
@@ -13,3 +14,10 @@ rules:
1314
then:
1415
field: '@key'
1516
function: 'createMethodRequestBodyIsRequestSuffixedObject'
17+
xgen-IPA-106-create-method-should-not-have-query-parameters:
18+
description: 'Create operations should not use query parameters. http://go/ipa/xxx'
19+
message: '{{error}} http://go/ipa/106'
20+
severity: warn
21+
given: '$.paths[*].post'
22+
then:
23+
function: 'createMethodShouldNotHaveQueryParameters'

tools/spectral/ipa/rulesets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ For rule definitions, see [IPA-106.yaml](https://github.com/mongodb/openapi/blob
4141
| Rule Name | Description | Severity |
4242
| ------------------------------------------------------------------ | -------------------------------------------------------------------------------- | -------- |
4343
| xgen-IPA-106-create-method-request-body-is-request-suffixed-object | The Create method request should be a Request suffixed object. http://go/ipa/106 | warn |
44+
| xgen-IPA-106-create-method-should-not-have-query-parameters | Create operations should not use query parameters. http://go/ipa/xxx | warn |
4445

4546
### IPA-109
4647

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
import { isCustomMethodIdentifier } from './utils/resourceEvaluation.js';
4+
5+
const RULE_NAME = 'xgen-IPA-106-create-method-should-not-have-query-parameters';
6+
const ERROR_MESSAGE = 'Create operations should not have query parameters.';
7+
8+
export default (input, _, { path }) => {
9+
const resourcePath = path[1];
10+
11+
if (isCustomMethodIdentifier(resourcePath)) {
12+
return;
13+
}
14+
15+
const postMethod = input;
16+
if (!postMethod.parameters || postMethod.parameters.length === 0) {
17+
return;
18+
}
19+
20+
if (hasException(postMethod, RULE_NAME)) {
21+
collectException(postMethod, RULE_NAME, path);
22+
return;
23+
}
24+
25+
for (const parameter of postMethod.parameters) {
26+
if (parameter.in === 'query') {
27+
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE);
28+
}
29+
}
30+
31+
collectAdoption(path, RULE_NAME);
32+
};

0 commit comments

Comments
 (0)