Skip to content

Commit

Permalink
Replaced JSON.stringify with to_json.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jun 18, 2024
1 parent 53e6da0 commit ab57a68
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
6 changes: 5 additions & 1 deletion tools/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand Down
4 changes: 2 additions & 2 deletions tools/src/linter/components/base/FileValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)}`)
}
}
}
7 changes: 4 additions & 3 deletions tools/src/tester/ChapterReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tools/src/tester/MergedOpenApiSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,7 +43,7 @@ export default class MergedOpenApiSpec {
}
}
})

visitor.visit_specification(ctx, spec)
}
}
5 changes: 3 additions & 2 deletions tools/src/tester/SchemaValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit ab57a68

Please sign in to comment.