Skip to content

Commit 1a18f25

Browse files
committed
fix: handle object type placed among other types
1 parent 1a27dfc commit 1a18f25

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

src/components/__tests__/Property.spec.tsx

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,35 @@ describe('Property component', () => {
8080
});
8181

8282
describe('properties counter', () => {
83+
test('given an object among other types, should still display the counter', () => {
84+
const treeNode: SchemaTreeListNode = {
85+
id: 'foo',
86+
name: '',
87+
parent: null,
88+
};
89+
90+
const schema: JSONSchema4 = {
91+
type: ['string', 'object'],
92+
properties: {
93+
foo: {
94+
type: 'array',
95+
items: {
96+
type: 'integer',
97+
},
98+
},
99+
},
100+
};
101+
102+
metadataStore.set(treeNode, {
103+
schemaNode: walk(schema).next().value.node,
104+
path: [],
105+
schema,
106+
});
107+
108+
const wrapper = shallow(<Property node={treeNode} />);
109+
expect(wrapper.findWhere(el => /^{\d\}$/.test(el.text())).first()).toHaveText('{1}');
110+
});
111+
83112
it('given missing properties property, should not display the counter', () => {
84113
const treeNode: SchemaTreeListNode = {
85114
id: 'foo',
@@ -98,7 +127,7 @@ describe('Property component', () => {
98127
});
99128

100129
const wrapper = shallow(<Property node={treeNode} />);
101-
expect(wrapper.findWhere(el => /^\{\d\}$/.test(el.text()))).not.toExist();
130+
expect(wrapper.findWhere(el => /^{\d\}$/.test(el.text()))).not.toExist();
102131
});
103132

104133
it('given nullish properties property, should not display the counter', () => {
@@ -120,7 +149,7 @@ describe('Property component', () => {
120149
});
121150

122151
const wrapper = shallow(<Property node={treeNode} />);
123-
expect(wrapper.findWhere(el => /^\{\d\}$/.test(el.text()))).not.toExist();
152+
expect(wrapper.findWhere(el => /^{\d\}$/.test(el.text()))).not.toExist();
124153
});
125154

126155
it('given object properties property, should display the counter', () => {
@@ -142,7 +171,7 @@ describe('Property component', () => {
142171
});
143172

144173
const wrapper = shallow(<Property node={treeNode} />);
145-
expect(wrapper.findWhere(el => /^\{\d\}$/.test(el.text())).first()).toHaveText('{0}');
174+
expect(wrapper.findWhere(el => /^{\d\}$/.test(el.text())).first()).toHaveText('{0}');
146175
});
147176
});
148177

@@ -170,6 +199,33 @@ describe('Property component', () => {
170199
expect(wrapper.find('div').first()).toHaveText('foo');
171200
});
172201

202+
test('given an object among other types, should still display its properties', () => {
203+
const schema: JSONSchema4 = {
204+
type: ['string', 'object'],
205+
properties: {
206+
foo: {
207+
type: 'array',
208+
items: {
209+
type: 'integer',
210+
},
211+
},
212+
},
213+
};
214+
215+
const tree = new SchemaTree(schema, new TreeState(), {
216+
expandedDepth: Infinity,
217+
mergeAllOf: false,
218+
resolveRef: void 0,
219+
shouldResolveEagerly: false,
220+
onPopulate: void 0,
221+
});
222+
223+
tree.populate();
224+
225+
const wrapper = shallow(<Property node={tree.itemAt(1)!} />);
226+
expect(wrapper.find('div').first()).toHaveText('foo');
227+
});
228+
173229
test('given an array of objects, should display names of those properties', () => {
174230
const schema: JSONSchema4 = {
175231
type: 'array',

src/components/shared/Property.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function shouldShowPropertyName(treeNode: SchemaTreeListNode) {
3131
return false;
3232
}
3333

34-
let type = schemaNode.type;
34+
let type = getPrimaryType(schemaNode);
3535

3636
if (type === SchemaKind.Array && (schemaNode as IArrayNode).items) {
3737
type = getPrimaryType((schemaNode as IArrayNode).items!);
@@ -53,7 +53,7 @@ export const Property: React.FunctionComponent<IProperty> = ({ node: treeNode, o
5353
const subtype = isArrayNodeWithItems(node) ? (hasRefItems(node) ? '$ref' : inferType(node.items)) : void 0;
5454

5555
const childrenCount = React.useMemo<number | null>(() => {
56-
if (type === SchemaKind.Object) {
56+
if (type === SchemaKind.Object || (Array.isArray(type) && type.includes(SchemaKind.Object))) {
5757
return count((node as IObjectNode).properties);
5858
}
5959

src/tree/__tests__/tree.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,4 +1170,43 @@ describe('SchemaTree', () => {
11701170
expect(onPopulate).nthCalledWith(2, tree, tree.itemAt(0));
11711171
});
11721172
});
1173+
1174+
describe('tree correctness', () => {
1175+
// you can put tests verifying whether we generate expected tree
1176+
test('given multiple object and string type, should process properties', () => {
1177+
const schema: JSONSchema4 = {
1178+
type: ['string', 'object'],
1179+
properties: {
1180+
ids: {
1181+
type: 'array',
1182+
items: {
1183+
type: 'integer',
1184+
},
1185+
},
1186+
},
1187+
};
1188+
1189+
const tree = new SchemaTree(schema, new SchemaTreeState(), {
1190+
expandedDepth: Infinity,
1191+
mergeAllOf: true,
1192+
resolveRef: void 0,
1193+
shouldResolveEagerly: true,
1194+
onPopulate: void 0,
1195+
});
1196+
1197+
tree.populate();
1198+
expect(printTree(tree)).toMatchInlineSnapshot(`
1199+
"└─ #
1200+
├─ type
1201+
│ ├─ 0: string
1202+
│ └─ 1: object
1203+
└─ children
1204+
└─ 0
1205+
└─ #/properties/ids
1206+
├─ type: array
1207+
└─ subtype: integer
1208+
"
1209+
`);
1210+
});
1211+
});
11731212
});

0 commit comments

Comments
 (0)