Skip to content

Commit b41cd0c

Browse files
authored
fix: render top-level description (#205)
1 parent e85511c commit b41cd0c

File tree

6 files changed

+97
-17
lines changed

6 files changed

+97
-17
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"react-dom": ">=16.8"
4646
},
4747
"dependencies": {
48-
"@stoplight/json": "^3.17.1",
49-
"@stoplight/json-schema-tree": "^2.2.1",
48+
"@stoplight/json": "^3.20.1",
49+
"@stoplight/json-schema-tree": "^2.2.2",
5050
"@stoplight/react-error-boundary": "^2.0.0",
5151
"@types/json-schema": "^7.0.7",
5252
"classnames": "^2.2.6",

src/__tests__/__snapshots__/index.spec.tsx.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,37 @@ exports[`HTML Output given write mode, should populate proper nodes 1`] = `
658658
</div>
659659
"
660660
`;
661+
662+
exports[`HTML Output should render top-level description on allOf 1`] = `
663+
"<div class=\\"\\" id=\\"mosaic-provider-react-aria-0-1\\">
664+
<div data-overlay-container=\\"true\\">
665+
<div class=\\"JsonSchemaViewer\\">
666+
<div></div>
667+
<div style=\\"font-size: 12px\\"><p>This is a description that should be rendered</p></div>
668+
<div data-level=\\"0\\">
669+
<div>
670+
<div>
671+
<div>
672+
<div>
673+
<div>foo</div>
674+
<span>string</span>
675+
</div>
676+
</div>
677+
</div>
678+
</div>
679+
<div>
680+
<div>
681+
<div>
682+
<div>
683+
<div>baz</div>
684+
<span>string</span>
685+
</div>
686+
</div>
687+
</div>
688+
</div>
689+
</div>
690+
</div>
691+
</div>
692+
</div>
693+
"
694+
`;

src/__tests__/index.spec.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,32 @@ describe('HTML Output', () => {
164164

165165
expect(dumpDom(<JsonSchemaViewer schema={schema} />)).toMatchSnapshot();
166166
});
167+
168+
it('should render top-level description on allOf', () => {
169+
const schema: JSONSchema4 = {
170+
description: 'This is a description that should be rendered',
171+
allOf: [
172+
{
173+
type: 'object',
174+
properties: {
175+
foo: {
176+
type: 'string',
177+
},
178+
},
179+
},
180+
{
181+
type: 'object',
182+
properties: {
183+
baz: {
184+
type: 'string',
185+
},
186+
},
187+
},
188+
],
189+
};
190+
191+
expect(dumpDom(<JsonSchemaViewer schema={schema} defaultExpandedDepth={Infinity} />)).toMatchSnapshot();
192+
});
167193
});
168194

