Skip to content
Closed
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
6 changes: 6 additions & 0 deletions .changeset/filter-in-applyto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": minor
"@redocly/cli": minor
---

Added `applyTo` option to `filter-in` decorator to limit filtering to specific node types. This allows users to avoid accidentally filtering schema properties when filtering operations by tags.
86 changes: 86 additions & 0 deletions packages/core/src/decorators/__tests__/filter-in.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,92 @@ describe('oas3 filter-in', () => {

`);
});

it('should support applyTo option to limit filtering to specific node types', async () => {
// Using applyTo: PathItem prevents filtering schema properties that happen
// to have the same name as the filter property (e.g., a schema with a "tags"
// property definition should not be filtered when filtering operations by tags).
const testDoc = parseYamlToDocument(
outdent`
openapi: 3.0.0
paths:
/events:
get:
tags:
- App
summary: List events
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Event'
/users:
get:
tags:
- Public
summary: List users
components:
schemas:
Event:
type: object
properties:
id:
type: string
tags:
type: array
items:
type: string
description: Event tags
`
);
const { bundle: res } = await bundleDocument({
document: testDoc,
externalRefResolver: new BaseResolver(),
config: await createConfig({
decorators: {
'filter-in': {
property: 'tags',
value: 'App',
applyTo: 'PathItem',
},
},
}),
types: Oas3Types,
});
// The Event schema should retain its 'tags' property (schema definition)
// Only the /users path should be filtered out (wrong operation tag)
expect(res.parsed).toMatchInlineSnapshot(`
openapi: 3.0.0
paths:
/events:
get:
tags:
- App
summary: List events
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Event'
components:
schemas:
Event:
type: object
properties:
id:
type: string
tags:
type: array
items:
type: string
description: Event tags

`);
});
});

describe('oas2 filter-in', () => {
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/decorators/common/filters/filter-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ import type { Oas2Decorator, Oas3Decorator } from '../../../visitors.js';

const DEFAULT_STRATEGY = 'any';

export const FilterIn: Oas3Decorator | Oas2Decorator = ({ property, value, matchStrategy }) => {
export const FilterIn: Oas3Decorator | Oas2Decorator = ({
property,
value,
matchStrategy,
applyTo,
}) => {
const strategy = matchStrategy || DEFAULT_STRATEGY;
const filterInCriteria = (item: any) =>
item?.[property] && !checkIfMatchByStrategy(item?.[property], value, strategy);

const visitor = applyTo || 'any';

return {
any: {
[visitor]: {
enter: (node, ctx) => {
filter(node, ctx, filterInCriteria);
},
Expand Down
Loading