Skip to content

Commit 1ec5dc6

Browse files
authored
Add support for the optional SchemaFragment#objectRefType property (#1)
1 parent e939434 commit 1ec5dc6

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
.cache-loader
2828
.cache
2929
.rpt2_cache
30+
.publish
3031

3132
# logs
3233
*.log*

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stoplight/json-schema-viewer",
3-
"version": "0.0.0",
3+
"version": "4.16.401",
44
"description": "A beautiful React component for viewing JSON Schema",
55
"keywords": [],
66
"sideEffects": false,
@@ -26,7 +26,7 @@
2626
"commit": "git-cz",
2727
"lint": "eslint 'src/**/*.{ts,tsx}'",
2828
"lint.fix": "yarn lint --fix",
29-
"release": "sl-scripts release",
29+
"release": "rm -rf .publish && mkdir .publish && cp package.json README.md LICENSE .publish/ && cp -r dist/* .publish/ && cd .publish && yarn pack && yarn publish ./stoplight-json-schema-viewer* --registry https://bin.ti8m.ch/artifactory/api/npm/channelsuite-npm-release/",
3030
"release.docs": "sl-scripts release:docs",
3131
"release.dryRun": "sl-scripts release --dry-run --debug",
3232
"storybook": "start-storybook -p 6006",

src/__tests__/index.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2298,7 +2298,8 @@ describe('$ref resolving', () => {
22982298
`);
22992299
});
23002300

2301-
it('should render caret for top-level array with $ref items', () => {
2301+
// skipped as we want '#/foo' to be resolved to "foo" rather than "$ref(#/foo)[]"
2302+
it.skip('should render caret for top-level array with $ref items', () => {
23022303
const schema: JSONSchema4 = {
23032304
type: 'array',
23042305
items: {

src/components/shared/Types.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getLastPathSegment } from '@stoplight/json';
12
import {
23
isBooleanishNode,
34
isReferenceNode,
@@ -71,6 +72,35 @@ export const Types: React.FunctionComponent<{ schemaNode: SchemaNode }> = ({ sch
7172

7273
printedName ??= type + (formats === null || formats[0] !== type ? '' : `<${formats[1]}>`);
7374

75+
// It addresses https://github.com/stoplightio/elements/issues/2762.
76+
// Abstract: instead of displaying "object" or "array[object]" we want to display the actual type of 'object'.
77+
//
78+
// The optional 'objectRefType' property is what ti&m Stoplight Elements core supplies for $ref types.
79+
// In some cases (which ones?), the type information is available in the standard '$ref' property (evaluate both).
80+
//
81+
// We cannot redefine the SchemaFragment type as it's defined outside the scope of this project.
82+
// -> sprinkle with '@ts-ignore'
83+
//
84+
if (type === SchemaNodeKind.Array) {
85+
// @ts-ignore
86+
if (schemaNode.fragment?.items?.objectRefType) {
87+
// @ts-ignore
88+
printedName = `array[${schemaNode.fragment.items.objectRefType}]`;
89+
// @ts-ignore
90+
} else if (schemaNode.fragment?.items?.$ref) {
91+
// @ts-ignore
92+
printedName = getLastPathSegment(schemaNode.fragment.items.$ref as string);
93+
}
94+
} else if (type === SchemaNodeKind.Object) {
95+
// @ts-ignore
96+
if (schemaNode.fragment?.objectRefType) {
97+
// @ts-ignore
98+
printedName = schemaNode.fragment.objectRefType;
99+
} else if (schemaNode.fragment?.$ref) {
100+
printedName = getLastPathSegment(schemaNode.fragment.$ref as string);
101+
}
102+
}
103+
74104
return (
75105
<React.Fragment key={type}>
76106
<Box as="span" textOverflow="truncate" color="muted" data-test="property-type">

0 commit comments

Comments
 (0)