From 4db9e70d3e0b797b4377f6afb7a55745ec76f512 Mon Sep 17 00:00:00 2001 From: dblock Date: Thu, 18 Jul 2024 16:55:26 -0400 Subject: [PATCH] Got rid of merge(). Signed-off-by: dblock --- tools/src/linter/SchemasValidator.ts | 2 +- tools/src/merger/OpenApiMerger.ts | 24 ++++++++++-------------- tools/src/merger/merge.ts | 2 +- tools/src/tester/MergedOpenApiSpec.ts | 2 +- tools/tests/merger/OpenApiMerger.test.ts | 12 ++---------- 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/tools/src/linter/SchemasValidator.ts b/tools/src/linter/SchemasValidator.ts index 6954d68ab..3c0065d94 100644 --- a/tools/src/linter/SchemasValidator.ts +++ b/tools/src/linter/SchemasValidator.ts @@ -33,7 +33,7 @@ export default class SchemasValidator { validate (): ValidationError[] { const merger = new OpenApiMerger(this.root_folder, undefined, new Logger(LogLevel.error)) - this.spec = merger.merge().spec().components as Record + this.spec = merger.spec().components as Record const named_schemas_errors = this.validate_named_schemas() if (named_schemas_errors.length > 0) return named_schemas_errors return [ diff --git a/tools/src/merger/OpenApiMerger.ts b/tools/src/merger/OpenApiMerger.ts index a0f16a5b8..b914c3be7 100644 --- a/tools/src/merger/OpenApiMerger.ts +++ b/tools/src/merger/OpenApiMerger.ts @@ -46,25 +46,21 @@ export default class OpenApiMerger { } write_to(output_path: string): OpenApiMerger { - if (!this._merged) { throw "Call merge() first." } this.logger.info(`Writing ${output_path} ...`) - write_yaml(output_path, this._spec) - return this - } - - merge (): OpenApiMerger { - if (this._merged) { throw "Spec already merged." } - this.#merge_schemas() - this.#merge_namespaces() - this.#sort_spec_keys() - this.#generate_global_params() - this.#generate_superseded_ops() - this._merged = true + write_yaml(output_path, this.spec()) return this } spec(): OpenAPIV3.Document { - if (!this._merged) { this.merge() } + if (!this._merged) { + this.#merge_schemas() + this.#merge_namespaces() + this.#sort_spec_keys() + this.#generate_global_params() + this.#generate_superseded_ops() + this._merged = true + } + return this._spec as OpenAPIV3.Document } diff --git a/tools/src/merger/merge.ts b/tools/src/merger/merge.ts index 0ad3d4eed..7b8c42903 100644 --- a/tools/src/merger/merge.ts +++ b/tools/src/merger/merge.ts @@ -25,5 +25,5 @@ const opts = command.opts() const logger = new Logger(opts.verbose ? LogLevel.info : LogLevel.warn) const merger = new OpenApiMerger(opts.source, opts.opensearchVersion, logger) logger.log(`Merging ${opts.source} into ${opts.output} (${opts.opensearchVersion}) ...`) -merger.merge().write_to(opts.output) +merger.write_to(opts.output) logger.log('Done.') diff --git a/tools/src/tester/MergedOpenApiSpec.ts b/tools/src/tester/MergedOpenApiSpec.ts index ddc157ece..b036c07fe 100644 --- a/tools/src/tester/MergedOpenApiSpec.ts +++ b/tools/src/tester/MergedOpenApiSpec.ts @@ -30,7 +30,7 @@ export default class MergedOpenApiSpec { spec (): OpenAPIV3.Document { if (this._spec) return this._spec const merger = new OpenApiMerger(this.file_path, this.target_version, this.logger) - const spec = merger.merge().spec() + const spec = merger.spec() const ctx = new SpecificationContext(this.file_path) this.inject_additional_properties(ctx, spec) this._spec = spec diff --git a/tools/tests/merger/OpenApiMerger.test.ts b/tools/tests/merger/OpenApiMerger.test.ts index 1169b2101..2b5904ed6 100644 --- a/tools/tests/merger/OpenApiMerger.test.ts +++ b/tools/tests/merger/OpenApiMerger.test.ts @@ -24,16 +24,8 @@ describe('OpenApiMerger', () => { }) test('merges spec', () => { - merger.merge() expect(merger.spec()).toBeDefined() }) - - test('raises an error when called twice', () => { - merger.merge() - expect(() => { - merger.merge() - }).toThrow('Spec already merged.'); - }) }) describe('write_to()', () => { @@ -42,7 +34,7 @@ describe('OpenApiMerger', () => { }) test('writes a spec', () => { - merger.merge().write_to('./tools/tests/merger/opensearch-openapi.yaml') + merger.write_to('./tools/tests/merger/opensearch-openapi.yaml') expect(fs.readFileSync('./tools/tests/merger/fixtures/expected_2.0.yaml', 'utf8')) .toEqual(fs.readFileSync('./tools/tests/merger/opensearch-openapi.yaml', 'utf8')) }) @@ -55,7 +47,7 @@ describe('OpenApiMerger', () => { }) test('writes a spec', () => { - merger.merge().write_to('./tools/tests/merger/opensearch-openapi.yaml') + merger.write_to('./tools/tests/merger/opensearch-openapi.yaml') expect(fs.readFileSync('./tools/tests/merger/fixtures/expected_1.3.yaml', 'utf8')) .toEqual(fs.readFileSync('./tools/tests/merger/opensearch-openapi.yaml', 'utf8')) })