Skip to content

Commit

Permalink
Return a Set from find_refs.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jul 25, 2024
1 parent 74cdcde commit ecc842e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
17 changes: 5 additions & 12 deletions tools/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,11 @@ export function delete_matching_keys(obj: any, condition: (obj: any) => boolean)
}
}

export function find_refs(obj: Record<string, any>): string[] {
let results: string[] = []

if (obj !== undefined && obj !== null && obj.$ref !== undefined) {
results.push(obj.$ref as string)
}

if (_.isObject(obj)) {
results = _.concat(results, _.flatMap(obj, (v, _k) => find_refs(v as Record<string, any>)))
}

return _.uniq(results)
export function find_refs (obj: 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)))

Check failure on line 83 in tools/src/helpers.ts

View workflow job for this annotation

GitHub Actions / lint

Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function
return results
}

export function ensure_parent_dir (file_path: string): void {
Expand Down
12 changes: 7 additions & 5 deletions tools/src/merger/OpenApiVersionExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export default class OpenApiVersionExtractor {
const x_version_added = semver.coerce(obj['x-version-added'] as string)
const x_version_removed = semver.coerce(obj['x-version-removed'] as string)

if (x_version_added && !semver.satisfies(this._target_version, `>=${x_version_added?.toString()}`)) {
if (x_version_added && !semver.satisfies(this._target_version, `>=${x_version_added.toString()}`)) {
return true
} else if (x_version_removed && !semver.satisfies(this._target_version, `<${x_version_removed?.toString()}`)) {
} else if (x_version_removed && !semver.satisfies(this._target_version, `<${x_version_removed.toString()}`)) {
return true
}

Expand All @@ -70,13 +70,15 @@ export default class OpenApiVersionExtractor {
if (this._spec === undefined) return

// remove anything that's not referenced
var references: string[] = find_refs(this._spec)
var references = find_refs(this._spec)

this._spec.components = _.reduce(_.map(['parameters', 'requestBodies', 'responses', 'schemas'], (p) =>
{
return {
[p]: _.pickBy(this._spec?.components?.[p], (_value, key) =>
_.includes(references, `#/components/${p}/${key}`))
[p]: _.pickBy(
this._spec?.components?.[p], (_value, key) =>
references.has(`#/components/${p}/${key}`)
)
}
}
), extend)
Expand Down
4 changes: 2 additions & 2 deletions tools/tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('helpers', () => {

describe('find_refs', () => {
test('empty collection', () => {
expect(find_refs({})).toEqual([])
expect(find_refs({})).toEqual(new Set())
})

test('with refs', () => {
Expand All @@ -148,7 +148,7 @@ describe('helpers', () => {
$ref: 'dup',
},
}]
})).toEqual([1, 2, 3, 'dup'])
})).toEqual(new Set([1, 2, 3, 'dup']))
})
})
})

0 comments on commit ecc842e

Please sign in to comment.