169195
describe.each([{}, { unknown: '' }, { $ref: null }])('given empty schema, should render empty text', schema => {

src/components/SchemaRow/TopLevelSchemaRow.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { COMBINER_NAME_MAP } from '../../consts';
77
import { useIsOnScreen } from '../../hooks/useIsOnScreen';
88
import { calculateChildrenToShow, isComplexArray } from '../../tree';
99
import { showPathCrumbsAtom } from '../PathCrumbs/state';
10+
import { Description } from '../shared';
1011
import { ChildStack } from '../shared/ChildStack';
1112
import { getInternalSchemaError } from '../shared/Validations';
1213
import { SchemaRow, SchemaRowProps } from './SchemaRow';
@@ -24,6 +25,7 @@ export const TopLevelSchemaRow = ({ schemaNode }: Pick<SchemaRowProps, 'schemaNo
2425
return (
2526
<>
2627
<ScrollCheck />
28+
<Description value={schemaNode.annotations.description} />
2729
<ChildStack schemaNode={schemaNode} childNodes={childNodes} currentNestingLevel={nestingLevel} />
2830
{internalSchemaError.hasError && (
2931
<Icon title={internalSchemaError.error} color="danger" icon={['fas', 'exclamation-triangle']} size="sm" />
@@ -38,6 +40,7 @@ export const TopLevelSchemaRow = ({ schemaNode }: Pick<SchemaRowProps, 'schemaNo
3840
return (
3941
<>
4042
<ScrollCheck />
43+
<Description value={schemaNode.annotations.description} />
4144

4245
<HStack spacing={3} pb={4}>
4346
<Menu
@@ -79,6 +82,7 @@ export const TopLevelSchemaRow = ({ schemaNode }: Pick<SchemaRowProps, 'schemaNo
7982
return (
8083
<>
8184
<ScrollCheck />
85+
<Description value={schemaNode.annotations.description} />
8286

8387
<Box fontFamily="mono" fontWeight="semibold" fontSize="base" pb={4}>
8488
array of:

src/components/shared/Description.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { MarkdownViewer } from '@stoplight/markdown-viewer';
22
import { Box, Link, Text } from '@stoplight/mosaic';
33
import * as React from 'react';
44

5-
export const Description: React.FunctionComponent<{ value: string }> = ({ value }) => {
5+
export const Description: React.FunctionComponent<{ value: unknown }> = ({ value }) => {
66
const [showAll, setShowAll] = React.useState(false);
77

8+
if (typeof value !== 'string' || value.trim().length === 0) return null;
9+
810
const paragraphs = value.split('\n\n');
911

1012
if (paragraphs.length <= 1 || showAll) {

yarn.lock

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,24 +2347,25 @@
23472347
json-schema-compare "^0.2.2"
23482348
lodash "^4.17.4"
23492349

2350-
"@stoplight/json-schema-tree@^2.2.1":
2351-
version "2.2.1"
2352-
resolved "https://registry.yarnpkg.com/@stoplight/json-schema-tree/-/json-schema-tree-2.2.1.tgz#bfd8ec5a18c4bbc3e0ae3617725e4e4173553f0a"
2353-
integrity sha512-TKAvYtB5U8A7sAKj4co61LnMI9+133keQ+z8yGiLCpvUd8FDCNkQ7TWuFqdWOA1s5La4U6Gc75A1qZqfkGNdJw==
2350+
"@stoplight/json-schema-tree@^2.2.2":
2351+
version "2.2.2"
2352+
resolved "https://registry.yarnpkg.com/@stoplight/json-schema-tree/-/json-schema-tree-2.2.2.tgz#48339973d72ec818efea11c450bf033cd0d78d91"
2353+
integrity sha512-p4hRzEgTMZQRrbP1CWAZDo68zXTOsr40IxITNizKPeMPt1jsB0lvHlLinx0tSTKp2XPnaoRXH7cMsdClGD7JDQ==
23542354
dependencies:
23552355
"@stoplight/json" "^3.12.0"
23562356
"@stoplight/json-schema-merge-allof" "^0.7.7"
23572357
"@stoplight/lifecycle" "^2.3.2"
23582358
"@types/json-schema" "^7.0.7"
23592359
magic-error "0.0.1"
23602360

2361-
"@stoplight/json@^3.12.0", "@stoplight/json@^3.17.1":
2362-
version "3.17.1"
2363-
resolved "https://registry.yarnpkg.com/@stoplight/json/-/json-3.17.1.tgz#17aa8f17c58678649cd0d345fb184e0bea16f8df"
2364-
integrity sha512-OQbjaPU9/VPQYa3zEfN82vjXu3vWv/537Ei7TX/xYLMPdpdBI/mCyThdZAG4SN1t3iho2dxIKpTVObuCTTgBvA==
2361+
"@stoplight/json@^3.12.0", "@stoplight/json@^3.20.1":
2362+
version "3.20.1"
2363+
resolved "https://registry.yarnpkg.com/@stoplight/json/-/json-3.20.1.tgz#a500c5a0ef3232ec3b2fd36c4456b28085d919ae"
2364+
integrity sha512-FXfud+uWgIj1xv6nUO9WnmgmnVikaxJcbtR4XQt4C42n5c2qua3U05Z/3B57hP5TJRSj+tpn9ID6/bFeyYYlEg==
23652365
dependencies:
2366-
"@stoplight/ordered-object-literal" "^1.0.2"
2367-
"@stoplight/types" "^12.3.0"
2366+
"@stoplight/ordered-object-literal" "^1.0.3"
2367+
"@stoplight/path" "^1.3.2"
2368+
"@stoplight/types" "^13.6.0"
23682369
jsonc-parser "~2.2.1"
23692370
lodash "^4.17.21"
23702371
safe-stable-stringify "^1.1"
@@ -2473,10 +2474,15 @@
24732474
use-resize-observer "^9.0.2"
24742475
zustand "^3.5.2"
24752476

2476-
"@stoplight/ordered-object-literal@^1.0.1", "@stoplight/ordered-object-literal@^1.0.2":
2477-
version "1.0.2"
2478-
resolved "https://registry.yarnpkg.com/@stoplight/ordered-object-literal/-/ordered-object-literal-1.0.2.tgz#2a88a5ebc8b68b54837ac9a9ae7b779cdd862062"
2479-
integrity sha512-0ZMS/9sNU3kVo/6RF3eAv7MK9DY8WLjiVJB/tVyfF2lhr2R4kqh534jZ0PlrFB9CRXrdndzn1DbX6ihKZXft2w==
2477+
"@stoplight/ordered-object-literal@^1.0.1", "@stoplight/ordered-object-literal@^1.0.3":
2478+
version "1.0.3"
2479+
resolved "https://registry.yarnpkg.com/@stoplight/ordered-object-literal/-/ordered-object-literal-1.0.3.tgz#1f24572623459c61238ae72176fadac5c40e0a54"
2480+
integrity sha512-cjJ7PPkhgTXNMTkevAlmyrx9xOOCaI3c6rEeYb6VitL1o1WcZtrz9KyFyISmTmUa7yYTiy2IS/ud9S8s2sn3+A==
2481+
2482+
"@stoplight/path@^1.3.2":
2483+
version "1.3.2"
2484+
resolved "https://registry.yarnpkg.com/@stoplight/path/-/path-1.3.2.tgz#96e591496b72fde0f0cdae01a61d64f065bd9ede"
2485+
integrity sha512-lyIc6JUlUA8Ve5ELywPC8I2Sdnh1zc1zmbYgVarhXIp9YeAB0ReeqmGEOWNtlHkbP2DAA1AL65Wfn2ncjK/jtQ==
24802486

24812487
"@stoplight/react-error-boundary@^2.0.0":
24822488
version "2.0.0"
@@ -2530,6 +2536,14 @@
25302536
"@types/json-schema" "^7.0.4"
25312537
utility-types "^3.10.0"
25322538

2539+
"@stoplight/types@^13.6.0":
2540+
version "13.6.0"
2541+
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-13.6.0.tgz#96c6aaae05858b36f589821cd52c95aa9b205ce7"
2542+
integrity sha512-dzyuzvUjv3m1wmhPfq82lCVYGcXG0xUYgqnWfCq3PCVR4BKFhjdkHrnJ+jIDoMKvXb05AZP/ObQF6+NpDo29IQ==
2543+
dependencies:
2544+
"@types/json-schema" "^7.0.4"
2545+
utility-types "^3.10.0"
2546+
25332547
"@stoplight/yaml-ast-parser@0.0.48":
25342548
version "0.0.48"
25352549
resolved "https://registry.yarnpkg.com/@stoplight/yaml-ast-parser/-/yaml-ast-parser-0.0.48.tgz#442b21f419427acaa8a3106ebc5d73351c407002"

0 commit comments

Comments
 (0)