Skip to content

Commit 71e276f

Browse files
committed
tests: inverse with strip
1 parent 6359be9 commit 71e276f

7 files changed

Lines changed: 113 additions & 2 deletions

File tree

test/filtering.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ describe('openapi-format CLI filtering tests', () => {
310310
});
311311
});
312312

313+
describe('yaml-filter-inverse-flags-stripFlags', () => {
314+
it('yaml-filter-inverse-flags-stripFlags - should match expected output', async () => {
315+
const testName = 'yaml-filter-inverse-flags-stripFlags';
316+
const {result, outputBefore, outputAfter} = await testUtils.loadTest(testName);
317+
expect(result.code).toBe(0);
318+
expect(result.stdout).toContain('formatted successfully');
319+
expect(outputAfter).toStrictEqual(outputBefore);
320+
});
321+
});
322+
313323
describe('isUsedComp', () => {
314324
it('returns false for non-object input', () => {
315325
expect(isUsedComp(null, 'schemas')).toBe(false);

test/openapi-core.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,56 @@ describe('openapi-format core API', () => {
2626
expect(result.data.tags).toEqual([{name: 'pets'}]);
2727
});
2828

29+
it('openapiFilter inverseFlags + stripFlags currently removes previously kept operations after recurse', async () => {
30+
const doc = {
31+
openapi: '3.0.0',
32+
info: {title: 'API', version: '1.0.0'},
33+
paths: {
34+
'/pets': {
35+
get: {'x-public': true, responses: {'200': {description: 'ok'}}},
36+
post: {responses: {'200': {description: 'ok'}}}
37+
}
38+
}
39+
};
40+
41+
const onlyInverse = await openapiFilter(doc, {filterSet: {inverseFlags: ['x-public']}});
42+
expect(onlyInverse.data.paths).toHaveProperty('/pets.get');
43+
44+
const inverseAndStrip = await openapiFilter(doc, {filterSet: {inverseFlags: ['x-public'], stripFlags: ['x-public']}});
45+
expect(inverseAndStrip.data.paths).toBeUndefined();
46+
});
47+
48+
it('adding responses to unusedComponents does not change inverseFlags+stripFlags outcome', async () => {
49+
const doc = {
50+
openapi: '3.0.0',
51+
info: {title: 'API', version: '1.0.0'},
52+
paths: {
53+
'/pets': {
54+
get: {'x-public': true, responses: {'200': {description: 'ok'}}},
55+
post: {responses: {'200': {description: 'ok'}}}
56+
}
57+
},
58+
components: {
59+
schemas: {
60+
Pet: {type: 'object'}
61+
}
62+
}
63+
};
64+
65+
const base = {
66+
inverseFlags: ['x-public'],
67+
stripFlags: ['x-public'],
68+
unusedComponents: ['schemas', 'parameters', 'examples', 'headers', 'requestBodies']
69+
};
70+
const withResponses = {...base, unusedComponents: [...base.unusedComponents, 'responses']};
71+
72+
const resultBase = await openapiFilter(doc, {filterSet: base});
73+
const resultWithResponses = await openapiFilter(doc, {filterSet: withResponses});
74+
75+
expect(resultBase.data).toEqual(resultWithResponses.data);
76+
expect(resultWithResponses.data.paths).toBeUndefined();
77+
});
78+
2979
it('openapiChangeCase should apply summary, description and securitySchemes ref casing', async () => {
3080
const doc = {
3181
openapi: '3.0.0',

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ const {run} = require('../bin/cli');
88
const {parseFile, stringify, writeFile} = require('../openapi-format');
99

1010
// SELECTIVE TESTING DEBUG
11-
const localTesting = false;
11+
const localTesting = true;
1212
const destroyOutput = false;
1313

1414
// Load tests
1515
const tests = !localTesting
1616
? fs.readdirSync(__dirname).filter(file => {
1717
return fs.statSync(path.join(__dirname, file)).isDirectory() && !file.startsWith('_');
1818
})
19-
: ['yaml-sort-component-props'];
19+
: ['yaml-filter-inverse-flags-stripFlags'];
2020

2121
describe('openapi-format tests', () => {
2222
let consoleLogSpy, consoleWarnSpy;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
inverseFlags:
2+
- x-public
3+
stripFlags:
4+
- x-public
5+
unusedComponents:
6+
- schemas
7+
- parameters
8+
- examples
9+
- headers
10+
- requestBodies
11+
- responses
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
paths:
6+
/pets:
7+
get:
8+
operationId: findPets
9+
x-public: true
10+
responses:
11+
'200':
12+
description: pet response
13+
content:
14+
application/json:
15+
schema:
16+
$ref: '#/components/schemas/Pet'
17+
post:
18+
operationId: addPet
19+
responses:
20+
'200':
21+
description: pet response
22+
content:
23+
application/json:
24+
schema:
25+
$ref: '#/components/schemas/Pet'
26+
components:
27+
schemas:
28+
Pet:
29+
type: object
30+
properties:
31+
id:
32+
type: integer
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
verbose: true
2+
no-sort: true
3+
output: output.yaml
4+
filterFile: customFilter.yaml
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore

0 commit comments

Comments
 (0)