Skip to content

Commit 1aff735

Browse files
committed
fix: show property names for properties of array of objects
1 parent 64ecbfe commit 1aff735

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

src/components/__tests__/Property.spec.tsx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { TreeListParentNode, TreeState } from '@stoplight/tree-list';
12
import { shallow } from 'enzyme';
23
import 'jest-enzyme';
34
import { JSONSchema4 } from 'json-schema';
45
import * as React from 'react';
6+
import { SchemaTree } from '../../tree';
57
import { metadataStore } from '../../tree/metadata';
68
import { walk } from '../../tree/walk';
79
import { SchemaTreeListNode } from '../../types';
@@ -122,4 +124,101 @@ describe('Property component', () => {
122124
expect(wrapper.findWhere(el => /^\{\d\}$/.test(el.text())).first()).toHaveText('{0}');
123125
});
124126
});
127+
128+
describe('properties names', () => {
129+
test('given an object, should display names its properties', () => {
130+
const schema: JSONSchema4 = {
131+
properties: {
132+
foo: {
133+
type: 'string',
134+
},
135+
},
136+
};
137+
138+
const tree = new SchemaTree(schema, new TreeState(), {
139+
expandedDepth: Infinity,
140+
mergeAllOf: false,
141+
resolveRef: void 0,
142+
});
143+
144+
tree.populate();
145+
146+
const wrapper = shallow(<Property node={Array.from(tree)[1]} />);
147+
expect(wrapper.find('div').first()).toHaveText('foo');
148+
});
149+
150+
test('given an array of objects, should display names of those properties', () => {
151+
const schema: JSONSchema4 = {
152+
type: 'array',
153+
items: {
154+
properties: {
155+
foo: {
156+
type: 'string',
157+
},
158+
},
159+
},
160+
};
161+
162+
const tree = new SchemaTree(schema, new TreeState(), {
163+
expandedDepth: Infinity,
164+
mergeAllOf: false,
165+
resolveRef: void 0,
166+
});
167+
168+
tree.populate();
169+
170+
const wrapper = shallow(<Property node={Array.from(tree)[1]} />);
171+
expect(wrapper.find('div').first()).toHaveText('foo');
172+
});
173+
174+
test('given a ref pointing at primitive type, should not display property name', () => {
175+
const schema: JSONSchema4 = {
176+
properties: {
177+
foo: {
178+
$ref: '#/properties/bar',
179+
},
180+
bar: {
181+
type: 'string',
182+
},
183+
},
184+
};
185+
186+
const tree = new SchemaTree(schema, new TreeState(), {
187+
expandedDepth: Infinity,
188+
mergeAllOf: false,
189+
resolveRef: void 0,
190+
});
191+
192+
tree.populate();
193+
tree.unwrap(Array.from(tree)[1] as TreeListParentNode);
194+
195+
const wrapper = shallow(<Property node={Array.from(tree)[2]} />);
196+
expect(wrapper.find('div').first()).not.toExist();
197+
});
198+
199+
xtest('given a ref pointing at complex type, should not display property name', () => {
200+
const schema: JSONSchema4 = {
201+
properties: {
202+
foo: {
203+
$ref: '#/properties/bar',
204+
},
205+
bar: {
206+
type: 'object',
207+
},
208+
},
209+
};
210+
211+
const tree = new SchemaTree(schema, new TreeState(), {
212+
expandedDepth: Infinity,
213+
mergeAllOf: false,
214+
resolveRef: void 0,
215+
});
216+
217+
tree.populate();
218+
tree.unwrap(Array.from(tree)[1] as TreeListParentNode);
219+
220+
const wrapper = shallow(<Property node={Array.from(tree)[2]} />);
221+
expect(wrapper.find('div').first()).not.toExist();
222+
});
223+
});
125224
});

src/components/shared/Property.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ function count(obj: Optional<JSONSchema4 | null>): number | null {
2626
function shouldShowPropertyName(treeNode: SchemaTreeListNode) {
2727
if (treeNode.parent === null) return false;
2828
try {
29-
return getPrimaryType(getNodeMetadata(treeNode.parent).schema) === SchemaKind.Object;
29+
const schema = getNodeMetadata(treeNode.parent).schema;
30+
let type = getPrimaryType(schema);
31+
32+
if (type === SchemaKind.Array && schema.items) {
33+
type = getPrimaryType(schema.items);
34+
}
35+
36+
return type === SchemaKind.Object;
3037
} catch {
3138
return false;
3239
}

0 commit comments

Comments
 (0)