Skip to content

Commit f6bcf5f

Browse files
GlassOfWhiskeynickgros
authored andcommitted
Applied required changes
1 parent 1ff11c2 commit f6bcf5f

File tree

5 files changed

+99
-66
lines changed

5 files changed

+99
-66
lines changed

CHANGELOG.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ should change the heading of the (upcoming) version to include a major version b
3232
- Extended `Registry` interface to include optional `experimental_componentUpdateStrategy` property
3333
- Added `shallowEquals()` utility function for shallow equality comparisons
3434

35+
# 6.0.0-beta.13
36+
37+
## rjsf/utils
38+
39+
- Always make all references absolute in nested bundled schemas
40+
3541
# 6.0.0-beta.12
3642

3743
## @rjsf/core
@@ -46,13 +52,6 @@ should change the heading of the (upcoming) version to include a major version b
4652

4753
- Updated `SchemaUtils` and `createSchemaUtils()` to add a new `getRootSchema()` function
4854

49-
# 6.0.0-beta.11
50-
51-
## rjsf/utils
52-
53-
- Always make all references absolute in nested bundled schemas
54-
55-
5655
# 6.0.0-beta.11
5756

5857
## @rjsf/antd

packages/utils/src/createSchemaUtils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import {
2929
toIdSchema,
3030
toPathSchema,
3131
} from './schema';
32+
import { makeAllReferencesAbsolute } from './findSchemaDefinition';
33+
import { ID_KEY, JSON_SCHEMA_DRAFT_2020_12, SCHEMA_KEY } from './constants';
34+
import get from 'lodash/get';
3235

