From 0ebc82ecc33d0a34bd63be98d959551fbd21f660 Mon Sep 17 00:00:00 2001 From: Nick Grosenbacher Date: Fri, 28 Jul 2023 14:25:15 -0400 Subject: [PATCH] [fix] Fix #3792, handle null case in `withIdRefPrefix` (#3794) Co-authored-by: Heath C <51679588+heath-freenome@users.noreply.github.com> --- CHANGELOG.md | 1 + packages/utils/src/withIdRefPrefix.ts | 7 ++++--- packages/utils/test/withIdRef.test.ts | 4 ++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cee338e53c..acb02e00c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ should change the heading of the (upcoming) version to include a major version b - Created new `resolveAllReferences()` function to resolve all references within a schema's properties and array items. - Updated `getClosestMatchingOption()` to use `resolveAllReferences()` for all oneOf/anyOf schemas - Updated `resolveAnyOrOneOfSchemas()` to use `resolveAllReferences()` for all oneOf/anyOf schemas +- Better handle the `null` case in `withIdRefPrefix`, fixing [#3792](https://github.com/rjsf-team/react-jsonschema-form/pull/3792) # 5.11.0 diff --git a/packages/utils/src/withIdRefPrefix.ts b/packages/utils/src/withIdRefPrefix.ts index 20c463b04f..e0c7b26c04 100644 --- a/packages/utils/src/withIdRefPrefix.ts +++ b/packages/utils/src/withIdRefPrefix.ts @@ -1,5 +1,6 @@ import { REF_KEY, ROOT_SCHEMA_PREFIX } from './constants'; import { RJSFSchema, StrictRJSFSchema } from './types'; +import isObject from 'lodash/isObject'; /** Takes a `node` object and transforms any contained `$ref` node variables with a prefix, recursively calling * `withIdRefPrefix` for any other elements. @@ -38,11 +39,11 @@ function withIdRefPrefixArray(node: S[] * @returns - A copy of the `schemaNode` with updated `$ref`s */ export default function withIdRefPrefix(schemaNode: S): S | S[] { - if (schemaNode.constructor === Object) { - return withIdRefPrefixObject({ ...schemaNode }); - } if (Array.isArray(schemaNode)) { return withIdRefPrefixArray([...schemaNode]); } + if (isObject(schemaNode)) { + return withIdRefPrefixObject({ ...schemaNode }); + } return schemaNode; } diff --git a/packages/utils/test/withIdRef.test.ts b/packages/utils/test/withIdRef.test.ts index 36da1ca576..17a63dbfb0 100644 --- a/packages/utils/test/withIdRef.test.ts +++ b/packages/utils/test/withIdRef.test.ts @@ -34,4 +34,8 @@ describe('withIdRefPrefix()', () => { expect(withIdRefPrefix(schema)).toEqual(schema); }); + it('should handle null schema', () => { + const schema: RJSFSchema = null; + expect(withIdRefPrefix(schema)).toEqual(schema); + }); });