Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
Replace final parser.dereference to speed up merges (#1203) (#1219)
Browse files Browse the repository at this point in the history
* Start exploring schema verification.

* Verify $ref as part of verifySchema
This makes merging much faster.

Co-authored-by: Chris McConnell <chrimc>

Co-authored-by: Chris McConnell <[email protected]>
  • Loading branch information
munozemilio and chrimc62 authored Apr 26, 2021
1 parent 639b6a1 commit 71bced4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"999999",
"--colors",
"-g",
"dialog:merge.*"
"dialog:merge.*missing schema.*"
],
"cwd": "${workspaceFolder}/packages/dialog",
"internalConsoleOptions": "openOnSessionStart",
Expand Down
44 changes: 25 additions & 19 deletions common/config/rush/pnpm-lock.yaml

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

23 changes: 12 additions & 11 deletions packages/dialog/src/library/schemaMerger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,7 @@ export class SchemaMerger {
// Final verification
this.verifySchema(finalSchema)
if (!this.failed) {
// Verify all refs work
const start = process.hrtime.bigint()
fullSchema = await parser.dereference(clone(finalSchema))
const end = process.hrtime.bigint()
const elapsed = Number(end - start) / 1000000000
this.vlog(`Expanding all $ref took ${elapsed} seconds`)
fullSchema = finalSchema
if (!this.checkOnly) {
this.log(`Writing ${this.currentFile}`)
await fs.writeJSON(this.currentFile, finalSchema, this.jsonOptions)
Expand Down Expand Up @@ -1954,14 +1949,19 @@ export class SchemaMerger {
const definition = schema.definitions[this.currentKind]
const verifyProperty = (val, path) => {
if (val.$ref) {
val = clone(val)
const ref: any = ptr.get(schema, val.$ref)
for (const prop in ref) {
if (!val[prop]) {
val[prop] = ref[prop]
if (!ref) {
this.mergingError(`${path} $ref ${val.$ref} does not exist`)
} else {
// Expand $ref to check locally
val = clone(val)
for (const prop in ref) {
if (!val[prop]) {
val[prop] = ref[prop]
}
}
delete val.$ref
}
delete val.$ref
}
if (!val.$schema) {
// Assume $schema is an external reference and ignore error checking
Expand Down Expand Up @@ -1995,6 +1995,7 @@ export class SchemaMerger {
}
walkJSON(definition, (val, _, path) => {
if (val.$schema && path) {
// Embedded non-component schema
return true
}
if (val.properties && (!path || !path.endsWith('properties'))) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dialog/test/commands/dialog/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('dialog:merge', async () => {
console.log('\nStart missing schema reference')
const [merged, lines] = await merge(['schemas/*.schema', 'schemas/badSchemas/missingSchemaRef.schema'])
assert(!merged, 'Merging should have failed')
assert(countMatches(/error|warning/i, lines) === 1, 'Wrong number of errors or warnings')
assert(countMatches(/error|warning/i, lines) === 3, 'Wrong number of errors or warnings')
assert(countMatches('does not exist', lines) === 1, 'Did not detect missing schema ref')
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://schemas.botframework.com/schemas/component/v1.0/component.schema",
"$role": [],
"properties": {
"foo": {
"$ref": "schema:#/definitions/foo"
Expand Down

0 comments on commit 71bced4

Please sign in to comment.