Skip to content

Commit 29ab430

Browse files
amarnusn1k0
authored andcommitted
Fix corrupted idSchema when the schema has a field named id (rjsf-team#262)
1 parent d83a1b1 commit 29ab430

File tree

8 files changed

+56
-30
lines changed

8 files changed

+56
-30
lines changed

src/components/fields/ArrayField.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ function ArrayFieldTitle({TitleField, idSchema, title, required}) {
2222
if (!title) {
2323
return null;
2424
}
25-
const id = `${idSchema.id}__title`;
25+
const id = `${idSchema.$id}__title`;
2626
return <TitleField id={id} title={title} required={required} />;
2727
}
2828

2929
function ArrayFieldDescription({DescriptionField, idSchema, description}) {
3030
if (!description) {
3131
return null;
3232
}
33-
const id = `${idSchema.id}__description`;
33+
const id = `${idSchema.$id}__description`;
3434
return <DescriptionField id={id} description={description} />;
3535
}
3636

@@ -185,7 +185,7 @@ class ArrayField extends Component {
185185
<div className="row array-item-list">{
186186
items.map((item, index) => {
187187
const itemErrorSchema = errorSchema ? errorSchema[index] : undefined;
188-
const itemIdPrefix = idSchema.id + "_" + index;
188+
const itemIdPrefix = idSchema.$id + "_" + index;
189189
const itemIdSchema = toIdSchema(itemsSchema, itemIdPrefix, definitions);
190190
return this.renderArrayFieldItem({
191191
index,
@@ -216,7 +216,7 @@ class ArrayField extends Component {
216216
const Widget = (multipleCheckboxes) ? CheckboxesWidget : SelectWidget;
217217
return (
218218
<Widget
219-
id={idSchema && idSchema.id}
219+
id={idSchema && idSchema.$id}
220220
multiple
221221
onChange={this.onSelectChange}
222222
options={{enumOptions: optionsList(itemsSchema)}}
@@ -235,7 +235,7 @@ class ArrayField extends Component {
235235
const {items} = this.state;
236236
return (
237237
<FileWidget
238-
id={idSchema && idSchema.id}
238+
id={idSchema && idSchema.$id}
239239
multiple
240240
onChange={this.onSelectChange}
241241
schema={schema}
@@ -287,7 +287,7 @@ class ArrayField extends Component {
287287
const additional = index >= itemSchemas.length;
288288
const itemSchema = additional ?
289289
additionalSchema : itemSchemas[index];
290-
const itemIdPrefix = idSchema.id + "_" + index;
290+
const itemIdPrefix = idSchema.$id + "_" + index;
291291
const itemIdSchema = toIdSchema(itemSchema, itemIdPrefix, definitions);
292292
const itemUiSchema = additional ?
293293
uiSchema.additionalItems || {} :

src/components/fields/BooleanField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function BooleanField(props) {
3636
const widget = uiSchema["ui:widget"];
3737
const commonProps = {
3838
schema,
39-
id: idSchema && idSchema.id,
39+
id: idSchema && idSchema.$id,
4040
onChange,
4141
label: title || name,
4242
placeholder: description,

src/components/fields/ObjectField.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ class ObjectField extends Component {
114114
return (
115115
<fieldset>
116116
{title ? <TitleField
117-
id={`${idSchema.id}__title`}
117+
id={`${idSchema.$id}__title`}
118118
title={title}
119119
required={required} /> : null}
120120
{schema.description ?
121121
<DescriptionField
122-
id={`${idSchema.id}__description`}
122+
id={`${idSchema.$id}__description`}
123123
description={schema.description}
124124
/> : null}
125125
{

src/components/fields/SchemaField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function SchemaField(props) {
162162
required={required}
163163
type={schema.type}
164164
displayLabel={displayLabel}
165-
id={idSchema.id}
165+
id={idSchema.$id}
166166
classNames={uiSchema.classNames}>
167167
<FieldComponent {...props}
168168
schema={schema}

src/components/fields/StringField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function StringField(props) {
2828
const widget = uiSchema["ui:widget"] || schema.format;
2929
const commonProps = {
3030
schema,
31-
id: idSchema && idSchema.id,
31+
id: idSchema && idSchema.$id,
3232
label: title || name,
3333
placeholder: description,
3434
onChange,

src/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ export function shouldRender(comp, nextProps, nextState) {
301301
}
302302

303303
export function toIdSchema(schema, id, definitions) {
304-
const idSchema = {id: id || "root"};
304+
const idSchema = {
305+
$id: id || "root"
306+
};
305307
if ("$ref" in schema) {
306308
const _schema = retrieveSchema(schema, definitions);
307309
return toIdSchema(_schema, id, definitions);
@@ -314,7 +316,7 @@ export function toIdSchema(schema, id, definitions) {
314316
}
315317
for (const name in schema.properties || {}) {
316318
const field = schema.properties[name];
317-
const fieldId = idSchema.id + "_" + name;
319+
const fieldId = idSchema.$id + "_" + name;
318320
idSchema[name] = toIdSchema(field, fieldId, definitions);
319321
}
320322
return idSchema;

test/performance_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe("Rendering performance optimizations", () => {
9595
foo: {type: "string"}
9696
}
9797
};
98-
const idSchema = {id: "root", foo: {id: "root_plop"}};
98+
const idSchema = {$id: "root", foo: {$id: "root_plop"}};
9999

100100
it("should not render if next props are equivalent", () => {
101101
const {comp} = createComponent(ObjectField, {

test/utils_test.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ describe("utils", () => {
464464
it("should return an idSchema for root field", () => {
465465
const schema = {type: "string"};
466466

467-
expect(toIdSchema(schema)).eql({id: "root"});
467+
expect(toIdSchema(schema)).eql({$id: "root"});
468468
});
469469

470470
it("should return an idSchema for nested objects", () => {
@@ -481,10 +481,10 @@ describe("utils", () => {
481481
};
482482

483483
expect(toIdSchema(schema)).eql({
484-
id: "root",
484+
$id: "root",
485485
level1: {
486-
id: "root_level1",
487-
level2: {id: "root_level1_level2"}
486+
$id: "root_level1",
487+
level2: {$id: "root_level1_level2"}
488488
}
489489
});
490490
});
@@ -511,20 +511,44 @@ describe("utils", () => {
511511
};
512512

513513
expect(toIdSchema(schema)).eql({
514-
id: "root",
514+
$id: "root",
515515
level1a: {
516-
id: "root_level1a",
517-
level1a2a: {id: "root_level1a_level1a2a"},
518-
level1a2b: {id: "root_level1a_level1a2b"},
516+
$id: "root_level1a",
517+
level1a2a: {$id: "root_level1a_level1a2a"},
518+
level1a2b: {$id: "root_level1a_level1a2b"},
519519
},
520520
level1b: {
521-
id: "root_level1b",
522-
level1b2a: {id: "root_level1b_level1b2a"},
523-
level1b2b: {id: "root_level1b_level1b2b"},
521+
$id: "root_level1b",
522+
level1b2a: {$id: "root_level1b_level1b2a"},
523+
level1b2b: {$id: "root_level1b_level1b2b"},
524524
},
525525
});
526526
});
527527

528+
it("schema with an id property must not corrupt the idSchema", () => {
529+
const schema = {
530+
type: "object",
531+
properties: {
532+
metadata: {
533+
type: "object",
534+
properties: {
535+
id: {
536+
type: "string"
537+
}
538+
},
539+
required: [ "id" ]
540+
}
541+
}
542+
};
543+
expect(toIdSchema(schema)).eql({
544+
$id: "root",
545+
metadata: {
546+
$id: "root_metadata",
547+
id: { $id: "root_metadata_id" }
548+
}
549+
});
550+
});
551+
528552
it("should return an idSchema for array item objects", () => {
529553
const schema = {
530554
type: "array",
@@ -537,8 +561,8 @@ describe("utils", () => {
537561
};
538562

539563
expect(toIdSchema(schema)).eql({
540-
id: "root",
541-
foo: {id: "root_foo"}
564+
$id: "root",
565+
foo: {$id: "root_foo"}
542566
});
543567
});
544568

@@ -557,9 +581,9 @@ describe("utils", () => {
557581
};
558582

559583
expect(toIdSchema(schema, undefined, schema.definitions)).eql({
560-
id: "root",
561-
foo: {id: "root_foo"},
562-
bar: {id: "root_bar"}
584+
$id: "root",
585+
foo: {$id: "root_foo"},
586+
bar: {$id: "root_bar"}
563587
});
564588
});
565589
});

0 commit comments

Comments
 (0)