Skip to content

Commit 6458615

Browse files
committed
Fix display of anyOf const values, e.g. DigitalSourceType. Alphabetize Definitions toc links
1 parent 44836cd commit 6458615

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

src/components/SchemaReference.js

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,13 @@ function PropertiesTable({ title, description, properties, required }) {
269269
}
270270

271271
function DefinitionsTOC({ defs }) {
272-
const names = useMemo(() => Object.keys(defs || {}), [defs]);
272+
const names = useMemo(
273+
() =>
274+
Object.keys(defs || {}).sort((a, b) =>
275+
a.localeCompare(b, undefined, { sensitivity: 'base' }),
276+
),
277+
[defs],
278+
);
273279
if (names.length === 0) return null;
274280

275281
const colCount = 4;
@@ -302,11 +308,74 @@ function DefinitionsTOC({ defs }) {
302308
);
303309
}
304310

311+
function getUnionOptions(schema) {
312+
if (!schema) return [];
313+
const union = Array.isArray(schema.oneOf)
314+
? schema.oneOf
315+
: Array.isArray(schema.anyOf)
316+
? schema.anyOf
317+
: null;
318+
if (!Array.isArray(union)) return [];
319+
return union.filter((opt) => opt && typeof opt === 'object');
320+
}
321+
322+
function UnionOptionsTable({ options }) {
323+
if (!options || options.length === 0) return null;
324+
325+
const getTypeLabel = (opt) => {
326+
if (!opt) return 'N/A';
327+
if (typeof opt.type === 'string') return capitalizeType(opt.type);
328+
if (Array.isArray(opt.type))
329+
return opt.type.map(capitalizeType).join(' or ');
330+
if (opt.$ref) {
331+
const name = opt.$ref.startsWith('#/$defs/')
332+
? opt.$ref.replace('#/$defs/', '')
333+
: opt.$ref;
334+
return name;
335+
}
336+
return 'N/A';
337+
};
338+
339+
return (
340+
<table className="manifest-ref-table" style={{ marginTop: 10 }}>
341+
<thead>
342+
<tr>
343+
<th className="manifest-ref-table">Type</th>
344+
<th className="manifest-ref-table">Description</th>
345+
<th className="manifest-ref-table">Value</th>
346+
</tr>
347+
</thead>
348+
<tbody>
349+
{options.map((opt, idx) => (
350+
<tr key={idx}>
351+
<td className="manifest-ref-table">{getTypeLabel(opt)}</td>
352+
<td className="manifest-ref-table">
353+
{opt.description ? (
354+
<div className="prop_desc">
355+
<Markdown text={opt.description} />
356+
</div>
357+
) : (
358+
'N/A'
359+
)}
360+
</td>
361+
<td className="manifest-ref-table">
362+
{Object.prototype.hasOwnProperty.call(opt, 'const')
363+
? String(opt.const)
364+
: 'N/A'}
365+
</td>
366+
</tr>
367+
))}
368+
</tbody>
369+
</table>
370+
);
371+
}
372+
305373
function DefinitionSection({ name, schema }) {
306374
const titleId = slugify(name);
307375
const isObjectType =
308376
(schema && schema.type === 'object') ||
309377
(schema && isObject(schema.properties));
378+
const unionOptions = getUnionOptions(schema);
310379

311380
return (
312381
<>
@@ -320,7 +389,9 @@ function DefinitionSection({ name, schema }) {
320389
</div>
321390
) : null}
322391

323-
{isObjectType ? (
392+
{unionOptions.length > 0 ? (
393+
<UnionOptionsTable options={unionOptions} />
394+
) : isObjectType ? (
324395
<PropertiesTable
325396
properties={schema.properties || {}}
326397
required={schema.required || []}

0 commit comments

Comments
 (0)