@redocly/openapi-core 1.0.0-beta.102 → 1.0.0-beta.105
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/utils.ts +3 -1
- package/lib/config/config.d.ts +4 -3
- package/lib/config/config.js +23 -16
- package/lib/config/load.d.ts +1 -1
- package/lib/config/load.js +15 -3
- package/lib/config/rules.d.ts +1 -1
- package/lib/config/types.d.ts +4 -2
- package/lib/decorators/common/filters/filter-helper.d.ts +3 -0
- package/lib/decorators/common/filters/filter-helper.js +67 -0
- package/lib/decorators/common/filters/filter-in.d.ts +2 -0
- package/lib/decorators/common/filters/filter-in.js +17 -0
- package/lib/decorators/common/filters/filter-out.d.ts +2 -0
- package/lib/decorators/common/filters/filter-out.js +17 -0
- package/lib/decorators/oas2/index.d.ts +2 -0
- package/lib/decorators/oas2/index.js +5 -1
- package/lib/decorators/oas3/index.d.ts +2 -0
- package/lib/decorators/oas3/index.js +5 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/lib/lint.d.ts +2 -0
- package/lib/lint.js +2 -2
- package/lib/redocly/registry-api-types.d.ts +2 -0
- package/lib/redocly/registry-api.d.ts +1 -1
- package/lib/redocly/registry-api.js +3 -1
- package/lib/rules/ajv.d.ts +1 -1
- package/lib/rules/ajv.js +1 -1
- package/lib/rules/common/assertions/asserts.d.ts +6 -1
- package/lib/rules/common/assertions/asserts.js +81 -51
- package/lib/rules/common/assertions/utils.d.ts +2 -1
- package/lib/rules/common/assertions/utils.js +27 -8
- package/lib/types/redocly-yaml.js +317 -27
- package/lib/utils.d.ts +5 -3
- package/lib/utils.js +15 -2
- package/lib/walk.d.ts +4 -14
- package/lib/walk.js +35 -26
- package/package.json +3 -2
- package/src/__tests__/fixtures/.redocly.lint-ignore.yaml +5 -0
- package/src/__tests__/lint.test.ts +70 -10
- package/src/__tests__/utils.test.ts +42 -1
- package/src/config/__tests__/load.test.ts +8 -2
- package/src/config/config.ts +31 -27
- package/src/config/load.ts +29 -9
- package/src/config/types.ts +6 -5
- package/src/decorators/__tests__/filter-in.test.ts +310 -0
- package/src/decorators/__tests__/filter-out.test.ts +331 -0
- package/src/decorators/common/filters/filter-helper.ts +72 -0
- package/src/decorators/common/filters/filter-in.ts +18 -0
- package/src/decorators/common/filters/filter-out.ts +18 -0
- package/src/decorators/oas2/index.ts +5 -1
- package/src/decorators/oas3/index.ts +5 -1
- package/src/index.ts +2 -1
- package/src/lint.ts +4 -3
- package/src/redocly/registry-api-types.ts +2 -0
- package/src/redocly/registry-api.ts +4 -0
- package/src/rules/ajv.ts +4 -4
- package/src/rules/common/assertions/__tests__/asserts.test.ts +149 -146
- package/src/rules/common/assertions/asserts.ts +97 -52
- package/src/rules/common/assertions/utils.ts +41 -16
- package/src/types/redocly-yaml.ts +322 -34
- package/src/utils.ts +28 -15
- package/src/walk.ts +59 -47
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { outdent } from 'outdent';
|
|
2
|
+
import { bundleDocument } from '../../bundle';
|
|
3
|
+
import { BaseResolver } from '../../resolve';
|
|
4
|
+
import { makeConfig, parseYamlToDocument, yamlSerializer } from '../../../__tests__/utils';
|
|
5
|
+
|
|
6
|
+
describe('oas3 filter-in', () => {
|
|
7
|
+
expect.addSnapshotSerializer(yamlSerializer);
|
|
8
|
+
|
|
9
|
+
const inputDoc = parseYamlToDocument(
|
|
10
|
+
outdent`
|
|
11
|
+
openapi: 3.0.0
|
|
12
|
+
paths:
|
|
13
|
+
/pet:
|
|
14
|
+
x-audience: Global
|
|
15
|
+
post:
|
|
16
|
+
summary: test
|
|
17
|
+
/user:
|
|
18
|
+
x-audience: [Public, Global]
|
|
19
|
+
post:
|
|
20
|
+
summary: test
|
|
21
|
+
/post:
|
|
22
|
+
get:
|
|
23
|
+
summary: test
|
|
24
|
+
/order:
|
|
25
|
+
x-audience: [Public, Protected]
|
|
26
|
+
post:
|
|
27
|
+
operationId: storeOrder
|
|
28
|
+
callbacks:
|
|
29
|
+
x-access: protected`
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
it('should include /user path and remove y parameter', async () => {
|
|
33
|
+
const testDocument = parseYamlToDocument(
|
|
34
|
+
outdent`
|
|
35
|
+
openapi: 3.0.0
|
|
36
|
+
paths:
|
|
37
|
+
/pet:
|
|
38
|
+
x-access: private
|
|
39
|
+
get:
|
|
40
|
+
parameters:
|
|
41
|
+
- $ref: '#/components/parameters/x'
|
|
42
|
+
/user:
|
|
43
|
+
x-access: public
|
|
44
|
+
get:
|
|
45
|
+
parameters:
|
|
46
|
+
- $ref: '#/components/parameters/y'
|
|
47
|
+
components:
|
|
48
|
+
parameters:
|
|
49
|
+
x:
|
|
50
|
+
name: x
|
|
51
|
+
y:
|
|
52
|
+
x-access: private
|
|
53
|
+
name: y
|
|
54
|
+
`
|
|
55
|
+
);
|
|
56
|
+
const { bundle: res } = await bundleDocument({
|
|
57
|
+
document: testDocument,
|
|
58
|
+
externalRefResolver: new BaseResolver(),
|
|
59
|
+
config: await makeConfig({}, { 'filter-in': { value: 'public', property: 'x-access' } }),
|
|
60
|
+
});
|
|
61
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
62
|
+
openapi: 3.0.0
|
|
63
|
+
paths:
|
|
64
|
+
/user:
|
|
65
|
+
x-access: public
|
|
66
|
+
get: {}
|
|
67
|
+
components:
|
|
68
|
+
parameters:
|
|
69
|
+
x:
|
|
70
|
+
name: x
|
|
71
|
+
|
|
72
|
+
`);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should include /order and /post paths', async () => {
|
|
76
|
+
const { bundle: res } = await bundleDocument({
|
|
77
|
+
document: inputDoc,
|
|
78
|
+
externalRefResolver: new BaseResolver(),
|
|
79
|
+
config: await makeConfig(
|
|
80
|
+
{},
|
|
81
|
+
{
|
|
82
|
+
'filter-in': {
|
|
83
|
+
property: 'x-audience',
|
|
84
|
+
value: ['Public', 'Protected'],
|
|
85
|
+
matchStrategy: 'all',
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
),
|
|
89
|
+
});
|
|
90
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
91
|
+
openapi: 3.0.0
|
|
92
|
+
paths:
|
|
93
|
+
/post:
|
|
94
|
+
get:
|
|
95
|
+
summary: test
|
|
96
|
+
/order:
|
|
97
|
+
x-audience:
|
|
98
|
+
- Public
|
|
99
|
+
- Protected
|
|
100
|
+
post:
|
|
101
|
+
operationId: storeOrder
|
|
102
|
+
callbacks:
|
|
103
|
+
x-access: protected
|
|
104
|
+
components: {}
|
|
105
|
+
|
|
106
|
+
`);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should include all paths', async () => {
|
|
110
|
+
const testDoc = parseYamlToDocument(
|
|
111
|
+
outdent`
|
|
112
|
+
openapi: 3.0.0
|
|
113
|
+
paths:
|
|
114
|
+
/pet:
|
|
115
|
+
x-audience: Global
|
|
116
|
+
post:
|
|
117
|
+
summary: test
|
|
118
|
+
/user:
|
|
119
|
+
x-audience: [Public, Global]
|
|
120
|
+
post:
|
|
121
|
+
summary: test
|
|
122
|
+
/order:
|
|
123
|
+
x-audience: [Public, Protected]
|
|
124
|
+
post:
|
|
125
|
+
operationId: storeOrder
|
|
126
|
+
parameters:
|
|
127
|
+
- name: api_key
|
|
128
|
+
`
|
|
129
|
+
);
|
|
130
|
+
const { bundle: res } = await bundleDocument({
|
|
131
|
+
document: testDoc,
|
|
132
|
+
externalRefResolver: new BaseResolver(),
|
|
133
|
+
config: await makeConfig(
|
|
134
|
+
{},
|
|
135
|
+
{
|
|
136
|
+
'filter-in': {
|
|
137
|
+
property: 'x-audience',
|
|
138
|
+
value: ['Public', 'Global'],
|
|
139
|
+
matchStrategy: 'any',
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
),
|
|
143
|
+
});
|
|
144
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
145
|
+
openapi: 3.0.0
|
|
146
|
+
paths:
|
|
147
|
+
/pet:
|
|
148
|
+
x-audience: Global
|
|
149
|
+
post:
|
|
150
|
+
summary: test
|
|
151
|
+
/user:
|
|
152
|
+
x-audience:
|
|
153
|
+
- Public
|
|
154
|
+
- Global
|
|
155
|
+
post:
|
|
156
|
+
summary: test
|
|
157
|
+
/order:
|
|
158
|
+
x-audience:
|
|
159
|
+
- Public
|
|
160
|
+
- Protected
|
|
161
|
+
post:
|
|
162
|
+
operationId: storeOrder
|
|
163
|
+
parameters:
|
|
164
|
+
- name: api_key
|
|
165
|
+
components: {}
|
|
166
|
+
|
|
167
|
+
`);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should include path without x-audience property ', async () => {
|
|
171
|
+
const { bundle: res } = await bundleDocument({
|
|
172
|
+
document: inputDoc,
|
|
173
|
+
externalRefResolver: new BaseResolver(),
|
|
174
|
+
config: await makeConfig(
|
|
175
|
+
{},
|
|
176
|
+
{
|
|
177
|
+
'filter-in': {
|
|
178
|
+
property: 'x-audience',
|
|
179
|
+
value: 'non-existing-audience',
|
|
180
|
+
matchStrategy: 'any',
|
|
181
|
+
},
|
|
182
|
+
}
|
|
183
|
+
),
|
|
184
|
+
});
|
|
185
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
186
|
+
openapi: 3.0.0
|
|
187
|
+
paths:
|
|
188
|
+
/post:
|
|
189
|
+
get:
|
|
190
|
+
summary: test
|
|
191
|
+
components: {}
|
|
192
|
+
|
|
193
|
+
`);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('should include /pet and /account without post method', async () => {
|
|
197
|
+
const testDoc = parseYamlToDocument(
|
|
198
|
+
outdent`
|
|
199
|
+
openapi: 3.0.0
|
|
200
|
+
paths:
|
|
201
|
+
/pet:
|
|
202
|
+
x-audience: Global
|
|
203
|
+
post:
|
|
204
|
+
summary: test
|
|
205
|
+
/user:
|
|
206
|
+
x-audience: Private
|
|
207
|
+
post:
|
|
208
|
+
summary: test
|
|
209
|
+
get:
|
|
210
|
+
summary: get
|
|
211
|
+
x-audience: [Public, Global]
|
|
212
|
+
/account:
|
|
213
|
+
get:
|
|
214
|
+
summary: get
|
|
215
|
+
post:
|
|
216
|
+
summary: test
|
|
217
|
+
x-audience: Private
|
|
218
|
+
`
|
|
219
|
+
);
|
|
220
|
+
const { bundle: res } = await bundleDocument({
|
|
221
|
+
document: testDoc,
|
|
222
|
+
externalRefResolver: new BaseResolver(),
|
|
223
|
+
config: await makeConfig(
|
|
224
|
+
{},
|
|
225
|
+
{
|
|
226
|
+
'filter-in': {
|
|
227
|
+
property: 'x-audience',
|
|
228
|
+
value: ['Public', 'Global'],
|
|
229
|
+
matchStrategy: 'any',
|
|
230
|
+
},
|
|
231
|
+
}
|
|
232
|
+
),
|
|
233
|
+
});
|
|
234
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
235
|
+
openapi: 3.0.0
|
|
236
|
+
paths:
|
|
237
|
+
/pet:
|
|
238
|
+
x-audience: Global
|
|
239
|
+
post:
|
|
240
|
+
summary: test
|
|
241
|
+
/account:
|
|
242
|
+
get:
|
|
243
|
+
summary: get
|
|
244
|
+
components: {}
|
|
245
|
+
|
|
246
|
+
`);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
describe('oas2 filter-in', () => {
|
|
251
|
+
it('should include only one parameter and not include response', async () => {
|
|
252
|
+
const testDoc = parseYamlToDocument(
|
|
253
|
+
outdent`
|
|
254
|
+
swagger: '2.0'
|
|
255
|
+
host: api.instagram.com
|
|
256
|
+
paths:
|
|
257
|
+
'/geographies/{geo-id}/media/recent':
|
|
258
|
+
get:
|
|
259
|
+
parameters:
|
|
260
|
+
- description: The geography ID.
|
|
261
|
+
x-access: private
|
|
262
|
+
in: path
|
|
263
|
+
name: geo-id
|
|
264
|
+
required: true
|
|
265
|
+
type: string
|
|
266
|
+
- description: Max number of media to return.
|
|
267
|
+
x-access: public
|
|
268
|
+
format: int32
|
|
269
|
+
in: query
|
|
270
|
+
name: count
|
|
271
|
+
required: false
|
|
272
|
+
type: integer
|
|
273
|
+
responses:
|
|
274
|
+
'200':
|
|
275
|
+
description: List of recent media entries.
|
|
276
|
+
x-access: [private, protected]
|
|
277
|
+
`
|
|
278
|
+
);
|
|
279
|
+
const { bundle: res } = await bundleDocument({
|
|
280
|
+
document: testDoc,
|
|
281
|
+
externalRefResolver: new BaseResolver(),
|
|
282
|
+
config: await makeConfig(
|
|
283
|
+
{},
|
|
284
|
+
{
|
|
285
|
+
'filter-in': {
|
|
286
|
+
property: 'x-access',
|
|
287
|
+
value: ['public', 'global'],
|
|
288
|
+
matchStrategy: 'any',
|
|
289
|
+
},
|
|
290
|
+
}
|
|
291
|
+
),
|
|
292
|
+
});
|
|
293
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
294
|
+
swagger: '2.0'
|
|
295
|
+
host: api.instagram.com
|
|
296
|
+
paths:
|
|
297
|
+
/geographies/{geo-id}/media/recent:
|
|
298
|
+
get:
|
|
299
|
+
parameters:
|
|
300
|
+
- description: Max number of media to return.
|
|
301
|
+
x-access: public
|
|
302
|
+
format: int32
|
|
303
|
+
in: query
|
|
304
|
+
name: count
|
|
305
|
+
required: false
|
|
306
|
+
type: integer
|
|
307
|
+
|
|
308
|
+
`);
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { outdent } from 'outdent';
|
|
2
|
+
import { bundleDocument } from '../../bundle';
|
|
3
|
+
import { BaseResolver } from '../../resolve';
|
|
4
|
+
import { makeConfig, parseYamlToDocument, yamlSerializer } from '../../../__tests__/utils';
|
|
5
|
+
|
|
6
|
+
describe('oas3 filter-out', () => {
|
|
7
|
+
expect.addSnapshotSerializer(yamlSerializer);
|
|
8
|
+
|
|
9
|
+
const inputDoc = parseYamlToDocument(
|
|
10
|
+
outdent`
|
|
11
|
+
openapi: 3.0.0
|
|
12
|
+
paths:
|
|
13
|
+
/pet:
|
|
14
|
+
x-audience: Private
|
|
15
|
+
post:
|
|
16
|
+
summary: test
|
|
17
|
+
/user:
|
|
18
|
+
x-audience: Protected
|
|
19
|
+
post:
|
|
20
|
+
summary: test
|
|
21
|
+
/order:
|
|
22
|
+
x-audience: [Private, Protected]
|
|
23
|
+
post:
|
|
24
|
+
operationId: storeOrder
|
|
25
|
+
parameters:
|
|
26
|
+
- name: api_key
|
|
27
|
+
callbacks:
|
|
28
|
+
x-access: protected
|
|
29
|
+
orderInProgress:
|
|
30
|
+
x-internal: true`);
|
|
31
|
+
|
|
32
|
+
it('should remove /pet path and y parameter', async () => {
|
|
33
|
+
const testDocument = parseYamlToDocument(
|
|
34
|
+
outdent`
|
|
35
|
+
openapi: 3.0.0
|
|
36
|
+
paths:
|
|
37
|
+
/pet:
|
|
38
|
+
x-access: private
|
|
39
|
+
get:
|
|
40
|
+
parameters:
|
|
41
|
+
- $ref: '#/components/parameters/y'
|
|
42
|
+
components:
|
|
43
|
+
parameters:
|
|
44
|
+
x:
|
|
45
|
+
name: x
|
|
46
|
+
y:
|
|
47
|
+
x-access: private
|
|
48
|
+
name: y
|
|
49
|
+
`
|
|
50
|
+
);
|
|
51
|
+
const { bundle: res } = await bundleDocument({
|
|
52
|
+
document: testDocument,
|
|
53
|
+
externalRefResolver: new BaseResolver(),
|
|
54
|
+
config: await makeConfig({}, { 'filter-out': { property: 'x-access', value: 'private' } }),
|
|
55
|
+
});
|
|
56
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
57
|
+
openapi: 3.0.0
|
|
58
|
+
components:
|
|
59
|
+
parameters:
|
|
60
|
+
x:
|
|
61
|
+
name: x
|
|
62
|
+
|
|
63
|
+
`);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should remove only /order path', async () => {
|
|
67
|
+
const { bundle: res } = await bundleDocument({
|
|
68
|
+
document: inputDoc,
|
|
69
|
+
externalRefResolver: new BaseResolver(),
|
|
70
|
+
config: await makeConfig(
|
|
71
|
+
{},
|
|
72
|
+
{
|
|
73
|
+
'filter-out': {
|
|
74
|
+
property: 'x-audience',
|
|
75
|
+
value: ['Private', 'Protected'],
|
|
76
|
+
matchStrategy: 'all',
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
),
|
|
80
|
+
});
|
|
81
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
82
|
+
openapi: 3.0.0
|
|
83
|
+
paths:
|
|
84
|
+
/pet:
|
|
85
|
+
x-audience: Private
|
|
86
|
+
post:
|
|
87
|
+
summary: test
|
|
88
|
+
/user:
|
|
89
|
+
x-audience: Protected
|
|
90
|
+
post:
|
|
91
|
+
summary: test
|
|
92
|
+
components: {}
|
|
93
|
+
|
|
94
|
+
`);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should remove all paths', async () => {
|
|
98
|
+
const { bundle: res } = await bundleDocument({
|
|
99
|
+
document: inputDoc,
|
|
100
|
+
externalRefResolver: new BaseResolver(),
|
|
101
|
+
config: await makeConfig(
|
|
102
|
+
{},
|
|
103
|
+
{
|
|
104
|
+
'filter-out': {
|
|
105
|
+
property: 'x-audience',
|
|
106
|
+
value: ['Private', 'Protected'],
|
|
107
|
+
matchStrategy: 'any',
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
),
|
|
111
|
+
});
|
|
112
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
113
|
+
openapi: 3.0.0
|
|
114
|
+
components: {}
|
|
115
|
+
|
|
116
|
+
`);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should remove requestBody', async () => {
|
|
120
|
+
const testDoc = parseYamlToDocument(
|
|
121
|
+
outdent`
|
|
122
|
+
openapi: 3.0.0
|
|
123
|
+
paths:
|
|
124
|
+
/pet:
|
|
125
|
+
post:
|
|
126
|
+
summary: test
|
|
127
|
+
requestBody:
|
|
128
|
+
content:
|
|
129
|
+
x-access: private
|
|
130
|
+
application/x-www-form-urlencoded:
|
|
131
|
+
schema:
|
|
132
|
+
type: object
|
|
133
|
+
components: {}
|
|
134
|
+
|
|
135
|
+
`);
|
|
136
|
+
const { bundle: res } = await bundleDocument({
|
|
137
|
+
document: testDoc,
|
|
138
|
+
externalRefResolver: new BaseResolver(),
|
|
139
|
+
config: await makeConfig(
|
|
140
|
+
{},
|
|
141
|
+
{
|
|
142
|
+
'filter-out': {
|
|
143
|
+
property: 'x-access',
|
|
144
|
+
value: 'private',
|
|
145
|
+
matchStrategy: 'any',
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
),
|
|
149
|
+
});
|
|
150
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
151
|
+
openapi: 3.0.0
|
|
152
|
+
paths:
|
|
153
|
+
/pet:
|
|
154
|
+
post:
|
|
155
|
+
summary: test
|
|
156
|
+
components: {}
|
|
157
|
+
|
|
158
|
+
`);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should remove /pet path and /my/path/false path', async () => {
|
|
162
|
+
const testDocument = parseYamlToDocument(
|
|
163
|
+
outdent`
|
|
164
|
+
openapi: 3.0.0
|
|
165
|
+
paths:
|
|
166
|
+
/pet:
|
|
167
|
+
get:
|
|
168
|
+
x-prop: false
|
|
169
|
+
parameters:
|
|
170
|
+
- $ref: '#/components/parameters/x'
|
|
171
|
+
/my/path/false:
|
|
172
|
+
x-access: private
|
|
173
|
+
x-prop: false
|
|
174
|
+
get:
|
|
175
|
+
parameters:
|
|
176
|
+
- $ref: '#/components/parameters/x'
|
|
177
|
+
/my/path/null:
|
|
178
|
+
x-access: private
|
|
179
|
+
x-prop: null
|
|
180
|
+
get:
|
|
181
|
+
parameters:
|
|
182
|
+
- $ref: '#/components/parameters/x'
|
|
183
|
+
components:
|
|
184
|
+
parameters:
|
|
185
|
+
x:
|
|
186
|
+
name: x
|
|
187
|
+
|
|
188
|
+
`);
|
|
189
|
+
const { bundle: res } = await bundleDocument({
|
|
190
|
+
document: testDocument,
|
|
191
|
+
externalRefResolver: new BaseResolver(),
|
|
192
|
+
config: await makeConfig({}, { 'filter-out': { property: 'x-prop', value: false } }),
|
|
193
|
+
});
|
|
194
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
195
|
+
openapi: 3.0.0
|
|
196
|
+
paths:
|
|
197
|
+
/my/path/null:
|
|
198
|
+
x-access: private
|
|
199
|
+
x-prop: null
|
|
200
|
+
get:
|
|
201
|
+
parameters:
|
|
202
|
+
- $ref: '#/components/parameters/x'
|
|
203
|
+
components:
|
|
204
|
+
parameters:
|
|
205
|
+
x:
|
|
206
|
+
name: x
|
|
207
|
+
|
|
208
|
+
`);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('should remove /my/path/null path ', async () => {
|
|
212
|
+
const testDocument = parseYamlToDocument(
|
|
213
|
+
outdent`
|
|
214
|
+
openapi: 3.0.0
|
|
215
|
+
paths:
|
|
216
|
+
/pet:
|
|
217
|
+
x-access: private
|
|
218
|
+
get:
|
|
219
|
+
x-prop: false
|
|
220
|
+
parameters:
|
|
221
|
+
- $ref: '#/components/parameters/y'
|
|
222
|
+
/my/path/false:
|
|
223
|
+
x-access: private
|
|
224
|
+
x-prop: false
|
|
225
|
+
get:
|
|
226
|
+
parameters:
|
|
227
|
+
- $ref: '#/components/parameters/y'
|
|
228
|
+
/my/path/null:
|
|
229
|
+
x-access: private
|
|
230
|
+
x-prop: null
|
|
231
|
+
get:
|
|
232
|
+
parameters:
|
|
233
|
+
- $ref: '#/components/parameters/y'
|
|
234
|
+
components:
|
|
235
|
+
parameters:
|
|
236
|
+
x:
|
|
237
|
+
name: x
|
|
238
|
+
|
|
239
|
+
`);
|
|
240
|
+
const { bundle: res } = await bundleDocument({
|
|
241
|
+
document: testDocument,
|
|
242
|
+
externalRefResolver: new BaseResolver(),
|
|
243
|
+
config: await makeConfig({}, { 'filter-out': { property: 'x-prop', value: null } }),
|
|
244
|
+
});
|
|
245
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
246
|
+
openapi: 3.0.0
|
|
247
|
+
paths:
|
|
248
|
+
/pet:
|
|
249
|
+
x-access: private
|
|
250
|
+
get:
|
|
251
|
+
x-prop: false
|
|
252
|
+
parameters:
|
|
253
|
+
- $ref: '#/components/parameters/y'
|
|
254
|
+
/my/path/false:
|
|
255
|
+
x-access: private
|
|
256
|
+
x-prop: false
|
|
257
|
+
get:
|
|
258
|
+
parameters:
|
|
259
|
+
- $ref: '#/components/parameters/y'
|
|
260
|
+
components:
|
|
261
|
+
parameters:
|
|
262
|
+
x:
|
|
263
|
+
name: x
|
|
264
|
+
|
|
265
|
+
`);
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe('oas2 filter-out', () => {
|
|
270
|
+
it('should clean all parameters and responses ', async () => {
|
|
271
|
+
const testDoc = parseYamlToDocument(
|
|
272
|
+
outdent`
|
|
273
|
+
swagger: '2.0'
|
|
274
|
+
host: api.instagram.com
|
|
275
|
+
paths:
|
|
276
|
+
'/geographies/{geo-id}/media/recent':
|
|
277
|
+
get:
|
|
278
|
+
parameters:
|
|
279
|
+
- description: The geography ID.
|
|
280
|
+
x-access: private
|
|
281
|
+
in: path
|
|
282
|
+
name: geo-id
|
|
283
|
+
required: true
|
|
284
|
+
type: string
|
|
285
|
+
- description: Max number of media to return.
|
|
286
|
+
x-access: protected
|
|
287
|
+
format: int32
|
|
288
|
+
in: query
|
|
289
|
+
name: count
|
|
290
|
+
required: false
|
|
291
|
+
type: integer
|
|
292
|
+
responses:
|
|
293
|
+
$ref: '#/components/response/200'
|
|
294
|
+
components:
|
|
295
|
+
response:
|
|
296
|
+
'200':
|
|
297
|
+
description: List of recent media entries.
|
|
298
|
+
x-access: [protected, public]
|
|
299
|
+
`
|
|
300
|
+
);
|
|
301
|
+
const { bundle: res } = await bundleDocument({
|
|
302
|
+
document: testDoc,
|
|
303
|
+
externalRefResolver: new BaseResolver(),
|
|
304
|
+
config: await makeConfig(
|
|
305
|
+
{},
|
|
306
|
+
{
|
|
307
|
+
'filter-out': {
|
|
308
|
+
property: 'x-access',
|
|
309
|
+
value: ['private', 'protected'],
|
|
310
|
+
matchStrategy: 'any',
|
|
311
|
+
},
|
|
312
|
+
}
|
|
313
|
+
),
|
|
314
|
+
});
|
|
315
|
+
expect(res.parsed).toMatchInlineSnapshot(`
|
|
316
|
+
swagger: '2.0'
|
|
317
|
+
host: api.instagram.com
|
|
318
|
+
paths:
|
|
319
|
+
/geographies/{geo-id}/media/recent:
|
|
320
|
+
get: {}
|
|
321
|
+
components:
|
|
322
|
+
response:
|
|
323
|
+
'200':
|
|
324
|
+
description: List of recent media entries.
|
|
325
|
+
x-access:
|
|
326
|
+
- protected
|
|
327
|
+
- public
|
|
328
|
+
|
|
329
|
+
`);
|
|
330
|
+
});
|
|
331
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { UserContext } from '../../../walk';
|
|
2
|
+
import { isRef } from '../../../ref-utils';
|
|
3
|
+
import { isEmptyArray, isEmptyObject, isPlainObject } from '../../../utils';
|
|
4
|
+
|
|
5
|
+
export function filter(node: any, ctx: UserContext, criteria: (item: any) => boolean) {
|
|
6
|
+
const { parent, key } = ctx;
|
|
7
|
+
let didDelete = false;
|
|
8
|
+
if (Array.isArray(node)) {
|
|
9
|
+
for (let i = 0; i < node.length; i++) {
|
|
10
|
+
if (isRef(node[i])) {
|
|
11
|
+
const resolved = ctx.resolve(node[i]);
|
|
12
|
+
if (criteria(resolved.node)) {
|
|
13
|
+
node.splice(i, 1);
|
|
14
|
+
didDelete = true;
|
|
15
|
+
i--;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (criteria(node[i])) {
|
|
19
|
+
node.splice(i, 1);
|
|
20
|
+
didDelete = true;
|
|
21
|
+
i--;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
} else if (isPlainObject(node)) {
|
|
25
|
+
for (const key of Object.keys(node)) {
|
|
26
|
+
node = node as any;
|
|
27
|
+
if (isRef(node[key])) {
|
|
28
|
+
const resolved = ctx.resolve(node[key]);
|
|
29
|
+
if (criteria(resolved.node)) {
|
|
30
|
+
delete node[key];
|
|
31
|
+
didDelete = true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (criteria(node[key])) {
|
|
35
|
+
delete node[key];
|
|
36
|
+
didDelete = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (didDelete && (isEmptyObject(node) || isEmptyArray(node))) {
|
|
41
|
+
delete parent[key];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function checkIfMatchByStrategy(
|
|
46
|
+
nodeValue: any,
|
|
47
|
+
decoratorValue: any,
|
|
48
|
+
strategy: 'all' | 'any'
|
|
49
|
+
): boolean {
|
|
50
|
+
if (nodeValue===undefined || decoratorValue===undefined) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!Array.isArray(decoratorValue) && !Array.isArray(nodeValue)) {
|
|
55
|
+
return nodeValue === decoratorValue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
decoratorValue = toArrayIfNeeded<string>(decoratorValue);
|
|
59
|
+
nodeValue = toArrayIfNeeded<string>(nodeValue);
|
|
60
|
+
|
|
61
|
+
if (strategy === 'any') {
|
|
62
|
+
return decoratorValue.some((item: string) => nodeValue.includes(item));
|
|
63
|
+
}
|
|
64
|
+
if (strategy === 'all') {
|
|
65
|
+
return decoratorValue.every((item: string) => nodeValue.includes(item));
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function toArrayIfNeeded<T>(value: T | T[]): T[] {
|
|
71
|
+
return Array.isArray(value) ? value : [value];
|
|
72
|
+
}
|