Skip to content

Commit c0b7754

Browse files
author
Riccardo Di Benedetto
committed
max_depth starts with 1. 0 is all
1 parent fceb104 commit c0b7754

File tree

6 files changed

+59
-9
lines changed

6 files changed

+59
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ Here are all the available options:
278278
</tr>
279279
<tr>
280280
<td>max_depth</td>
281-
<td>Max depth of the nested properties to be rendered of provided json schema. The missing of this option could cause "maximum call stack size exceeded" in case of object properties with circular references</td>
282-
<td><code>undefined</code></td>
281+
<td>Max depth of the nested properties to be rendered of provided json schema. The missing of this option could cause "maximum call stack size exceeded" in case of object properties with circular references. <code>0</code> value means "render all".</td>
282+
<td><code>0</code></td>
283283
</tr>
284284
<tr>
285285
<td>use_default_values</td>

src/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export class JSONEditor {
211211
return JSONEditor.defaults.editors[classname]
212212
}
213213

214-
createEditor (editorClass, options, depthCounter = 0) {
214+
createEditor (editorClass, options, depthCounter = 1) {
215215
options = extend({}, editorClass.options || {}, options)
216216
// eslint-disable-next-line new-cap
217217
return new editorClass(options, JSONEditor.defaults, depthCounter)

src/defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ function translate (key, variables) {
284284
const options = {
285285
upload,
286286
prompt_before_delete: true,
287-
use_default_values: true
287+
use_default_values: true,
288+
max_depth: 0
288289
}
289290

290291
/* This assignment was previously in index.js but makes more sense here */

src/editors/object.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,11 @@ export class ObjectEditor extends AbstractEditor {
972972
...acc,
973973
[key]: {}
974974
}
975+
case 'additionalProperties':
976+
return {
977+
...acc,
978+
[key]: true
979+
}
975980
default:
976981
return {
977982
...acc,
@@ -1009,7 +1014,7 @@ export class ObjectEditor extends AbstractEditor {
10091014

10101015
this.editors[name] = this.jsoneditor.createEditor(editor, {
10111016
jsoneditor: this.jsoneditor,
1012-
schema: maxDepth !== undefined && this.currentDepth >= maxDepth ? this.getSchemaOnMaxDepth(schema) : schema,
1017+
schema: !!maxDepth && this.currentDepth >= maxDepth ? this.getSchemaOnMaxDepth(schema) : schema,
10131018
path: `${this.path}.${name}`,
10141019
parent: this
10151020
}, this.currentDepth + 1)

tests/fixtures/nested_object.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"foo1": {
5+
"type": "object",
6+
"properties": {
7+
"foo2": {
8+
"type": "object",
9+
"properties": {
10+
"foo3": {
11+
"type": "object",
12+
"properties": {
13+
"foo4": {
14+
"type": "object",
15+
"properties": {
16+
"bar": {"type": "string", "default": "end schema"}
17+
}
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}

tests/unit/core.spec.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ TODO: Write unit tests for all interfaces
44
*/
55
import { JSONEditor } from '../../src/core'
66
import someTypes from '../fixtures/some_types.json'
7+
import nestedObject from '../fixtures/nested_object.json'
78

89
const schema = {
910
type: 'object',
@@ -117,7 +118,7 @@ describe('JSONEditor', function () {
117118
}
118119
}
119120
},
120-
max_depth: 3
121+
max_depth: 4
121122
})
122123
expect(editor).toBeTruthy()
123124
expect(editor.getValue()).toEqual({
@@ -132,13 +133,13 @@ describe('JSONEditor', function () {
132133
})
133134

134135
it('with max_depth that stops on level with enum as object property', () => {
135-
const depthWithEnum = 1
136+
const depthWithEnum = 2
136137

137138
editor = new JSONEditor(element, {
138139
schema: {
139140
type: 'object',
140141
properties: {
141-
field_on_level_zero: {
142+
field_on_level_one: {
142143
type: 'object',
143144
properties: {
144145
propertyWithEnum: {
@@ -163,12 +164,29 @@ describe('JSONEditor', function () {
163164
})
164165
expect(editor).toBeTruthy()
165166
expect(editor.getValue()).toEqual({
166-
field_on_level_zero: {
167+
field_on_level_one: {
167168
propertyWithEnum: 'bar',
168169
something_else: {}
169170
}
170171
})
171172
})
173+
174+
it('with max_depth equals to 0 renders all schema', () => {
175+
editor = new JSONEditor(element, {
176+
schema: nestedObject,
177+
max_depth: 0
178+
})
179+
expect(editor).toBeTruthy()
180+
expect(editor.getValue()).toEqual({ foo1: { foo2: { foo3: { foo4: { bar: 'end schema' } } } } })
181+
})
182+
183+
it('renders all schema as default', () => {
184+
editor = new JSONEditor(element, {
185+
schema: nestedObject
186+
})
187+
expect(editor).toBeTruthy()
188+
expect(editor.getValue()).toEqual({ foo1: { foo2: { foo3: { foo4: { bar: 'end schema' } } } } })
189+
})
172190
})
173191

174192
describe('use_default_values', () => {

0 commit comments

Comments
 (0)