diff --git a/tools/src/helpers.ts b/tools/src/helpers.ts index f584629dd..be9aa209f 100644 --- a/tools/src/helpers.ts +++ b/tools/src/helpers.ts @@ -77,18 +77,11 @@ export function delete_matching_keys(obj: any, condition: (obj: any) => boolean) } } -export function find_refs(obj: Record): 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))) - } - - return _.uniq(results) +export function find_refs (obj: any): Set { + var results = new Set() + if (obj?.$ref != null) results.add(obj.$ref as string) + if (_.isObject(obj)) _.forEach(obj, (v) => find_refs(v).forEach((ref) => results.add(ref))) + return results } export function ensure_parent_dir (file_path: string): void { diff --git a/tools/src/merger/OpenApiVersionExtractor.ts b/tools/src/merger/OpenApiVersionExtractor.ts index 2c0ce0f57..5d4164b1c 100644 --- a/tools/src/merger/OpenApiVersionExtractor.ts +++ b/tools/src/merger/OpenApiVersionExtractor.ts @@ -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 } @@ -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) diff --git a/tools/tests/helpers.test.ts b/tools/tests/helpers.test.ts index 1991d21b6..185ca6415 100644 --- a/tools/tests/helpers.test.ts +++ b/tools/tests/helpers.test.ts @@ -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', () => { @@ -148,7 +148,7 @@ describe('helpers', () => { $ref: 'dup', }, }] - })).toEqual([1, 2, 3, 'dup']) + })).toEqual(new Set([1, 2, 3, 'dup'])) }) }) })