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

Commit 71bced4

Browse files
munozemilioChris McConnell
andauthored
Replace final parser.dereference to speed up merges (#1203) (#1219)
* 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]>
1 parent 639b6a1 commit 71bced4

File tree

5 files changed

+40
-32
lines changed

5 files changed

+40
-32
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"999999",
5959
"--colors",
6060
"-g",
61-
"dialog:merge.*"
61+
"dialog:merge.*missing schema.*"
6262
],
6363
"cwd": "${workspaceFolder}/packages/dialog",
6464
"internalConsoleOptions": "openOnSessionStart",

common/config/rush/pnpm-lock.yaml

Lines changed: 25 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dialog/src/library/schemaMerger.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,7 @@ export class SchemaMerger {
682682
// Final verification
683683
this.verifySchema(finalSchema)
684684
if (!this.failed) {
685-
// Verify all refs work
686-
const start = process.hrtime.bigint()
687-
fullSchema = await parser.dereference(clone(finalSchema))
688-
const end = process.hrtime.bigint()
689-
const elapsed = Number(end - start) / 1000000000
690-
this.vlog(`Expanding all $ref took ${elapsed} seconds`)
685+
fullSchema = finalSchema
691686
if (!this.checkOnly) {
692687
this.log(`Writing ${this.currentFile}`)
693688
await fs.writeJSON(this.currentFile, finalSchema, this.jsonOptions)
@@ -1954,14 +1949,19 @@ export class SchemaMerger {
19541949
const definition = schema.definitions[this.currentKind]
19551950
const verifyProperty = (val, path) => {
19561951
if (val.$ref) {
1957-
val = clone(val)
19581952
const ref: any = ptr.get(schema, val.$ref)
1959-
for (const prop in ref) {
1960-
if (!val[prop]) {
1961-
val[prop] = ref[prop]
1953+
if (!ref) {
1954+
this.mergingError(`${path} $ref ${val.$ref} does not exist`)
1955+
} else {
1956+
// Expand $ref to check locally
1957+
val = clone(val)
1958+
for (const prop in ref) {
1959+
if (!val[prop]) {
1960+
val[prop] = ref[prop]
1961+
}
19621962
}
1963+
delete val.$ref
19631964
}
1964-
delete val.$ref
19651965
}
19661966
if (!val.$schema) {
19671967
// Assume $schema is an external reference and ignore error checking
@@ -1995,6 +1995,7 @@ export class SchemaMerger {
19951995
}
19961996
walkJSON(definition, (val, _, path) => {
19971997
if (val.$schema && path) {
1998+
// Embedded non-component schema
19981999
return true
19992000
}
20002001
if (val.properties && (!path || !path.endsWith('properties'))) {

packages/dialog/test/commands/dialog/merge.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ describe('dialog:merge', async () => {
195195
console.log('\nStart missing schema reference')
196196
const [merged, lines] = await merge(['schemas/*.schema', 'schemas/badSchemas/missingSchemaRef.schema'])
197197
assert(!merged, 'Merging should have failed')
198-
assert(countMatches(/error|warning/i, lines) === 1, 'Wrong number of errors or warnings')
198+
assert(countMatches(/error|warning/i, lines) === 3, 'Wrong number of errors or warnings')
199199
assert(countMatches('does not exist', lines) === 1, 'Did not detect missing schema ref')
200200
})
201201

packages/dialog/test/commands/dialog/schemas/badSchemas/missingSchemaRef.schema

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"$schema": "https://schemas.botframework.com/schemas/component/v1.0/component.schema",
3+
"$role": [],
34
"properties": {
45
"foo": {
56
"$ref": "schema:#/definitions/foo"

0 commit comments

Comments
 (0)