Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/__fixtures__/array-of-refs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "array",
"items": {
"$ref": "./models/todo-full.json"
}
}
2 changes: 1 addition & 1 deletion src/components/SchemaTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TreeList, TreeListMouseEventHandler, TreeStore } from '@stoplight/tree-
import { Omit } from '@stoplight/types';
import { Box, IBox, ThemeZone } from '@stoplight/ui-kit';
import { JSONSchema4 } from 'json-schema';
import _isEmpty = require('lodash/isEmpty');
import { isEmpty as _isEmpty } from 'lodash';
import * as React from 'react';
import { useMetadata } from '../hooks';
import { useTheme } from '../theme';
Expand Down
4 changes: 2 additions & 2 deletions src/components/Type.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Box, IBoxCSS } from '@stoplight/ui-kit';
import { JSONSchema4TypeName } from 'json-schema';
import * as React from 'react';
import { IJsonSchemaViewerTheme, useTheme } from '../theme';
import { JSONSchema4CombinerName } from '../types';
import { ITreeNodeMeta, JSONSchema4CombinerName } from '../types';

export const Type: React.FunctionComponent<IType> = ({ type, subtype, children }) => {
const theme = useTheme() as IJsonSchemaViewerTheme;
Expand All @@ -18,7 +18,7 @@ export const Type: React.FunctionComponent<IType> = ({ type, subtype, children }

export interface IType {
type: JSONSchema4TypeName | JSONSchema4CombinerName | '$ref';
subtype?: JSONSchema4TypeName | JSONSchema4TypeName[];
subtype?: ITreeNodeMeta['subtype'];
}

export const rowStyles = (theme: IJsonSchemaViewerTheme, { type }: IType): IBoxCSS => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Types.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { JSONSchema4TypeName } from 'json-schema';
import * as React from 'react';
import { JSONSchema4CombinerName } from '../types';
import { ITreeNodeMeta, JSONSchema4CombinerName } from '../types';
import { MutedText } from './common/MutedText';
import { Type } from './Type';

interface ITypes {
type?: JSONSchema4TypeName | JSONSchema4TypeName[] | JSONSchema4CombinerName;
subtype?: JSONSchema4TypeName | JSONSchema4TypeName[];
subtype?: ITreeNodeMeta['subtype'];
}

export const Types: React.FunctionComponent<ITypes> = ({ type, subtype }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface ITreeNodeMeta {
additional?: IArrayNode['additionalItems'] | IObjectNode['additionalProperties'];
path: JsonPath;
divider?: string;
subtype?: IBaseNode['type'];
subtype?: IBaseNode['type'] | string;
expanded?: boolean;
required?: boolean;
inheritedFrom?: string;
Expand Down
27 changes: 26 additions & 1 deletion src/utils/__tests__/__snapshots__/renderSchema.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ Array [
],
"required": false,
"subtype": "object",
"type": "array",
"type": Array [
"array",
],
"validations": Object {},
},
"name": "",
Expand Down Expand Up @@ -77,6 +79,27 @@ Array [
]
`;

exports[`renderSchema util should match array-of-refs.json 1`] = `
Array [
Object {
"id": "random-id",
"level": 0,
"metadata": Object {
"additionalItems": undefined,
"annotations": Object {},
"enum": undefined,
"id": "random-id",
"items": undefined,
"path": Array [],
"subtype": "$ref( ./models/todo-full.json )",
"type": "array",
"validations": Object {},
},
"name": "",
},
]
`;

exports[`renderSchema util should match combiner-schema.json 1`] = `
Array [
Object {
Expand Down Expand Up @@ -479,9 +502,11 @@ Array [
"id": "random-id",
"level": 1,
"metadata": Object {
"additionalItems": undefined,
"annotations": Object {},
"enum": undefined,
"id": "random-id",
"items": undefined,
"name": "items",
"path": Array [
"properties",
Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/renderSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('renderSchema util', () => {
['ref/original.json', 'ref/resolved.json'],
['combiner-schema.json', ''],
['array-of-objects.json', ''],
['array-of-refs.json', ''],
])('should match %s', (schema, dereferenced) => {
expect(
Array.from(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getAnnotations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSONSchema4 } from 'json-schema';
import _pick = require('lodash/pick');
import { pick as _pick } from 'lodash';
import { JSONSchema4Annotations } from '../types';

const ANNOTATIONS: JSONSchema4Annotations[] = ['title', 'description', 'default', 'examples'];
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSONSchema4 } from 'json-schema';
import _pick = require('lodash/pick');
import { pick as _pick } from 'lodash';
import { JSONSchema4Metadata } from '../types';

const METADATA: JSONSchema4Metadata[] = ['id', '$schema'];
Expand Down
21 changes: 21 additions & 0 deletions src/utils/getPrimaryType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { JSONSchema4 } from 'json-schema';
import { SchemaKind } from '../types';
import { inferType } from './inferType';

export function getPrimaryType(node: JSONSchema4) {
if (node.type !== undefined) {
if (Array.isArray(node.type)) {
if (node.type.includes(SchemaKind.Object)) {
return SchemaKind.Object;
}

if (node.type.includes(SchemaKind.Array)) {
return SchemaKind.Array;
}
}

return node.type;
}

return inferType(node);
}
3 changes: 1 addition & 2 deletions src/utils/getValidations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Dictionary } from '@stoplight/types';
import { JSONSchema4, JSONSchema4TypeName } from 'json-schema';
import _flatMap = require('lodash/flatMap');
import _pick = require('lodash/pick');
import { flatMap as _flatMap, pick as _pick } from 'lodash';

export const COMMON_VALIDATION_TYPES = [
'enum', // https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.1
Expand Down
14 changes: 14 additions & 0 deletions src/utils/inferType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { JSONSchema4, JSONSchema4TypeName } from 'json-schema';
import { SchemaKind } from '../types';

export function inferType(node: JSONSchema4): JSONSchema4TypeName | undefined {
if ('properties' in node) {
return SchemaKind.Object;
}

if ('items' in node) {
return SchemaKind.Array;
}

return;
}
5 changes: 2 additions & 3 deletions src/utils/isSchemaViewerEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { ISchema } from '@stoplight/types';
import get = require('lodash/get');
import isEmpty = require('lodash/isEmpty');
import { get as _get, isEmpty as _isEmpty } from 'lodash';

const combinerTypes = ['allOf', 'oneOf', 'anyOf'];

export const isSchemaViewerEmpty = (schema: ISchema) => {
const objectKeys = Object.keys(schema);

if (objectKeys.length === 1 && combinerTypes.includes(objectKeys[0])) {
return isEmpty(get(schema, objectKeys[0], []));
return _isEmpty(_get(schema, objectKeys[0], []));
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lookupRef.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JsonPath } from '@stoplight/types';
import { JSONSchema4 } from 'json-schema';
import _get = require('lodash/get');
import { get as _get } from 'lodash';

export const lookupRef = (path: JsonPath, dereferencedSchema?: JSONSchema4): JSONSchema4 | null => {
if (dereferencedSchema === undefined) {
Expand Down
121 changes: 66 additions & 55 deletions src/utils/renderSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { JSONSchema4 } from 'json-schema';
import _isEmpty = require('lodash/isEmpty');
import { isEmpty as _isEmpty } from 'lodash';
import { IArrayNode, IObjectNode, ITreeNodeMeta, SchemaKind, SchemaTreeListNode } from '../types';
import { DIVIDERS } from './dividers';
import { getPrimaryType } from './getPrimaryType';
import { isCombiner } from './isCombiner';
import { isRef } from './isRef';
import { lookupRef } from './lookupRef';
Expand Down Expand Up @@ -51,7 +52,10 @@ export const renderSchema: Walker = function*(schema, dereferencedSchema, level
metadata: {
...node,
...meta,
...(schema.items !== undefined && !Array.isArray(schema.items) && { subtype: schema.items.type }),
...(schema.items !== undefined &&
!Array.isArray(schema.items) && {
subtype: '$ref' in schema.items ? `$ref( ${schema.items.$ref} )` : schema.items.type,
}),
path,
},
};
Expand Down Expand Up @@ -90,62 +94,69 @@ export const renderSchema: Walker = function*(schema, dereferencedSchema, level
});
}
}
} else if (node.type === SchemaKind.Array) {
yield {
...baseNode,
...('items' in node &&
!_isEmpty(node.items) &&
!('subtype' in baseNode.metadata!) && { canHaveChildren: true }),
metadata: {
...baseNode.metadata,
// https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.1.2
...(!('subtype' in baseNode) &&
(node as IArrayNode).additionalItems && { additional: (node as IArrayNode).additionalItems }),
},
} as SchemaTreeListNode;
if (Array.isArray(schema.items)) {
for (const [i, property] of schema.items.entries()) {
yield* renderSchema(property, dereferencedSchema, level + 1, {
path: [...path, 'items', i],
} else {
switch (getPrimaryType(node)) {
case SchemaKind.Array:
yield {
...baseNode,
...('items' in node &&
!_isEmpty(node.items) &&
!('subtype' in baseNode.metadata!) && { canHaveChildren: true }),
metadata: {
...baseNode.metadata,
// https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.1.2
...(!('subtype' in baseNode) &&
(node as IArrayNode).additionalItems && { additional: (node as IArrayNode).additionalItems }),
},
} as SchemaTreeListNode;

if (Array.isArray(schema.items)) {
for (const [i, property] of schema.items.entries()) {
yield* renderSchema(property, dereferencedSchema, level + 1, {
path: [...path, 'items', i],
});
}
} else if (schema.items) {
switch (baseNode.metadata && baseNode.metadata.subtype) {
case SchemaKind.Object:
yield* getProperties(schema.items, dereferencedSchema, level + 1, {
...meta,
path: [...path, 'items'],
});
break;
case SchemaKind.Array:
yield* renderSchema(schema.items, dereferencedSchema, level + 1, {
path,
});
break;
}
}

break;
case SchemaKind.Object:
yield {
...baseNode,
...('properties' in node && !_isEmpty(node.properties) && { canHaveChildren: true }),
metadata: {
...baseNode.metadata,
...((node as IObjectNode).additionalProperties && {
additional: (node as IObjectNode).additionalProperties,
}),
},
} as SchemaTreeListNode;

yield* getProperties(schema, dereferencedSchema, level, {
path: [...path, 'properties'],
});
}
} else if (schema.items) {
switch (baseNode.metadata && baseNode.metadata.subtype) {
case SchemaKind.Object:
yield* getProperties(schema.items, dereferencedSchema, level + 1, {
...meta,
path: [...path, 'items'],
});
break;
case SchemaKind.Array:
yield* renderSchema(schema.items, dereferencedSchema, level + 1, {
path,
});
break;
}
}
} else if ('properties' in node) {
// special case :P, it's
yield {
...baseNode,
...('properties' in node && !_isEmpty(node.properties) && { canHaveChildren: true }),
metadata: {
...baseNode.metadata,
...((node as IObjectNode).additionalProperties && {
additional: (node as IObjectNode).additionalProperties,
}),
},
} as SchemaTreeListNode;

yield* getProperties(schema, dereferencedSchema, level, {
path: [...path, 'properties'],
});
yield* getPatternProperties(schema, dereferencedSchema, level, {
path: [...path, 'patternProperties'],
});

yield* getPatternProperties(schema, dereferencedSchema, level, {
path: [...path, 'patternProperties'],
});
} else {
yield baseNode;
break;
default:
yield baseNode;
}
}
}
};
Loading