Skip to content

Commit

Permalink
Added tests for merger
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong committed Apr 15, 2024
1 parent 198473d commit 51fae97
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 2 deletions.
44 changes: 44 additions & 0 deletions spec/opensearch-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,47 @@ paths:
$ref: 'namespaces/tasks.yaml#/paths/~1_tasks~1{task_id}'
/_tasks/{task_id}/_cancel:
$ref: 'namespaces/tasks.yaml#/paths/~1_tasks~1{task_id}~1_cancel'
components:
parameters:
_global::query.pretty:
x-global: true
name: pretty
in: query
description: Whether to pretty format the returned JSON response.
schema:
type: boolean
default: false
_global::query.human:
x-global: true
name: human
in: query
description: Whether to return human readable values for statistics.
schema:
type: boolean
default: true
_global::query.error_trace:
x-global: true
name: error_trace
in: query
description: Whether to include the stack trace of returned errors.
schema:
type: boolean
default: false
_global::query.source:
x-global: true
name: source
in: query
description: The URL-encoded request definition. Useful for libraries that do not accept a request body for non-POST requests.
schema:
type: string
_global::query.filter_path:
x-global: true
name: filter_path
in: query
description: Comma-separated list of filters used to reduce the response.
schema:
oneOf:
- type: string
- type: array
items:
type: string
4 changes: 2 additions & 2 deletions tools/merger/OpenApiMerger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export default class OpenApiMerger {
this.root_folder = this.root_path.split('/').slice(0, -1).join('/');
this.spec = yaml.parse(fs.readFileSync(this.root_path, 'utf8'));
this.spec.components = {
parameters: {},
parameters: this.spec.components?.parameters || {},
requestBodies: {},
responses: {},
schemas: {},
};
}

merge(output_path: string | null): OpenAPIV3.Document {
merge(output_path?: string): OpenAPIV3.Document {
this.#merge_namespaces();
this.#merge_schemas();
this.#sort_spec_keys();
Expand Down
12 changes: 12 additions & 0 deletions tools/test/merger/OpenApiMerger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import OpenApiMerger from '../../merger/OpenApiMerger';
import fs from 'fs';
import yaml from 'yaml';


test('merge()', async () => {
const merger = new OpenApiMerger('./test/merger/fixtures/spec/opensearch-openapi.yaml');
merger.merge('./test/merger/opensearch-openapi.yaml');
expect(fs.readFileSync('./test/merger/fixtures/expected.yaml', 'utf8'))
.toEqual(fs.readFileSync('./test/merger/opensearch-openapi.yaml', 'utf8'));
fs.unlinkSync('./test/merger/opensearch-openapi.yaml');
});
73 changes: 73 additions & 0 deletions tools/test/merger/fixtures/expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
openapi: 3.1.0
info:
title: OpenSearch API
description: OpenSearch API
version: 1.0.0
paths:
/adopt/{animal}:
post:
parameters:
- $ref: '#/components/parameters/adopt::path.animal'
requestBody:
$ref: '#/components/requestBodies/adopt'
responses:
'200':
$ref: '#/components/responses/adopt@200'
components:
parameters:
_global::query.human:
x-global: true
name: human
in: query
description: Whether to return human readable values for statistics.
schema:
type: boolean
default: true
adopt::path.animal:
name: animal
in: path
schema:
$ref: '#/components/schemas/animals:Animal'
indices.create::path.index:
name: index
in: path
schema:
type: string
indices.create::query.pretty:
name: pretty
in: query
schema:
type: boolean
requestBodies:
adopt: {}
indices.create: {}
responses:
adopt@200:
description: ''
application/json:
schema:
type: object
indices.create@200:
description: ''
application/json:
schema:
type: object
schemas:
actions:Bark:
type: string
actions:Meow:
type: string
animals:Animal:
oneOf:
- $ref: '#/components/schemas/animals:Dog'
- $ref: '#/components/schemas/animals:Cat'
animals:Cat:
type: object
properties:
meow:
$ref: '#/components/schemas/actions:Meow'
animals:Dog:
type: object
properties:
bark:
$ref: '#/components/schemas/actions:Bark'
35 changes: 35 additions & 0 deletions tools/test/merger/fixtures/spec/namespaces/ignored.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.1.0
info:
title: Ignored
version: 1.0.0
paths:
'/{index}':
post:
parameters:
- $ref: '#/components/parameters/indices.create::path.index'
- $ref: '#/components/parameters/indices.create::query.pretty'
requestBody:
$ref: '#/components/requestBodies/indices.create'
responses:
'200':
$ref: '#/components/responses/indices.create@200'
components:
requestBodies:
indices.create: {}
parameters:
indices.create::path.index:
name: index
in: path
schema:
type: string
indices.create::query.pretty:
name: pretty
in: query
schema:
type: boolean
responses:
indices.create@200:
description: ''
application/json:
schema:
type: object
30 changes: 30 additions & 0 deletions tools/test/merger/fixtures/spec/namespaces/shelter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.1.0
info:
title: OpenSearch API
description: OpenSearch API
version: 1.0.0
paths:
'/adopt/{animal}':
post:
parameters:
- $ref: '#/components/parameters/adopt::path.animal'
requestBody:
$ref: '#/components/requestBodies/adopt'
responses:
'200':
$ref: '#/components/responses/adopt@200'
components:
requestBodies:
adopt: {}
parameters:
adopt::path.animal:
name: animal
in: path
schema:
$ref: '../schemas/animals.yaml#/components/schemas/Animal'
responses:
adopt@200:
description: ''
application/json:
schema:
type: object
18 changes: 18 additions & 0 deletions tools/test/merger/fixtures/spec/opensearch-openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
openapi: 3.1.0
info:
title: OpenSearch API
description: OpenSearch API
version: 1.0.0
paths:
'/adopt/{animal}':
$ref: 'namespaces/shelter.yaml#/paths/~1adopt~1{animal}'
components:
parameters:
_global::query.human:
x-global: true
name: human
in: query
description: Whether to return human readable values for statistics.
schema:
type: boolean
default: true
11 changes: 11 additions & 0 deletions tools/test/merger/fixtures/spec/schemas/actions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
openapi: 3.1.0
info:
title: OpenSearch API
description: OpenSearch API
version: 1.0.0
components:
schemas:
Bark:
type: string
Meow:
type: string
21 changes: 21 additions & 0 deletions tools/test/merger/fixtures/spec/schemas/animals.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.1.0
info:
title: OpenSearch API
description: OpenSearch API
version: 1.0.0
components:
schemas:
Animal:
oneOf:
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Cat'
Dog:
type: object
properties:
bark:
$ref: 'actions.yaml#/components/schemas/Bark'
Cat:
type: object
properties:
meow:
$ref: 'actions.yaml#/components/schemas/Meow'

0 comments on commit 51fae97

Please sign in to comment.