Skip to content

Commit

Permalink
[fix] Fix #3792, handle null case in withIdRefPrefix (#3794)
Browse files Browse the repository at this point in the history
Co-authored-by: Heath C <51679588+heath-freenome@users.noreply.github.com>
  • Loading branch information
nickgros and heath-freenome committed Jul 28, 2023
1 parent 30edd48 commit 0ebc82e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions packages/utils/src/withIdRefPrefix.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -38,11 +39,11 @@ function withIdRefPrefixArray<S extends StrictRJSFSchema = RJSFSchema>(node: S[]
* @returns - A copy of the `schemaNode` with updated `$ref`s
*/
export default function withIdRefPrefix<S extends StrictRJSFSchema = RJSFSchema>(schemaNode: S): S | S[] {
if (schemaNode.constructor === Object) {
return withIdRefPrefixObject<S>({ ...schemaNode });
}
if (Array.isArray(schemaNode)) {
return withIdRefPrefixArray<S>([...schemaNode]);
}
if (isObject(schemaNode)) {
return withIdRefPrefixObject<S>({ ...schemaNode });
}
return schemaNode;
}
4 changes: 4 additions & 0 deletions packages/utils/test/withIdRef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 0ebc82e

Please sign in to comment.