-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: dblock <[email protected]>
- Loading branch information
Showing
14 changed files
with
155 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { type OpenAPIV3 } from 'openapi-types' | ||
import { Logger } from '../Logger' | ||
import { SpecificationContext } from '../linter/utils'; | ||
import { SchemaVisitor } from '../linter/utils/SpecificationVisitor'; | ||
import OpenApiMerger from '../merger/OpenApiMerger'; | ||
|
||
// An augmented spec with additionalProperties: false. | ||
export default class MergedOpenApiSpec { | ||
logger: Logger | ||
file_path: string | ||
protected _spec: OpenAPIV3.Document | undefined | ||
|
||
constructor (spec_path: string, logger: Logger = new Logger()) { | ||
this.logger = logger | ||
this.file_path = spec_path | ||
} | ||
|
||
spec (): OpenAPIV3.Document { | ||
if (this._spec) return this._spec | ||
const spec = (new OpenApiMerger(this.file_path, this.logger)).merge() | ||
const ctx = new SpecificationContext(this.file_path) | ||
this.inject_additional_properties(ctx, spec) | ||
this._spec = spec | ||
return this._spec | ||
} | ||
|
||
private inject_additional_properties(ctx: SpecificationContext, spec: OpenAPIV3.Document): void { | ||
const visitor = new SchemaVisitor((_ctx, schema) => { | ||
if ('properties' in schema && schema.additionalProperties === undefined) { | ||
// causes any undeclared field in the response to produce an error | ||
(schema as any).additionalProperties = { | ||
not: true, | ||
errorMessage: "property is not defined in the spec" | ||
} | ||
} | ||
}); | ||
|
||
visitor.visit_specification(ctx, spec) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Logger } from 'Logger' | ||
import MergedOpenApiSpec from "tester/MergedOpenApiSpec" | ||
|
||
const spec = new MergedOpenApiSpec('tools/tests/tester/fixtures/specs/complete', new Logger()) | ||
|
||
test('adds additionalProperties when not present', () => { | ||
const responses: any = spec.spec().components?.responses | ||
const schema = responses['info@200'].content['application/json'].schema | ||
expect(schema.additionalProperties).toEqual({ not: true, errorMessage: 'property is not defined in the spec' }) | ||
}) | ||
|
||
test('does not add additionalProperties when present', () => { | ||
const responses: any = spec.spec().components?.responses | ||
const schema = responses['info@201'].content['application/json'].schema | ||
expect(schema.additionalProperties).toEqual(true) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tools/tests/tester/fixtures/specs/complete/_global_parameters.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: '' | ||
version: '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$schema: should-be-ignored | ||
|
||
title: OpenSearch API | ||
description: OpenSearch API | ||
version: 1.0.0 |
1 change: 1 addition & 0 deletions
1
tools/tests/tester/fixtures/specs/complete/_superseded_operations.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$schema: should-be-ignored |
39 changes: 39 additions & 0 deletions
39
tools/tests/tester/fixtures/specs/complete/namespaces/index.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: OpenSearch API | ||
description: OpenSearch API | ||
version: 1.0.0 | ||
paths: | ||
'/index': | ||
get: | ||
operationId: get.0 | ||
responses: | ||
'200': | ||
$ref: '#/components/responses/info@200' | ||
'201': | ||
$ref: '#/components/responses/info@201' | ||
components: | ||
responses: | ||
info@200: | ||
description: '' | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
tagline: | ||
type: string | ||
required: | ||
- tagline | ||
info@201: | ||
description: '' | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
tagline: | ||
type: string | ||
required: | ||
- tagline | ||
additionalProperties: true |
5 changes: 5 additions & 0 deletions
5
tools/tests/tester/fixtures/specs/complete/schemas/actions.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: OpenSearch API | ||
description: OpenSearch API | ||
version: 1.0.0 |