22import { Tabs , TabItem } from " @astrojs/starlight/components" ;
33
44import { getType } from " starlight-openapi/libs/items" ;
5- import type {
6- Discriminator ,
7- SchemaObjects ,
8- SchemaObject as SchemaObjectType ,
5+ import {
6+ getSchemaObjects ,
7+ type Discriminator ,
8+ type SchemaObjects ,
9+ type SchemaObject as SchemaObjectType ,
910} from " starlight-openapi/libs/schemaObject" ;
1011import Tag from " ../Tag.astro" ;
1112
@@ -15,6 +16,7 @@ interface Props {
1516 discriminator: Discriminator ;
1617 nested: boolean ;
1718 parents? : SchemaObjectType [];
19+ schemaObject: SchemaObjectType ;
1820 schemaObjects: SchemaObjects ;
1921}
2022
@@ -36,6 +38,29 @@ const humanReadableType: Record<SchemaObjects["type"], string> = {
3638 anyOf: " Any of" ,
3739 oneOf: " One of" ,
3840};
41+
42+ function isCircularArrayItem(item : SchemaObjectType , parents : SchemaObjectType [] = []): boolean {
43+ if (parents .some ((parent ) => parent === item )) {
44+ return true ;
45+ }
46+
47+ if (
48+ item .type !== " array" ||
49+ ! item .items ||
50+ " $ref" in item .items ||
51+ item .items .type !== " object"
52+ ) {
53+ return false ;
54+ }
55+
56+ const nestedSchemaObjects = getSchemaObjects (item .items );
57+
58+ return (
59+ nestedSchemaObjects ?.schemaObjects .some ((nestedSchemaObject ) =>
60+ isCircularArrayItem (nestedSchemaObject , [... parents , item ]),
61+ ) ?? false
62+ );
63+ }
3964---
4065
4166<div >
@@ -46,7 +71,11 @@ const humanReadableType: Record<SchemaObjects["type"], string> = {
4671 {
4772 schemaObjects .map ((schemaObject ) => (
4873 <TabItem label = { schemaObject .title ?? getType (schemaObject ) ?? " unknown" } >
49- <SchemaObject { parents } { nested } schemaObject = { schemaObject } />
74+ { isCircularArrayItem (schemaObject ) ? (
75+ " Self reference"
76+ ) : (
77+ <SchemaObject { parents } { nested } schemaObject = { schemaObject } />
78+ )}
5079 </TabItem >
5180 ))
5281 }
0 commit comments