Skip to content

Commit

Permalink
Implemented recursive $ref resolution (fixes #1006).
Browse files Browse the repository at this point in the history
  • Loading branch information
wadamek65 authored Sep 13, 2021
1 parent e8d1879 commit ceeb8b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ describe('JSONSchemaBridge', () => {
type: 'object',
properties: {
firstName: { $ref: '#/definitions/firstName' },
middleName: { $ref: '#/definitions/middleName' },
lastName: { $ref: '#/definitions/firstName' },
},
required: ['lastName'],
},
firstName: { type: 'string', default: 'John' },
middleName: { $ref: '#/definitions/lastName' },
lastName: { type: 'string' },
recursive: {
type: 'object',
Expand Down Expand Up @@ -474,6 +476,12 @@ describe('JSONSchemaBridge', () => {
});
});

it('returns correct definition ($ref pointing to $ref)', () => {
expect(bridge.getField('personalData.middleName')).toEqual({
type: 'string',
});
});

it('returns correct definition (array tuple)', () => {
expect(bridge.getField('dateOfBirthTuple.1')).toEqual({ type: 'string' });
});
Expand Down
13 changes: 8 additions & 5 deletions packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ function resolveRef(reference: string, schema: Record<string, any>) {
function resolveRefIfNeeded(
partial: Record<string, any>,
schema: Record<string, any>,
) {
if (partial.$ref) {
partial = Object.assign({}, partial, resolveRef(partial.$ref, schema));
delete partial.$ref;
): Record<string, any> {
if (!('$ref' in partial)) {
return partial;
}

return partial;
const { $ref, ...partialWithoutRef } = partial;
return resolveRefIfNeeded(
Object.assign({}, partialWithoutRef, resolveRef($ref, schema)),
schema,
);
}

const partialNames = ['allOf', 'anyOf', 'oneOf'];
Expand Down

0 comments on commit ceeb8b6

Please sign in to comment.