Skip to content

Commit

Permalink
Got rid of merge().
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jul 18, 2024
1 parent 262e1e0 commit 4db9e70
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 27 deletions.
2 changes: 1 addition & 1 deletion tools/src/linter/SchemasValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>
this.spec = merger.spec().components as Record<string, any>
const named_schemas_errors = this.validate_named_schemas()
if (named_schemas_errors.length > 0) return named_schemas_errors
return [
Expand Down
24 changes: 10 additions & 14 deletions tools/src/merger/OpenApiMerger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion tools/src/merger/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
2 changes: 1 addition & 1 deletion tools/src/tester/MergedOpenApiSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions tools/tests/merger/OpenApiMerger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand All @@ -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'))
})
Expand All @@ -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'))
})
Expand Down

0 comments on commit 4db9e70

Please sign in to comment.