Skip to content

Commit 351a648

Browse files
CLOUDP-304944: Adds tests and refactoring
1 parent 2c6fff8 commit 351a648

9 files changed

+662
-318
lines changed
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
const componentSchemas = {
5+
schemas: {
6+
SchemaWithReadOnly: {
7+
type: 'object',
8+
properties: {
9+
id: {
10+
type: 'string',
11+
readOnly: true,
12+
},
13+
name: {
14+
type: 'string',
15+
},
16+
},
17+
},
18+
SchemaWithoutReadOnly: {
19+
type: 'object',
20+
properties: {
21+
name: {
22+
type: 'string',
23+
},
24+
description: {
25+
type: 'string',
26+
},
27+
},
28+
},
29+
NestedSchemaWithReadOnly: {
30+
type: 'object',
31+
properties: {
32+
user: {
33+
type: 'object',
34+
properties: {
35+
userId: {
36+
type: 'string',
37+
readOnly: true,
38+
},
39+
username: {
40+
type: 'string',
41+
},
42+
},
43+
},
44+
},
45+
},
46+
ArraySchemaWithReadOnly: {
47+
type: 'object',
48+
properties: {
49+
items: {
50+
type: 'array',
51+
items: {
52+
type: 'object',
53+
properties: {
54+
itemId: {
55+
type: 'string',
56+
readOnly: true,
57+
},
58+
itemName: {
59+
type: 'string',
60+
},
61+
},
62+
},
63+
},
64+
},
65+
},
66+
},
67+
};
68+
69+
testRule('xgen-IPA-107-update-method-request-has-no-readonly-fields', [
70+
{
71+
name: 'valid methods - no readOnly fields',
72+
document: {
73+
components: componentSchemas,
74+
paths: {
75+
'/resource/{id}': {
76+
patch: {
77+
requestBody: {
78+
content: {
79+
'application/vnd.atlas.2023-01-01+json': {
80+
schema: {
81+
$ref: '#/components/schemas/SchemaWithoutReadOnly',
82+
},
83+
},
84+
'application/vnd.atlas.2024-01-01+json': {
85+
schema: {
86+
type: 'string',
87+
},
88+
},
89+
},
90+
},
91+
},
92+
put: {
93+
requestBody: {
94+
content: {
95+
'application/vnd.atlas.2023-01-01+json': {
96+
schema: {
97+
$ref: '#/components/schemas/SchemaWithoutReadOnly',
98+
},
99+
},
100+
'application/vnd.atlas.2024-01-01+json': {
101+
schema: {
102+
type: 'string',
103+
},
104+
},
105+
},
106+
},
107+
},
108+
},
109+
},
110+
},
111+
errors: [],
112+
},
113+
{
114+
name: 'invalid methods - direct readOnly field',
115+
document: {
116+
components: componentSchemas,
117+
paths: {
118+
'/resource/{id}': {
119+
patch: {
120+
requestBody: {
121+
content: {
122+
'application/vnd.atlas.2023-01-01+json': {
123+
schema: {
124+
$ref: '#/components/schemas/SchemaWithReadOnly',
125+
},
126+
},
127+
'application/vnd.atlas.2024-01-01+json': {
128+
schema: {
129+
type: 'string',
130+
readOnly: true,
131+
},
132+
},
133+
},
134+
},
135+
},
136+
put: {
137+
requestBody: {
138+
content: {
139+
'application/vnd.atlas.2023-01-01+json': {
140+
schema: {
141+
$ref: '#/components/schemas/SchemaWithReadOnly',
142+
},
143+
},
144+
'application/vnd.atlas.2024-01-01+json': {
145+
schema: {
146+
type: 'string',
147+
readOnly: true,
148+
},
149+
},
150+
},
151+
},
152+
},
153+
},
154+
},
155+
},
156+
errors: [
157+
{
158+
code: 'xgen-IPA-107-update-method-request-has-no-readonly-fields',
159+
message:
160+
'The Update method request object must not include input fields (readOnly properties). Found readOnly property at: id. ',
161+
path: ['paths', '/resource/{id}', 'patch', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
162+
severity: DiagnosticSeverity.Warning,
163+
},
164+
{
165+
code: 'xgen-IPA-107-update-method-request-has-no-readonly-fields',
166+
message:
167+
'The Update method request object must not include input fields (readOnly properties). Found readOnly property at one of the inline schemas. ',
168+
path: ['paths', '/resource/{id}', 'patch', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
169+
severity: DiagnosticSeverity.Warning,
170+
},
171+
{
172+
code: 'xgen-IPA-107-update-method-request-has-no-readonly-fields',
173+
message:
174+
'The Update method request object must not include input fields (readOnly properties). Found readOnly property at: id. ',
175+
path: ['paths', '/resource/{id}', 'put', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
176+
severity: DiagnosticSeverity.Warning,
177+
},
178+
{
179+
code: 'xgen-IPA-107-update-method-request-has-no-readonly-fields',
180+
message:
181+
'The Update method request object must not include input fields (readOnly properties). Found readOnly property at one of the inline schemas. ',
182+
path: ['paths', '/resource/{id}', 'put', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'],
183+
severity: DiagnosticSeverity.Warning,
184+
},
185+
],
186+
},
187+
{
188+
name: 'invalid methods - nested readOnly field',
189+
document: {
190+
components: componentSchemas,
191+
paths: {
192+
'/resource/{id}': {
193+
patch: {
194+
requestBody: {
195+
content: {
196+
'application/vnd.atlas.2023-01-01+json': {
197+
schema: {
198+
$ref: '#/components/schemas/NestedSchemaWithReadOnly',
199+
},
200+
},
201+
},
202+
},
203+
},
204+
},
205+
},
206+
},
207+
errors: [
208+
{
209+
code: 'xgen-IPA-107-update-method-request-has-no-readonly-fields',
210+
message:
211+
'The Update method request object must not include input fields (readOnly properties). Found readOnly property at: user.userId. ',
212+
path: ['paths', '/resource/{id}', 'patch', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
213+
severity: DiagnosticSeverity.Warning,
214+
},
215+
],
216+
},
217+
{
218+
name: 'invalid methods - array with readOnly field',
219+
document: {
220+
components: componentSchemas,
221+
paths: {
222+
'/resource/{id}': {
223+
patch: {
224+
requestBody: {
225+
content: {
226+
'application/vnd.atlas.2023-01-01+json': {
227+
schema: {
228+
$ref: '#/components/schemas/ArraySchemaWithReadOnly',
229+
},
230+
},
231+
},
232+
},
233+
},
234+
},
235+
},
236+
},
237+
errors: [
238+
{
239+
code: 'xgen-IPA-107-update-method-request-has-no-readonly-fields',
240+
message:
241+
'The Update method request object must not include input fields (readOnly properties). Found readOnly property at: items.items.itemId. ',
242+
path: ['paths', '/resource/{id}', 'patch', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'],
243+
severity: DiagnosticSeverity.Warning,
244+
},
245+
],
246+
},
247+
{
248+
name: 'methods with exceptions',
249+
document: {
250+
components: componentSchemas,
251+
paths: {
252+
'/resource': {
253+
post: {
254+
requestBody: {
255+
content: {
256+
'application/vnd.atlas.2023-01-01+json': {
257+
schema: {
258+
$ref: '#/components/schemas/SchemaWithReadOnly',
259+
},
260+
'x-xgen-IPA-exception': {
261+
'xgen-IPA-107-update-method-request-has-no-readonly-fields': 'Reason',
262+
},
263+
},
264+
},
265+
},
266+
},
267+
},
268+
'/resource/{id}': {
269+
patch: {
270+
requestBody: {
271+
content: {
272+
'application/vnd.atlas.2023-01-01+json': {
273+
schema: {
274+
$ref: '#/components/schemas/SchemaWithReadOnly',
275+
},
276+
'x-xgen-IPA-exception': {
277+
'xgen-IPA-107-update-method-request-has-no-readonly-fields': 'Reason',
278+
},
279+
},
280+
},
281+
},
282+
},
283+
put: {
284+
requestBody: {
285+
content: {
286+
'application/vnd.atlas.2023-01-01+json': {
287+
schema: {
288+
$ref: '#/components/schemas/SchemaWithReadOnly',
289+
},
290+
'x-xgen-IPA-exception': {
291+
'xgen-IPA-107-update-method-request-has-no-readonly-fields': 'Reason',
292+
},
293+
},
294+
},
295+
},
296+
},
297+
},
298+
},
299+
},
300+
errors: [],
301+
},
302+
]);

0 commit comments

Comments
 (0)