Skip to content

Feature - additional filter options: operationIds & method #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ paths:
...
```

Works with OpenAPI/Swagger 2.0 and 3.0.x and AsyncAPI definitions.
Works with OpenAPI/Swagger 2.0 and 3.x and AsyncAPI definitions.

```
openapi-filter.js <infile> [outfile]
Expand All @@ -31,13 +31,15 @@ Positionals:

Options:

--info include complete info object with --valid [boolean]
--inverse, -i output filtered elements only [boolean]
--flags, -f flags to filter by [array] [default: ["x-internal"]]
--flagValues, -v flag String values to match [array] [default: []]
--checkTags filter if flags given in --flags are in the tags array
--info include complete info object with --valid [boolean]
--inverse, -i output filtered elements only [boolean]
--flags, -f flags to filter by [array] [default: ["x-internal"]]
--flagValues, -v flag String values to match [array] [default: []]
--checkTags filter if flags given in --flags are in the tags array
[boolean]
--overrides, -o prefixes used to override named properties[arr] [default: []]
--methods, -m OpenAPI methods to filter by [array] [default: []]
--operationIds, -id OpenAPI operationIds to filter by [array] [default: []]
--valid try to ensure inverse output is valid [boolean]
--strip, -s strip the flags from the finished product [boolean]
--servers include complete servers object with --valid [boolean]
Expand Down
51 changes: 51 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ function filter(obj,options) {
const defaults = {};
defaults.flags = ['x-internal'];
defaults.flagValues = [];
defaults.methods = [];
defaults.operationIds = [];
defaults.checkTags = false;
defaults.inverse = false;
defaults.strip = false;
Expand Down Expand Up @@ -51,6 +53,55 @@ function filter(obj,options) {
break;
}
}

for (let method of options.methods) {
if (key === method) {
if (options.inverse) {
if (Array.isArray(obj)) {
// we need to seed the presence of an empty array
// otherwise jptr won't know whether it's setting
// an array entry or a property with a numeric key #26
const components = state.path.split('/');
components.pop(); // throw away last item
if (jptr(filtered,components.join('/')) === false) {
jptr(filtered,components.join('/'),[]);
}
}
jptr(filtered,state.path,clone(obj[key]));
}
filteredpaths.push(state.path);
delete obj[key];
break;
}
}

for (let operationId of options.operationIds) {
if (obj[key] === operationId) {
if (options.inverse) {
if (Array.isArray(obj)) {
// we need to seed the presence of an empty array
// otherwise jptr won't know whether it's setting
// an array entry or a property with a numeric key #26
const components = state.path.split('/');
components.pop(); // throw away last item
if (jptr(filtered,components.join('/')) === false) {
jptr(filtered,components.join('/'),[]);
}
}
jptr(filtered,state.path,clone(obj[key]));
}

// Recursed to a level too deep, so we go a level higher
const components = state.path.split('/');
components.pop(); // throw away last item
const parentstate = components.join('/')
filteredpaths.push(parentstate);

// Use the parent state object to remove the item attached to the parent state
delete state.parent[state.pkey];
break;
}
}
});

// remove undefined properties (important for YAML output)
Expand Down
9 changes: 8 additions & 1 deletion openapi-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ let argv = require('yargs')
.alias('overrides','o')
.default('overrides', [])
.describe('overrides', 'prefixes used to override named properties')
.array('methods')
.alias('methods', 'm')
.describe('methods', 'OpenAPI methods to filter by')
.default('methods', [])
.array('operationIds')
.alias('operationIds', 'id')
.describe('operationIds', 'OpenAPI operationIds to filter by')
.default('operationIds', [])
.boolean('valid')
.describe('valid', 'try to ensure inverse output is valid')
.boolean('strip')
Expand Down Expand Up @@ -102,4 +110,3 @@ else {
}

info('\n✅ Document was filtered successfully')

Loading