Skip to content

Commit 8c19c52

Browse files
committed
Fix #68: Allow overriding refs
1 parent b6ea13d commit 8c19c52

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

src/data.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ export function getBlankObject(schema, getRef) {
1414
let isRef = value.hasOwnProperty('$ref');
1515
let isConst = value.hasOwnProperty('const');
1616

17-
if (isRef)
18-
value = getRef(value['$ref']);
17+
if (isRef) {
18+
value = {...getRef(value['$ref']), ...value};
19+
delete value['$ref'];
20+
}
1921

2022
let type = normalizeKeyword(value.type);
2123

@@ -81,7 +83,8 @@ export function getBlankArray(schema, getRef) {
8183
if (schema.items.hasOwnProperty('$ref')) {
8284
// :TODO: this mutates the original schema
8385
// but i'll fix it later
84-
schema.items = getRef(schema.items['$ref']);
86+
schema.items = {...getRef(schema.items['$ref']), ...schema.items};
87+
delete schema.items['$ref'];
8588
}
8689

8790
let type = normalizeKeyword(schema.items.type);
@@ -166,8 +169,10 @@ export function getBlankAnyOf(schema, getRef) {
166169

167170

168171
export function getBlankData(schema, getRef) {
169-
if (schema.hasOwnProperty('$ref'))
170-
schema = getRef(schema['$ref']);
172+
if (schema.hasOwnProperty('$ref')) {
173+
schema = {...getRef(schema['$ref']), ...schema};
174+
delete schema['$ref'];
175+
}
171176

172177
let type = getSchemaType(schema);
173178

@@ -209,7 +214,8 @@ function getSyncedArray(data, schema, getRef) {
209214
if (schema.items.hasOwnProperty('$ref')) {
210215
// :TODO: this will most probably mutate the original schema
211216
// but i'll fix it later
212-
schema.items = getRef(schema.items['$ref'])
217+
schema.items = {...getRef(schema.items['$ref']), ...schema.items};
218+
delete schema.items['$ref'];
213219
}
214220

215221
let type;
@@ -291,8 +297,10 @@ function getSyncedObject(data, schema, getRef) {
291297

292298
let isRef = schemaValue.hasOwnProperty('$ref');
293299

294-
if (isRef)
295-
schemaValue = getRef(schemaValue['$ref']);
300+
if (isRef) {
301+
schemaValue = {...getRef(schemaValue['$ref']), ...schemaValue};
302+
delete schemaValue['$ref'];
303+
}
296304

297305
let type;
298306
let default_;
@@ -384,8 +392,10 @@ export function getSyncedAnyOf(data, schema, getRef) {
384392

385393
export function getSyncedData(data, schema, getRef) {
386394
// adds those keys to data which are in schema but not in data
387-
if (schema.hasOwnProperty('$ref'))
388-
schema = getRef(schema['$ref']);
395+
if (schema.hasOwnProperty('$ref')) {
396+
schema = {...getRef(schema['$ref']), ...schema};
397+
delete schema['$ref'];
398+
}
389399

390400

391401
let type = getSchemaType(schema);
@@ -424,8 +434,10 @@ export function findMatchingSubschemaIndex(data, schema, getRef, schemaName) {
424434
for (let i = 0; i < subschemas.length; i++) {
425435
let subschema = subschemas[i];
426436

427-
if (subschema.hasOwnProperty('$ref'))
428-
subschema = getRef(subschema['$ref']);
437+
if (subschema.hasOwnProperty('$ref')) {
438+
subschema = {...getRef(subschema['$ref']), ...subschema};
439+
delete subschema['$ref'];
440+
}
429441

430442
let subType = getSchemaType(subschema);
431443

@@ -453,8 +465,10 @@ export function findMatchingSubschemaIndex(data, schema, getRef, schemaName) {
453465
for (let i = 0; i < subschemas.length; i++) {
454466
let subschema = subschemas[i];
455467

456-
if (subschema.hasOwnProperty('$ref'))
457-
subschema = getRef(subschema['$ref']);
468+
if (subschema.hasOwnProperty('$ref')) {
469+
subschema = {...getRef(subschema['$ref']), ...subschema};
470+
delete subschema['$ref'];
471+
}
458472

459473
let subType = getSchemaType(subschema);
460474

src/ui.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,10 @@ export function getArrayFormRow(args) {
231231

232232
let isRef = schema.items.hasOwnProperty('$ref');
233233

234-
if (isRef)
235-
schema.items = args.getRef(schema.items['$ref']);
234+
if (isRef) {
235+
schema.items = {...args.getRef(schema.items['$ref']), ...schema.items};
236+
delete schema.items['$ref'];
237+
}
236238

237239
let type = normalizeKeyword(schema.items.type);
238240

@@ -415,8 +417,10 @@ export function getObjectFormRow(args) {
415417

416418
let isRef = schemaValue.hasOwnProperty('$ref');
417419

418-
if (isRef)
419-
schemaValue = args.getRef(schemaValue['$ref']);
420+
if (isRef) {
421+
schemaValue = {...args.getRef(schemaValue['$ref']), ...schemaValue};
422+
delete schemaValue['$ref'];
423+
}
420424

421425
if (isReadonly)
422426
schemaValue.readOnly = true;
@@ -591,8 +595,10 @@ class OneOfTopLevel extends React.Component {
591595

592596
let isRef = schema.hasOwnProperty('$ref');
593597

594-
if (isRef)
595-
schema = this.props.args.getRef(schema['$ref']);
598+
if (isRef) {
599+
schema = {...this.props.args.getRef(schema['$ref']), schema};
600+
delete schema['$ref'];
601+
}
596602

597603
return schema;
598604
}
@@ -711,8 +717,10 @@ class OneOf extends React.Component {
711717
let subschema = subschemas[i];
712718
let isRef = subschema.hasOwnProperty('$ref');
713719

714-
if (isRef)
715-
subschema = this.props.parentArgs.getRef(subschema['$ref']);
720+
if (isRef) {
721+
subschema = {...this.props.parentArgs.getRef(subschema['$ref']), ...subschema};
722+
delete subschema['$ref'];
723+
}
716724

717725
let subType = getSchemaType(subschema);
718726

@@ -839,8 +847,10 @@ class OneOf extends React.Component {
839847

840848
let isRef = schema.hasOwnProperty('$ref');
841849

842-
if (isRef)
843-
schema = this.props.parentArgs.getRef(schema['$ref']);
850+
if (isRef) {
851+
schema = {...this.props.parentArgs.getRef(schema['$ref']), ...schema};
852+
delete schema['$ref'];
853+
}
844854

845855
return schema;
846856
}

0 commit comments

Comments
 (0)