Skip to content

Commit

Permalink
feat: better names for oneOf/anyOf selector (#151)
Browse files Browse the repository at this point in the history
* feat: better names for oneOf/anyOf selector

* chore: update JST

* fix: only apply $ref based naming for oneOf/anyOf

* chore: add missing snapshot patch

* chore: capitalize -> firstUpper

* chore: missing option
  • Loading branch information
Marcell Toth authored Jun 22, 2021
1 parent 5fe5348 commit 3ceb51e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@fortawesome/free-solid-svg-icons": "^5.15.2",
"@stoplight/json": "^3.10.0",
"@stoplight/json-schema-tree": "^2.0.1",
"@stoplight/json-schema-tree": "^2.1.0",
"@stoplight/react-error-boundary": "^1.0.0",
"@types/json-schema": "^7.0.7",
"classnames": "^2.2.6",
Expand Down
24 changes: 14 additions & 10 deletions src/__fixtures__/combiners/oneof-within-array-item.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
}
},
{
"type": "object",
"title": "B",
"properties": {
"foo": {
"type": "number"
},
"bar": {
"type": "string"
}
}
"$ref": "#/__bundled__/someBundledObj"
}
]
},
"__bundled__": {
"someBundledObj": {
"type": "object",
"properties": {
"foo": {
"type": "number"
},
"bar": {
"type": "string"
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,7 @@ exports[`HTML Output should match combiners/oneof-within-array-item.json 1`] = `
<select size=\\"2\\">
<option></option>
<option value=\\"0\\">1. array of A-s</option>
<option value=\\"1\\">2. array of B-s</option>
<option value=\\"1\\">2. array of SomeBundledObj-s</option>
</select>
</label>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/SchemaRow/useChoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function calculateChoiceTitle(node: SchemaNode, isPlural: boolean): string {
const complexObjectSuffix = isPlural ? '-s' : '';
const primitiveSuffix = isPlural ? 's' : '';
if (isRegularNode(node)) {
const realName = printName(node);
const realName = printName(node, { shouldUseRefNameFallback: true });
if (realName) {
return realName + complexObjectSuffix;
}
Expand Down
33 changes: 27 additions & 6 deletions src/utils/printName.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { getLastPathSegment } from '@stoplight/json';
import { isReferenceNode, isRegularNode, RegularNode, SchemaNodeKind } from '@stoplight/json-schema-tree';
import upperFirst from 'lodash/upperFirst.js';

import { isNonNullable } from '../guards/isNonNullable';
import { isComplexArray, isPrimitiveArray } from '../tree';

export function printName(schemaNode: RegularNode): string | null {
type PrintNameOptions = {
shouldUseRefNameFallback?: boolean;
};

export function printName(
schemaNode: RegularNode,
{ shouldUseRefNameFallback = false }: PrintNameOptions = {},
): string | undefined {
if (
schemaNode.primaryType !== SchemaNodeKind.Array ||
!isNonNullable(schemaNode.children) ||
schemaNode.children.length === 0
) {
return schemaNode.title;
return schemaNode.title ?? (shouldUseRefNameFallback ? getNodeNameFromOriginalRef(schemaNode) : undefined);
}

return printArrayName(schemaNode);
return printArrayName(schemaNode, { shouldUseRefNameFallback });
}

function printArrayName(schemaNode: RegularNode): string | null {
function printArrayName(
schemaNode: RegularNode,
{ shouldUseRefNameFallback = false }: PrintNameOptions,
): string | undefined {
if (!isNonNullable(schemaNode.children) || schemaNode.children.length === 0) {
return schemaNode.title;
return schemaNode.title ?? (shouldUseRefNameFallback ? getNodeNameFromOriginalRef(schemaNode) : undefined);
}

if (schemaNode.children.length === 1 && isReferenceNode(schemaNode.children[0])) {
Expand Down Expand Up @@ -48,6 +60,8 @@ function printArrayName(schemaNode: RegularNode): string | null {
const firstChild = schemaNode.children[0];
if (firstChild.title) {
return `array of ${firstChild.title}-s`;
} else if (shouldUseRefNameFallback && getNodeNameFromOriginalRef(schemaNode)) {
return `array of ${getNodeNameFromOriginalRef(schemaNode)}-s`;
} else if (firstChild.primaryType) {
return `array of ${firstChild.primaryType}s`;
} else if (firstChild.combiners?.length) {
Expand All @@ -56,5 +70,12 @@ function printArrayName(schemaNode: RegularNode): string | null {
return 'array';
}

return null;
return undefined;
}

function getNodeNameFromOriginalRef(node: RegularNode) {
if (typeof node.originalFragment.$ref === 'string') {
return upperFirst(getLastPathSegment(node.originalFragment.$ref));
}
return undefined;
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2393,10 +2393,10 @@
json-schema-compare "^0.2.2"
lodash "^4.17.4"

"@stoplight/json-schema-tree@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@stoplight/json-schema-tree/-/json-schema-tree-2.0.1.tgz#0affbc2c71b7cffab275385b367332fe0eaaf04e"
integrity sha512-K+NP7dNbcwoA3OUuGsG0uZ6PJjVSP9kq0QN5+j9uHCshbLfUHwtgx9JXCX64HFWyew6HhF9wUTnzFGw/vvo+5g==
"@stoplight/json-schema-tree@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@stoplight/json-schema-tree/-/json-schema-tree-2.1.0.tgz#a921d6682971f5734a163ca11817ebd9f0266fbf"
integrity sha512-i4OXbL3A7aslphG+ezxL+ld6dsDtQhw+Iy2jxQYSIjuapvPeP7uY6MxAnbJkOvnRtefYYgCG0pl26xfjrt6n0Q==
dependencies:
"@stoplight/json" "^3.12.0"
"@stoplight/json-schema-merge-allof" "^0.7.5"
Expand Down

0 comments on commit 3ceb51e

Please sign in to comment.