-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fail on missing properties. Signed-off-by: dblock <[email protected]>
- Loading branch information
Showing
22 changed files
with
233 additions
and
35 deletions.
There are no files selected for viewing
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
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
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: any) => { | ||
if (schema.required !== undefined && schema.properties !== undefined && schema.additionalProperties === undefined) { | ||
// causes any undeclared field in the response to produce an error | ||
schema.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
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,36 @@ | ||
/* | ||
* 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" | ||
|
||
describe('additionalProperties', () => { | ||
const spec = new MergedOpenApiSpec('tools/tests/tester/fixtures/specs/complete', new Logger()) | ||
const responses: any = spec.spec().components?.responses | ||
|
||
test('is added with required fields', () => { | ||
const schema = responses['info@200'].content['application/json'].schema | ||
expect(schema.additionalProperties).toEqual({ not: true, errorMessage: 'property is not defined in the spec' }) | ||
}) | ||
|
||
test('is not added when true', () => { | ||
const schema = responses['info@201'].content['application/json'].schema | ||
expect(schema.additionalProperties).toEqual(true) | ||
}) | ||
|
||
test('is not added when object', () => { | ||
const schema = responses['info@404'].content['application/json'].schema | ||
expect(schema.additionalProperties).toEqual({ type: 'object' }) | ||
}) | ||
|
||
test('is not added unless required is present', () => { | ||
const schema = responses['info@500'].content['application/json'].schema | ||
expect(schema.additionalProperties).toBeUndefined() | ||
}) | ||
}) |
File renamed without changes.
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 |
Oops, something went wrong.