3336
/** The `SchemaUtils` class provides a wrapper around the publicly exported APIs in the `utils/schema` directory such
3437
* that one does not have to explicitly pass the `validator`, `rootSchema`, `experimental_defaultFormStateBehavior` or
@@ -57,7 +60,11 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
5760
experimental_defaultFormStateBehavior: Experimental_DefaultFormStateBehavior,
5861
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
5962
) {
60-
this.rootSchema = rootSchema;
63+
if (rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) {
64+
this.rootSchema = makeAllReferencesAbsolute(rootSchema, get(rootSchema, ID_KEY, '#'));
65+
} else {
66+
this.rootSchema = rootSchema;
67+
}
6168
this.validator = validator;
6269
this.experimental_defaultFormStateBehavior = experimental_defaultFormStateBehavior;
6370
this.experimental_customMergeAllOf = experimental_customMergeAllOf;

packages/utils/src/findSchemaDefinition.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function findEmbeddedSchemaRecursive<S extends StrictRJSFSchema = RJSFSchema>(sc
5151
* @param schema - The schema to be processed
5252
* @param baseURI - The base URI to be used for resolving relative references
5353
*/
54-
function makeAllReferencesAbsolute<S extends StrictRJSFSchema = RJSFSchema>(schema: S, baseURI: string): S {
54+
export function makeAllReferencesAbsolute<S extends StrictRJSFSchema = RJSFSchema>(schema: S, baseURI: string): S {
5555
const currentURI = get(schema, ID_KEY, baseURI);
5656
// Make all other references absolute
5757
if (REF_KEY in schema) {
@@ -114,9 +114,6 @@ export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFS
114114
current = findEmbeddedSchemaRecursive<S>(rootSchema, baseURI.replace(/\/$/, ''));
115115
if (current !== undefined) {
116116
current = jsonpointer.get(current, decodedRef);
117-
if (current !== undefined) {
118-
current = makeAllReferencesAbsolute(current, current[ID_KEY]!);
119-
}
120117
}
121118
}
122119
} else if (rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) {
@@ -128,9 +125,6 @@ export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFS
128125
if (!isEmpty(refAnchor)) {
129126
current = jsonpointer.get(current, decodeURIComponent(refAnchor.join('#')));
130127
}
131-
if (current !== undefined) {
132-
current = makeAllReferencesAbsolute(current!, baseURI!);
133-
}
134128
}
135129
}
136130
if (current === undefined) {

packages/utils/test/createSchemaUtils.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import {
22
createSchemaUtils,
33
Experimental_DefaultFormStateBehavior,
4+
ID_KEY,
5+
JSON_SCHEMA_DRAFT_2020_12,
6+
PROPERTIES_KEY,
7+
REF_KEY,
48
RJSFSchema,
9+
SCHEMA_KEY,
510
SchemaUtilsType,
611
ValidatorType,
712
} from '../src';
@@ -23,6 +28,36 @@ describe('createSchemaUtils()', () => {
2328
expect(schemaUtils.getValidator()).toBe(testValidator);
2429
});
2530

31+
describe('2020-12 schema', () => {
32+
const rootSchema2020: RJSFSchema = {
33+
[SCHEMA_KEY]: JSON_SCHEMA_DRAFT_2020_12,
34+
[ID_KEY]: 'https://example.com/2020-12.json',
35+
type: 'object',
36+
$defs: {
37+
example: {
38+
type: 'integer',
39+
},
40+
},
41+
[PROPERTIES_KEY]: {
42+
ref: {
43+
[REF_KEY]: '#/$defs/example',
44+
},
45+
},
46+
};
47+
const schemaUtils2020: SchemaUtilsType = createSchemaUtils(testValidator, rootSchema2020, defaultFormStateBehavior);
48+
49+
it('getRootSchema()', () => {
50+
expect(schemaUtils2020.getRootSchema()).toEqual({
51+
...rootSchema2020,
52+
[PROPERTIES_KEY]: {
53+
ref: {
54+
[REF_KEY]: 'https://example.com/2020-12.json#/$defs/example',
55+
},
56+
},
57+
});
58+
});
59+
});
60+
2661
describe('doesSchemaUtilsDiffer()', () => {
2762
describe('constructed without defaultFormStateBehavior', () => {
2863
const schemaUtils: SchemaUtilsType = createSchemaUtils(testValidator, rootSchema);

packages/utils/test/findSchemaDefinition.test.ts

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { findSchemaDefinition, RJSFSchema } from '../src';
2-
import { findSchemaDefinitionRecursive } from '../src/findSchemaDefinition';
1+
import { findSchemaDefinition, ID_KEY, RJSFSchema } from '../src';
2+
import { findSchemaDefinitionRecursive, makeAllReferencesAbsolute } from '../src/findSchemaDefinition';
33

44
const schema: RJSFSchema = {
55
type: 'object',
@@ -85,29 +85,6 @@ const internalSchema: RJSFSchema = {
8585
},
8686
};
8787

88-
const absoluteInternalSchema: RJSFSchema = {
89-
...internalSchema,
90-
$defs: {
91-
...internalSchema.$defs,
92-
circularRef: { $ref: 'https://example.com/bundled.schema.json/#/$defs/circularRef' },
93-
undefinedRef: { $ref: 'https://example.com/bundled.ref.json#/$defs/undefined' },
94-
},
95-
properties: {
96-
...internalSchema.properties,
97-
string: { $ref: 'https://example.com/bundled.ref.json#/$defs/string' },
98-
allOf: {
99-
allOf: [
100-
{
101-
$ref: 'https://example.com/bundled.ref.json#/$defs/string',
102-
},
103-
{
104-
title: 'String',
105-
},
106-
],
107-
},
108-
},
109-
};
110-
11188
const bundledSchema: RJSFSchema = {
11289
$schema: 'https://json-schema.org/draft/2020-12/schema',
11390
type: 'object',
@@ -209,11 +186,11 @@ describe('findSchemaDefinition()', () => {
209186
);
210187
});
211188
it('correctly resolves absolute bundled refs when JSON Schema Draft 2020-12', () => {
212-
expect(findSchemaDefinition('#/$defs/bundledAbsoluteRef', bundledSchema)).toStrictEqual(absoluteInternalSchema);
189+
expect(findSchemaDefinition('#/$defs/bundledAbsoluteRef', bundledSchema)).toStrictEqual(internalSchema);
213190
});
214191
it('correctly resolves absolute bundled refs with anchors within a JSON Schema Draft 2020-12', () => {
215192
expect(findSchemaDefinition('#/$defs/bundledAbsoluteRefWithAnchor', bundledSchema)).toBe(
216-
absoluteInternalSchema.properties!.num,
193+
internalSchema.properties!.num,
217194
);
218195
});
219196
it('correctly resolves absolute bundled refs in arrays within a JSON Schema Draft 2020-12', () => {
@@ -223,29 +200,27 @@ describe('findSchemaDefinition()', () => {
223200
});
224201
it('correctly resolves absolute bundled refs within a JSON Schema Draft 2020-12 without an `$id`', () => {
225202
const { $id: d, ...bundledSchemaWithoutId } = bundledSchema;
226-
expect(findSchemaDefinition('#/$defs/bundledAbsoluteRef', bundledSchemaWithoutId)).toStrictEqual(
227-
absoluteInternalSchema,
228-
);
203+
expect(findSchemaDefinition('#/$defs/bundledAbsoluteRef', bundledSchemaWithoutId)).toStrictEqual(internalSchema);
229204
});
230205
it('correctly resolves relative bundled refs within a JSON Schema Draft 2020-12', () => {
231-
expect(findSchemaDefinition('#/$defs/bundledRelativeRef', bundledSchema)).toStrictEqual(absoluteInternalSchema);
206+
expect(findSchemaDefinition('#/$defs/bundledRelativeRef', bundledSchema)).toStrictEqual(internalSchema);
232207
});
233208
it('correctly resolves relative bundled refs with anchors within a JSON Schema Draft 2020-12', () => {
234209
expect(findSchemaDefinition('#/$defs/bundledRelativeRefWithAnchor', bundledSchema)).toBe(
235-
absoluteInternalSchema.$defs!.string,
210+
internalSchema.$defs!.string,
236211
);
237212
});
238213
it('correctly resolves indirect bundled refs within a JSON Schema Draft 2020-12', () => {
239-
expect(findSchemaDefinition('#/$defs/indirectRef', bundledSchema)).toStrictEqual(absoluteInternalSchema);
214+
expect(findSchemaDefinition('#/$defs/indirectRef', bundledSchema)).toStrictEqual(internalSchema);
240215
});
241216
it('correctly resolves refs with explicit base URI in a bundled JSON Schema', () => {
242217
expect(
243218
findSchemaDefinition('bundled.ref.json', bundledSchema, 'https://example.com/undefined.ref.json'),
244-
).toStrictEqual(absoluteInternalSchema);
219+
).toStrictEqual(internalSchema);
245220
});
246221
it('correctly resolves local refs with explicit base URI in a bundled JSON Schema', () => {
247222
expect(findSchemaDefinition('#/properties/num', bundledSchema, 'https://example.com/bundled.ref.json')).toBe(
248-
absoluteInternalSchema.properties!.num,
223+
internalSchema.properties!.num,
249224
);
250225
});
251226
it('throws error when relative ref is undefined in a bundled JSON Schema', () => {
@@ -255,7 +230,7 @@ describe('findSchemaDefinition()', () => {
255230
});
256231
it('throws error when relative ref with anchor is undefined in a bundled JSON Schema', () => {
257232
expect(() => findSchemaDefinition('#/$defs/undefinedRefWithAnchor', bundledSchema)).toThrowError(
258-
'Could not find a definition for https://example.com/bundled.ref.json#/$defs/undefined',
233+
'Could not find a definition for #/$defs/undefined',
259234
);
260235
});
261236
it('throws error when local ref is undefined in a bundled JSON Schema with explicit base URI', () => {
@@ -270,7 +245,7 @@ describe('findSchemaDefinition()', () => {
270245
});
271246
it('throws error when ref is a deep circular reference in a bundled JSON Schema', () => {
272247
expect(() => findSchemaDefinition('#/$defs/circularRef', bundledSchema)).toThrowError(
273-
'Definition for #/$defs/circularRef contains a circular reference through /bundled.ref.json/#/$defs/circularRef -> https://example.com/bundled.schema.json/#/$defs/circularRef',
248+
'Definition for #/$defs/circularRef contains a circular reference through /bundled.ref.json/#/$defs/circularRef -> /bundled.schema.json/#/$defs/circularRef -> #/$defs/circularRef',
274249
);
275250
});
276251
});
@@ -319,13 +294,11 @@ describe('findSchemaDefinitionRecursive()', () => {
319294
).toThrowError('Could not find a definition for #/properties/num');
320295
});
321296
it('correctly resolves absolute bundled refs within a JSON Schema Draft 2020-12', () => {
322-
expect(findSchemaDefinitionRecursive('#/$defs/bundledAbsoluteRef', bundledSchema)).toStrictEqual(
323-
absoluteInternalSchema,
324-
);
297+
expect(findSchemaDefinitionRecursive('#/$defs/bundledAbsoluteRef', bundledSchema)).toStrictEqual(internalSchema);
325298
});
326299
it('correctly resolves absolute bundled refs with anchors within a JSON Schema Draft 2020-12', () => {
327300
expect(findSchemaDefinitionRecursive('#/$defs/bundledAbsoluteRefWithAnchor', bundledSchema)).toBe(
328-
absoluteInternalSchema.properties!.num,
301+
internalSchema.properties!.num,
329302
);
330303
});
331304
it('correctly resolves absolute bundled refs in arrays within a JSON Schema Draft 2020-12', () => {
@@ -336,31 +309,29 @@ describe('findSchemaDefinitionRecursive()', () => {
336309
it('correctly resolves absolute bundled refs within a JSON Schema Draft 2020-12 without an `$id`', () => {
337310
const { $id: d, ...bundledSchemaWithoutId } = bundledSchema;
338311
expect(findSchemaDefinitionRecursive('#/$defs/bundledAbsoluteRef', bundledSchemaWithoutId)).toStrictEqual(
339-
absoluteInternalSchema,
312+
internalSchema,
340313
);
341314
});
342315
it('correctly resolves relative bundled refs within a JSON Schema Draft 2020-12', () => {
343-
expect(findSchemaDefinitionRecursive('#/$defs/bundledRelativeRef', bundledSchema)).toStrictEqual(
344-
absoluteInternalSchema,
345-
);
316+
expect(findSchemaDefinitionRecursive('#/$defs/bundledRelativeRef', bundledSchema)).toStrictEqual(internalSchema);
346317
});
347318
it('correctly resolves relative bundled refs with anchors within a JSON Schema Draft 2020-12', () => {
348319
expect(findSchemaDefinitionRecursive('#/$defs/bundledRelativeRefWithAnchor', bundledSchema)).toBe(
349-
absoluteInternalSchema.$defs!.string,
320+
internalSchema.$defs!.string,
350321
);
351322
});
352323
it('correctly resolves indirect bundled refs within a JSON Schema Draft 2020-12', () => {
353-
expect(findSchemaDefinitionRecursive('#/$defs/indirectRef', bundledSchema)).toStrictEqual(absoluteInternalSchema);
324+
expect(findSchemaDefinitionRecursive('#/$defs/indirectRef', bundledSchema)).toStrictEqual(internalSchema);
354325
});
355326
it('correctly resolves relative refs with explicit base URI in a bundled JSON Schema', () => {
356327
expect(
357328
findSchemaDefinitionRecursive('bundled.ref.json', bundledSchema, [], 'https://example.com/undefined.ref.json'),
358-
).toStrictEqual(absoluteInternalSchema);
329+
).toStrictEqual(internalSchema);
359330
});
360331
it('correctly resolves local refs with explicit base URI in a bundled JSON Schema', () => {
361332
expect(
362333
findSchemaDefinitionRecursive('#/properties/num', bundledSchema, [], 'https://example.com/bundled.ref.json'),
363-
).toBe(absoluteInternalSchema.properties!.num);
334+
).toBe(internalSchema.properties!.num);
364335
});
365336
it('throws error when relative ref is undefined in a bundled JSON Schema', () => {
366337
expect(() => findSchemaDefinitionRecursive('#/$defs/undefinedRef', bundledSchema)).toThrowError(
@@ -369,7 +340,7 @@ describe('findSchemaDefinitionRecursive()', () => {
369340
});
370341
it('throws error when relative ref with anchor is undefined in a bundled JSON Schema', () => {
371342
expect(() => findSchemaDefinitionRecursive('#/$defs/undefinedRefWithAnchor', bundledSchema)).toThrowError(
372-
'Could not find a definition for https://example.com/bundled.ref.json#/$defs/undefined',
343+
'Could not find a definition for #/$defs/undefined',
373344
);
374345
});
375346
it('throws error when local ref is undefined in a bundled JSON Schema with explicit base URI', () => {
@@ -389,7 +360,34 @@ describe('findSchemaDefinitionRecursive()', () => {
389360
});
390361
it('throws error when ref is a deep circular reference in a bundled JSON Schema', () => {
391362
expect(() => findSchemaDefinitionRecursive('#/$defs/circularRef', bundledSchema, [])).toThrowError(
392-
'Definition for #/$defs/circularRef contains a circular reference through /bundled.ref.json/#/$defs/circularRef -> https://example.com/bundled.schema.json/#/$defs/circularRef',
363+
'Definition for #/$defs/circularRef contains a circular reference through /bundled.ref.json/#/$defs/circularRef -> /bundled.schema.json/#/$defs/circularRef -> #/$defs/circularRef',
393364
);
394365
});
395366
});
367+
368+
describe('makeAllReferencesAbsolute()', () => {
369+
it('correctly makes all references absolute in a JSON Schema', () => {
370+
expect(makeAllReferencesAbsolute(internalSchema, internalSchema[ID_KEY]!)).toStrictEqual({
371+
...internalSchema,
372+
$defs: {
373+
...internalSchema.$defs,
374+
circularRef: { $ref: 'https://example.com/bundled.schema.json/#/$defs/circularRef' },
375+
undefinedRef: { $ref: 'https://example.com/bundled.ref.json#/$defs/undefined' },
376+
},
377+
properties: {
378+
...internalSchema.properties,
379+
string: { $ref: 'https://example.com/bundled.ref.json#/$defs/string' },
380+
allOf: {
381+
allOf: [
382+
{
383+
$ref: 'https://example.com/bundled.ref.json#/$defs/string',
384+
},
385+
{
386+
title: 'String',
387+
},
388+
],
389+
},
390+
},
391+
});
392+
});
393+
});

0 commit comments

Comments
 (0)