-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace legacy mechanism for looking up key name from definitions
- Loading branch information
Showing
5 changed files
with
57,637 additions
and
84,404 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,113 @@ | ||
import {isPlainObject} from 'lodash' | ||
import {DereferencedPaths} from './resolver' | ||
import {AnnotatedJSONSchema, JSONSchema, Parent, isAnnotated} from './types/JSONSchema' | ||
import {AnnotatedJSONSchema, JSONSchema, Parent, KeyNameFromDefinition, isAnnotated} from './types/JSONSchema' | ||
|
||
/** | ||
* Traverses over the schema, assigning to each | ||
* node metadata that will be used downstream. | ||
*/ | ||
export function annotate( | ||
schema: JSONSchema, | ||
dereferencedPaths: DereferencedPaths, | ||
parent: JSONSchema | null = null, | ||
): AnnotatedJSONSchema { | ||
if (!Array.isArray(schema) && !isPlainObject(schema)) { | ||
return schema as AnnotatedJSONSchema | ||
const annotators = new Set< | ||
(schema: JSONSchema, parent: JSONSchema | null, dereferencedPaths: DereferencedPaths) => void | ||
>() | ||
|
||
// note: this must run first, since it is used by the next annotator | ||
annotators.add(function annotateParent(schema, parent) { | ||
Object.defineProperty(schema, Parent, { | ||
enumerable: false, | ||
value: parent, | ||
writable: false, | ||
}) | ||
}) | ||
|
||
annotators.add(function annotateKeyNameFromDefinition(schema) { | ||
const keyNameFromSchema = getDefinitionKeyNameFromParent(schema as AnnotatedJSONSchema) // todo | ||
if (!keyNameFromSchema) { | ||
return | ||
} | ||
|
||
// Handle cycles | ||
if (isAnnotated(schema)) { | ||
return schema | ||
Object.defineProperty(schema, KeyNameFromDefinition, { | ||
enumerable: false, | ||
value: keyNameFromSchema, | ||
writable: false, | ||
}) | ||
}) | ||
|
||
annotators.add(function annotateKeyNameFrom$Ref(schema, _, dereferencedPaths) { | ||
if (schema.hasOwnProperty(KeyNameFromDefinition)) { | ||
return | ||
} | ||
|
||
// Add a reference to this schema's parent | ||
Object.defineProperty(schema, Parent, { | ||
const ref = dereferencedPaths.get(schema) | ||
if (!ref) { | ||
return | ||
} | ||
|
||
const keyNameFromRef = getDefinitionKeyNameFromRef(ref) | ||
if (!keyNameFromRef) { | ||
return | ||
} | ||
|
||
Object.defineProperty(schema, KeyNameFromDefinition, { | ||
enumerable: false, | ||
value: parent, | ||
value: keyNameFromRef, | ||
writable: false, | ||
}) | ||
}) | ||
|
||
// Arrays | ||
if (Array.isArray(schema)) { | ||
schema.forEach(child => annotate(child, dereferencedPaths, schema)) | ||
/** | ||
* If this schema came from a `definitions` block, returns the key name for the definition. | ||
*/ | ||
function getDefinitionKeyNameFromParent(schema: AnnotatedJSONSchema): string | undefined { | ||
const parent = schema[Parent] | ||
if (!parent) { | ||
return | ||
} | ||
const grandparent = parent[Parent] | ||
if (!grandparent) { | ||
return | ||
} | ||
if (Object.keys(grandparent).find(_ => grandparent[_] === parent) !== 'definitions') { | ||
return | ||
} | ||
return Object.keys(parent).find(_ => parent[_] === schema) | ||
} | ||
|
||
// Objects | ||
for (const key in schema) { | ||
annotate(schema[key], dereferencedPaths, schema) | ||
function getDefinitionKeyNameFromRef(ref: string): string | undefined { | ||
const parts = ref.split('/') | ||
if (parts[parts.length - 2] !== 'definitions') { | ||
return | ||
} | ||
return parts[parts.length - 1] | ||
} | ||
|
||
/** | ||
* Traverses over the schema, assigning to each | ||
* node metadata that will be used downstream. | ||
*/ | ||
export function annotate(schema: JSONSchema, dereferencedPaths: DereferencedPaths): AnnotatedJSONSchema { | ||
function go(s: JSONSchema, parent: JSONSchema | null): void { | ||
if (!Array.isArray(s) && !isPlainObject(s)) { | ||
return | ||
} | ||
|
||
// Handle cycles | ||
if (isAnnotated(s)) { | ||
return | ||
} | ||
|
||
// Run annotators | ||
annotators.forEach(f => { | ||
f(s, parent, dereferencedPaths) | ||
}) | ||
|
||
// Handle arrays | ||
if (Array.isArray(s)) { | ||
s.forEach(_ => go(_, s)) | ||
} | ||
|
||
// Handle objects | ||
for (const key in s) { | ||
go(s[key], s) | ||
} | ||
} | ||
|
||
go(schema, null) | ||
|
||
return schema as AnnotatedJSONSchema | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.