Skip to content

Commit

Permalink
fix #3700 - Compiling Schema mergeAllOf errors (#3701)
Browse files Browse the repository at this point in the history
* fix #3700 - Compiling Schema mergeAllOf errors

* update changelog with fixes

* add soruce comment for ordering json fields
  • Loading branch information
cwendtxealth authored May 30, 2023
1 parent 76b82d1 commit eea5bd0
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 125 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ should change the heading of the (upcoming) version to include a major version b

# 5.8.0

## @rjsf/utils

- Updated `retrieveSchemaInternal` to return failed merged allOf sub schemas for expandAllBranches flag, fixing [#3689](https://github.com/rjsf-team/react-jsonschema-form/issues/3700)
- Updated `hashForSchema` to short schema fields in consistent order before stringify to prevent different hash ids for the same schema

## @rjsf/bootstrap-4

- Updated FieldTemplate Component to display description from SchemaField and make it consistent for all the available themes
Expand Down
8 changes: 6 additions & 2 deletions packages/utils/src/hashForSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ function hashString(string: string): string {
return hash.toString(16);
}

/** Stringifies the schema and returns the hash of the resulting string.
/** Stringifies the schema and returns the hash of the resulting string. Sorts schema fields
* in consistent order before stringify to prevent different hash ids for the same schema.
*
* @param schema - The schema for which the hash is desired
* @returns - The string obtained from the hash of the stringified schema
*/
export default function hashForSchema<S extends StrictRJSFSchema = RJSFSchema>(schema: S) {
return hashString(JSON.stringify(schema));
const allKeys = new Set<string>();
// solution source: https://stackoverflow.com/questions/16167581/sort-object-properties-and-json-stringify/53593328#53593328
JSON.stringify(schema, (key, value) => (allKeys.add(key), value));
return hashString(JSON.stringify(schema, Array.from(allKeys).sort()));
}
3 changes: 3 additions & 0 deletions packages/utils/src/schema/retrieveSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ export function retrieveSchemaInternal<
} catch (e) {
console.warn('could not merge subschemas in allOf:\n', e);
const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;
if (expandAllBranches && allOf) {
return [resolvedSchemaWithoutAllOf as S, ...(allOf as S[])];
}
return resolvedSchemaWithoutAllOf as S;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/test/__snapshots__/hashFromSchema.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`hashForSchema returns a hash for a more complex schema 1`] = `"-6bbb062a"`;
exports[`hashForSchema returns a hash for a more complex schema 1`] = `"7cf4cd8a"`;

exports[`hashForSchema returns a hash for a tiny schema 1`] = `"5b2fed3d"`;
exports[`hashForSchema returns a hash for a tiny schema 1`] = `"2faae26f"`;
5 changes: 5 additions & 0 deletions packages/utils/test/hashFromSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ describe('hashForSchema', () => {
it('returns a hash for a more complex schema', () => {
expect(hashForSchema(RECURSIVE_REF)).toMatchSnapshot();
});
it('returns same hash for two schemas but fields are different order', () => {
const schema1: RJSFSchema = { type: 'string', title: 'order' };
const schema2: RJSFSchema = { title: 'order', type: 'string' };
expect(hashForSchema(schema1)).toBe(hashForSchema(schema2));
});
});
Loading

0 comments on commit eea5bd0

Please sign in to comment.