-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Always make all references absolute in nested bundled schemas #4683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
import jsonpointer from 'jsonpointer'; | ||
import omit from 'lodash/omit'; | ||
|
||
import { ID_KEY, JSON_SCHEMA_DRAFT_2020_12, REF_KEY, SCHEMA_KEY } from './constants'; | ||
import { | ||
ALL_OF_KEY, | ||
ID_KEY, | ||
JSON_SCHEMA_DRAFT_2019_09, | ||
JSON_SCHEMA_DRAFT_2020_12, | ||
REF_KEY, | ||
SCHEMA_KEY, | ||
} from './constants'; | ||
import { GenericObjectType, RJSFSchema, StrictRJSFSchema } from './types'; | ||
import isObject from 'lodash/isObject'; | ||
import isEmpty from 'lodash/isEmpty'; | ||
|
@@ -20,7 +27,16 @@ function findEmbeddedSchemaRecursive<S extends StrictRJSFSchema = RJSFSchema>(sc | |
return schema; | ||
} | ||
for (const subSchema of Object.values(schema)) { | ||
if (isObject(subSchema)) { | ||
if (Array.isArray(subSchema)) { | ||
for (const item of subSchema) { | ||
if (isObject(item)) { | ||
const result = findEmbeddedSchemaRecursive<S>(item as S, ref); | ||
if (result !== undefined) { | ||
return result as S; | ||
} | ||
} | ||
} | ||
} else if (isObject(subSchema)) { | ||
const result = findEmbeddedSchemaRecursive<S>(subSchema as S, ref); | ||
if (result !== undefined) { | ||
return result as S; | ||
|
@@ -30,6 +46,31 @@ function findEmbeddedSchemaRecursive<S extends StrictRJSFSchema = RJSFSchema>(sc | |
return undefined; | ||
} | ||
|
||
/** Parses a JSONSchema and makes all references absolute with respect to | ||
* the `baseURI` argument | ||
* @param schema - The schema to be processed | ||
* @param baseURI - The base URI to be used for resolving relative references | ||
*/ | ||
function makeAllReferencesAbsolute<S extends StrictRJSFSchema = RJSFSchema>(schema: S, baseURI: string): S { | ||
const currentURI = get(schema, ID_KEY, baseURI); | ||
// Make all other references absolute | ||
if (REF_KEY in schema) { | ||
schema = { ...schema, [REF_KEY]: UriResolver.resolve(currentURI, schema[REF_KEY]!) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we are manipulating the root schema here or the lower-level schema as we are calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I just updated the |
||
} | ||
// Look for references in nested subschemas | ||
for (const [key, subSchema] of Object.entries(schema)) { | ||
if (Array.isArray(subSchema)) { | ||
schema = { | ||
...schema, | ||
[key]: subSchema.map((item) => (isObject(item) ? makeAllReferencesAbsolute(item as S, currentURI) : item)), | ||
}; | ||
} else if (isObject(subSchema)) { | ||
schema = { ...schema, [key]: makeAllReferencesAbsolute(subSchema as S, currentURI) }; | ||
} | ||
} | ||
return schema; | ||
} | ||
|
||
/** Splits out the value at the `key` in `object` from the `object`, returning an array that contains in the first | ||
* location, the `object` minus the `key: value` and in the second location the `value`. | ||
* | ||
|
@@ -73,6 +114,9 @@ export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFS | |
current = findEmbeddedSchemaRecursive<S>(rootSchema, baseURI.replace(/\/$/, '')); | ||
if (current !== undefined) { | ||
current = jsonpointer.get(current, decodedRef); | ||
if (current !== undefined) { | ||
current = makeAllReferencesAbsolute(current, current[ID_KEY]!); | ||
} | ||
} | ||
} | ||
} else if (rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) { | ||
|
@@ -84,6 +128,9 @@ export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFS | |
if (!isEmpty(refAnchor)) { | ||
current = jsonpointer.get(current, decodeURIComponent(refAnchor.join('#'))); | ||
} | ||
if (current !== undefined) { | ||
current = makeAllReferencesAbsolute(current!, baseURI!); | ||
} | ||
} | ||
} | ||
if (current === undefined) { | ||
|
@@ -103,7 +150,14 @@ export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFS | |
const [remaining, theRef] = splitKeyElementFromObject(REF_KEY, current); | ||
const subSchema = findSchemaDefinitionRecursive<S>(theRef, rootSchema, [...recurseList, ref], baseURI); | ||
if (Object.keys(remaining).length > 0) { | ||
return { ...remaining, ...subSchema }; | ||
if ( | ||
rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2019_09 || | ||
rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12 | ||
) { | ||
return { [ALL_OF_KEY]: [remaining, subSchema] } as S; | ||
} else { | ||
return { ...remaining, ...subSchema }; | ||
} | ||
Comment on lines
+153
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have different logic for different standard versions? Is the |
||
} | ||
return subSchema; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.