Skip to content

Commit

Permalink
Traverse 's recursively.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <dblock@amazon.com>
  • Loading branch information
dblock committed Jul 25, 2024
1 parent e661b32 commit 8367e52
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
23 changes: 20 additions & 3 deletions tools/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,27 @@ export function delete_matching_keys(obj: any, condition: (obj: any) => boolean)
}
}

export function find_refs (obj: any): Set<string> {
export function find_refs (current: Record<string, any>, root?: Record<string, any>): Set<string> {
var results = new Set<string>()
if (obj?.$ref != null) results.add(obj.$ref as string)
if (_.isObject(obj)) _.forEach(obj, (v) => { find_refs(v).forEach((ref) => results.add(ref)); })

if (root === undefined) {
root = current
current = current.paths
}

if (current?.$ref != null) {
const ref = current.$ref as string
results.add(ref)
const ref_node = resolve_ref(ref, root)
if (ref_node !== undefined) find_refs(ref_node, root).forEach((ref) => results.add(ref))
}

if (_.isObject(current)) {
_.forEach(current, (v) => {
find_refs(v as Record<string, any>, root).forEach((ref) => results.add(ref));
})
}

return results
}

Expand Down
55 changes: 40 additions & 15 deletions tools/tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,49 @@ describe('helpers', () => {

test('with refs', () => {
expect(find_refs({
$ref: 1,
null: null,
undefined,
obj: {
$ref: 2,
paths: {
$ref: '#1',
null: null,
undefined,
obj: {
$ref: 3
}
},
arr: [{
obj1: {
$ref: 'dup',
$ref: '#2',
obj: {
$ref: '#3'
},
schema_obj: {
$ref: '#/schemas/schema1'
}
},
obj2: {
$ref: 'dup',
arr: [{
obj1: {
$ref: '#dup',
},
obj2: {
$ref: '#dup',
},
}]
},
schemas: {
schema1: {
items: {
$ref: '#/schemas/schema2'
}
},
}]
})).toEqual(new Set([1, 2, 3, 'dup']))
schema2: {
x: 1
}
},
unused1: {
$ref: '#unused2',
obj: {
$ref: '#unused3'
}
},
'#1': {},
'#2': {},
'#3': {},
'#dup': {}
})).toEqual(new Set(['#1', '#2', '#3', '#dup', '#/schemas/schema1', '#/schemas/schema2']))
})
})
})
15 changes: 14 additions & 1 deletion tools/tests/merger/fixtures/extractor/expected_2.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ components:
properties:
_all:
type: boolean
ids:
$ref: '#/components/schemas/_common:Ids'
x-version-added: '2.0'
description: Nodes options.
responses:
info@200:
Expand Down Expand Up @@ -158,4 +161,14 @@ components:
application/json:
schema:
type: object
schemas: {}
schemas:
_common:Ids:
oneOf:
- $ref: '#/components/schemas/_common:OldId'
- type: array
items:
$ref: '#/components/schemas/_common:NewId'
_common:NewId:
type: string
_common:OldId:
type: string
12 changes: 12 additions & 0 deletions tools/tests/tester/fixtures/specs/complete/namespaces/nodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ components:
properties:
_all:
type: boolean
ids:
$ref: '../schemas/_common.yaml#/components/schemas/Ids'
x-version-added: '2.0'
description: Nodes options.
parameters:
nodes.info::path.id:
Expand All @@ -63,6 +66,15 @@ components:
schema:
type: boolean
default: false
nodes.info::query.new:
in: query
name: new
description: New option added in 2.1.
required: false
x-version-added: '2.1'
schema:
type: array
default: false
responses:
nodes.info@200:
description: All nodes.
Expand Down
10 changes: 10 additions & 0 deletions tools/tests/tester/fixtures/specs/complete/schemas/_common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ components:
Type:
type: string
x-version-removed: '2.0'
OldId:
type: string
NewId:
type: string
Ids:
oneOf:
- $ref: '#/components/schemas/OldId'
- type: array
items:
$ref: '#/components/schemas/NewId'

0 comments on commit 8367e52

Please sign in to comment.