Skip to content

Commit

Permalink
Fail on missing properties.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jun 17, 2024
1 parent 49cd814 commit 130f86c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
27 changes: 15 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@eslint/js": "^9.1.1",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"ajv-errors": "^3.0.0",
"eslint": "^8.57.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-eslint-comments": "^3.2.0",
Expand Down
3 changes: 0 additions & 3 deletions spec/schemas/_common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1800,8 +1800,6 @@ components:
type: boolean
build_type:
type: string
distribution:
type: string
lucene_version:
$ref: '#/components/schemas/VersionString'
minimum_index_compatibility_version:
Expand All @@ -1815,7 +1813,6 @@ components:
- build_hash
- build_snapshot
- build_type
- distribution
- lucene_version
- minimum_index_compatibility_version
- minimum_wire_compatibility_version
Expand Down
2 changes: 2 additions & 0 deletions tools/src/tester/SchemaValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import AJV from 'ajv'
import ajv_errors from 'ajv-errors'
import addFormats from 'ajv-formats'
import { type OpenAPIV3 } from 'openapi-types'
import { type Evaluation, Result } from './types/eval.types'
Expand All @@ -17,6 +18,7 @@ export default class SchemaValidator {
constructor (spec: OpenAPIV3.Document) {
this.ajv = new AJV({ allErrors: true, strict: true })
addFormats(this.ajv)
ajv_errors(this.ajv, { singleError: true })
this.ajv.addKeyword('discriminator')
const schemas = spec.components?.schemas ?? {}
for (const key in schemas) this.ajv.addSchema(schemas[key], `#/components/schemas/${key}`)
Expand Down
49 changes: 49 additions & 0 deletions tools/src/tester/StricterMergedOpenApiSpec.ts
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 StricterMergedOpenApiSpec {
logger: Logger
file_path: string
protected _spec: OpenAPIV3.Document | undefined

constructor (specPath: string, logger: Logger = new Logger()) {

Check failure on line 22 in tools/src/tester/StricterMergedOpenApiSpec.ts

View workflow job for this annotation

GitHub Actions / test

Parameter name `specPath` must match one of the following formats: snake_case, UPPER_CASE
this.logger = logger
this.file_path = specPath
}

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) {

Check failure on line 36 in tools/src/tester/StricterMergedOpenApiSpec.ts

View workflow job for this annotation

GitHub Actions / test

Missing return type on function
const visitor = new SchemaVisitor((_ctx, schema) => {
if ('properties' in schema) {
// 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)
}
}
5 changes: 2 additions & 3 deletions tools/src/tester/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* compatible open source license.
*/

import OpenApiMerger from '../merger/OpenApiMerger'
import { LogLevel, Logger } from '../Logger'
import TestRunner from './TestRunner'
import { Command, Option } from '@commander-js/extra-typings'
Expand All @@ -26,6 +25,7 @@ import StoryEvaluator from './StoryEvaluator'
import { ConsoleResultLogger } from './ResultLogger'
import * as process from 'node:process'
import SupplementalChapterEvaluator from './SupplementalChapterEvaluator'
import StricterMergedOpenApiSpec from './StricterMergedOpenApiSpec'

const command = new Command()
.description('Run test stories against the OpenSearch spec.')
Expand All @@ -48,8 +48,7 @@ const command = new Command()
const opts = command.opts()
const logger = new Logger(opts.verbose ? LogLevel.info : LogLevel.warn)

const spec = (new OpenApiMerger(opts.specPath, logger)).merge()

const spec = (new StricterMergedOpenApiSpec(opts.specPath, logger)).spec()
const http_client = new OpenSearchHttpClient(get_opensearch_opts_from_cli(opts))
const chapter_reader = new ChapterReader(http_client)
const chapter_evaluator = new ChapterEvaluator(new OperationLocator(spec), chapter_reader, new SchemaValidator(spec))
Expand Down

0 comments on commit 130f86c

Please sign in to comment.