diff --git a/tools/src/helpers.ts b/tools/src/helpers.ts index 790407497..bb95fd253 100644 --- a/tools/src/helpers.ts +++ b/tools/src/helpers.ts @@ -93,8 +93,12 @@ export function write_yaml (file_path: string, content: any): void { })) } +export function to_json(content: any, replacer?: (this: any, key: string, value: any) => any): string { + return JSON.stringify(content, replacer, 2) +} + export function write_json (file_path: string, content: any, replacer?: (this: any, key: string, value: any) => any): void { - write_text(file_path, JSON.stringify(content, replacer, 2)) + write_text(file_path, to_json(content, replacer)) } export async function sleep (ms: number): Promise { diff --git a/tools/src/linter/components/base/FileValidator.ts b/tools/src/linter/components/base/FileValidator.ts index 53a887014..a6f9da82e 100644 --- a/tools/src/linter/components/base/FileValidator.ts +++ b/tools/src/linter/components/base/FileValidator.ts @@ -10,7 +10,7 @@ import ValidatorBase from './ValidatorBase' import { type ValidationError } from 'types' import { type OpenAPIV3 } from 'openapi-types' -import { read_yaml } from '../../../helpers' +import { read_yaml, to_json } from '../../../helpers' import AJV from 'ajv' import addFormats from 'ajv-formats' @@ -65,7 +65,7 @@ export default class FileValidator extends ValidatorBase { addFormats(ajv) const validator = ajv.compile(schema) if (!validator(this.spec())) { - return this.error(`File content does not match JSON schema found in '${json_schema_path}':\n ${JSON.stringify(validator.errors, null, 2)}`) + return this.error(`File content does not match JSON schema found in '${json_schema_path}':\n ${to_json(validator.errors)}`) } } } diff --git a/tools/src/tester/ChapterReader.ts b/tools/src/tester/ChapterReader.ts index 827d2e181..04ff18b1d 100644 --- a/tools/src/tester/ChapterReader.ts +++ b/tools/src/tester/ChapterReader.ts @@ -11,6 +11,7 @@ import { type ChapterRequest, type ActualResponse, type Parameter } from './type import { type OpenSearchHttpClient } from '../OpenSearchHttpClient' import { type StoryOutputs } from './StoryOutputs' import { Logger } from 'Logger' +import { to_json } from '../helpers' // A lightweight client for testing the API export default class ChapterReader { @@ -27,14 +28,14 @@ export default class ChapterReader { const resolved_params = story_outputs.resolve_params(chapter.parameters ?? {}) const [url_path, params] = this.#parse_url(chapter.path, resolved_params) const request_data = chapter.request_body?.payload !== undefined ? story_outputs.resolve_value(chapter.request_body.payload) : undefined - this.logger.info(`=> ${chapter.method} ${url_path} (${JSON.stringify(params, null, 2)}) | ${JSON.stringify(request_data, null, 2)}`) + this.logger.info(`=> ${chapter.method} ${url_path} (${to_json(params)}) | ${to_json(request_data)}`) await this._client.request({ url: url_path, method: chapter.method, params, data: request_data }).then(r => { - this.logger.info(`<= ${r.status} (${r.headers['content-type']}) | ${JSON.stringify(r.data, null, 2)}`) + this.logger.info(`<= ${r.status} (${r.headers['content-type']}) | ${to_json(r.data)}`) response.status = r.status response.content_type = r.headers['content-type'].split(';')[0] response.payload = r.data @@ -43,7 +44,7 @@ export default class ChapterReader { this.logger.info(`<= ERROR: ${e}`) throw e } - this.logger.info(`<= ${e.response.status} (${e.response.headers['content-type']}) | ${JSON.stringify(e.response.data, null, 2)}`) + this.logger.info(`<= ${e.response.status} (${e.response.headers['content-type']}) | ${to_json(e.response.data)}`) response.status = e.response.status response.content_type = e.response.headers['content-type'].split(';')[0] response.payload = e.response.data?.error diff --git a/tools/src/tester/MergedOpenApiSpec.ts b/tools/src/tester/MergedOpenApiSpec.ts index 903526dc1..8ff253157 100644 --- a/tools/src/tester/MergedOpenApiSpec.ts +++ b/tools/src/tester/MergedOpenApiSpec.ts @@ -21,12 +21,12 @@ export default class MergedOpenApiSpec { constructor (spec_path: string, logger: Logger = new Logger()) { this.logger = logger - this.file_path = spec_path + 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 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 @@ -43,7 +43,7 @@ export default class MergedOpenApiSpec { } } }) - + visitor.visit_specification(ctx, spec) } } diff --git a/tools/src/tester/SchemaValidator.ts b/tools/src/tester/SchemaValidator.ts index 380d891b2..7d13320f9 100644 --- a/tools/src/tester/SchemaValidator.ts +++ b/tools/src/tester/SchemaValidator.ts @@ -13,6 +13,7 @@ import addFormats from 'ajv-formats' import { type OpenAPIV3 } from 'openapi-types' import { type Evaluation, Result } from './types/eval.types' import { Logger } from 'Logger' +import { to_json } from '../helpers' export default class SchemaValidator { private readonly ajv: AJV @@ -32,8 +33,8 @@ export default class SchemaValidator { const validate = this.ajv.compile(schema) const valid = validate(data) if (! valid) { - this.logger.info(`# ${JSON.stringify(schema, null, 2)}`) - this.logger.info(`* ${JSON.stringify(data, null, 2)}`) + this.logger.info(`# ${to_json(schema)}`) + this.logger.info(`* ${to_json(data)}`) } return { result: valid ? Result.PASSED : Result.FAILED,