Skip to content
Merged
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
74 changes: 59 additions & 15 deletions src/components/ApiDocs/Schema.astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ const isNonArraySchemaObject = (
): object is OpenAPIV3_1.NonArraySchemaObject =>
!!object && !("items" in object);

const getRenderableAttributesSchema = (
object: OpenAPIV3_1.SchemaObject | OpenAPIV3_1.ReferenceObject | undefined,
): OpenAPIV3_1.SchemaObject | null => {
if (!object) return null;

const schema = resolveSchema(object);
const hasProperties = Object.keys(schema.properties || {}).length > 0;

if (hasProperties) return schema;

if (schema.type === "array" && "items" in schema) {
return getRenderableAttributesSchema(schema.items);
}

return null;
};

export interface Props {
kind: string;
schema: OpenAPIV3_1.SchemaObject;
Expand Down Expand Up @@ -55,12 +72,20 @@ const schema = resolveSchema(obj);
}
{!nested && !collapsable && <h3>{schema.title || kind}</h3>}
{
schema.type === "array" && "items" in schema && (
<div>
<ParameterName name={""} def={schema} required={false} />
<Attributes schema={schema.items} kind={"Attributes"} />
</div>
)
schema.type === "array" &&
"items" in schema &&
(() => {
const attributesSchema = getRenderableAttributesSchema(schema.items);

return (
<div>
<ParameterName name={""} def={schema} required={false} />
{attributesSchema && (
<Attributes schema={attributesSchema} kind={"Attributes"} />
)}
</div>
);
})()
}
<ul class:list={[{ nested }]}>
{
Expand Down Expand Up @@ -91,18 +116,37 @@ const schema = resolveSchema(obj);
{propertySchema.description && (
<Markdown>{propertySchema.description}</Markdown>
)}
{"type" in propertySchema && propertySchema.type === "object" && (
{propertySchema.type === "array" &&
!propertySchema.description &&
"items" in propertySchema &&
resolveSchema(propertySchema.items).description && (
<Markdown>
{resolveSchema(propertySchema.items).description}
</Markdown>
)}
{propertySchema.type === "object" &&
getRenderableAttributesSchema(propertySchema) && (
<Attributes
schema={propertySchema}
kind={propertySchema.title || "Attributes"}
/>
)}
{"type" in def && propertySchema.type === "array" && (
<Attributes
schema={propertySchema.items}
kind={propertySchema.title || "Attributes"}
/>
)}
/>
)}
{propertySchema.type === "array" &&
"items" in propertySchema &&
(() => {
const attributesSchema = getRenderableAttributesSchema(
propertySchema.items,
);

return (
attributesSchema && (
<Attributes
schema={attributesSchema}
kind={propertySchema.title || "Attributes"}
/>
)
);
})()}
{isNonArraySchemaObject(propertySchema) &&
propertySchema.example && (
<div class="example">
Expand Down