Skip to content

Commit

Permalink
Merge pull request #50 from HiDeoo/hd-fix-array-recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo authored Oct 16, 2024
2 parents 484a0a1 + 46d9882 commit b541a88
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import { getType } from '../../libs/items'
import type { Properties, SchemaObject } from '../../libs/schemaObject'
import Key from '../Key.astro'
import Tag from '../Tag.astro'
Expand All @@ -15,18 +16,24 @@ const { parents, properties, required } = Astro.props
---

{
Object.entries(properties).map(([name, schema]) => (
<Key name={name} required={required?.includes(name)}>
{parents?.indexOf(schema) >= 0 ? (
<div>
<span class="type">object</span>
<Tag>recursive</Tag>
</div>
) : (
<Schema {parents} nested schemaObject={schema} />
)}
</Key>
))
Object.entries(properties).map(([name, schema]) => {
const isRecursive = parents?.some(
(parent) => parent === schema || (schema.type === 'array' && parent === schema.items),
)

return (
<Key name={name} required={required?.includes(name)}>
{isRecursive ? (
<div>
<span class="type">{getType(schema)}</span>
<Tag>recursive</Tag>
</div>
) : (
<Schema parents={parents} nested schemaObject={schema} />
)}
</Key>
)
})
}

<style>
Expand Down
15 changes: 13 additions & 2 deletions packages/starlight-openapi/tests/recursion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ import { expect, test } from './test'

test('displays the recursive tag for a recursive category schema', async ({ docPage }) => {
await docPage.goto('/v3/recursive/operations/listcategories')

const okResponse = docPage.getResponse('200')

await expect(okResponse.getByText('recursive')).toHaveCount(1)
})

test('displays the recursive tag for a recursive post schema', async ({ docPage }) => {
await docPage.goto('/v3/recursive/operations/listposts')

const okResponse = docPage.getResponse('200')

await expect(okResponse.getByText('recursive')).toHaveCount(1)
})

test('displays the recursive tag for a simpler recursive category schema', async ({ docPage }) => {
test('displays the recursive tag for simple and array recursive schema', async ({ docPage }) => {
await docPage.goto('/v3/recursive-simple/operations/listcategories')

const okResponse = docPage.getResponse('200')
await expect(okResponse.getByText('recursive')).toHaveCount(1)

await expect(okResponse.getByText('recursive')).toHaveCount(2)

const descriptions = okResponse.locator('.description')

await expect(descriptions.nth(2)).toHaveText(/object\s+recursive/)
await expect(descriptions.nth(3)).toHaveText(/Array<object>\s+recursive/)
})
149 changes: 77 additions & 72 deletions schemas/v3.0/recursive-simple.yaml
Original file line number Diff line number Diff line change
@@ -1,72 +1,77 @@
openapi: 3.1.0
info:
title: Test Simple Recursion
description: Example of the simple recursion issue.
version: 1.0.0
servers:
- url: 'http://localhost'
paths:
/categories:
get:
summary: List all categories
operationId: listCategories
parameters:
- name: limit
in: query
description: How many categories to return at one time (max 100)
schema:
type: integer
maximum: 50
format: int32
nullable: true
- name: offset
in: query
description: The number of categories to skip before starting to collect the result set
required: false
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of categories
content:
application/json:
schema:
$ref: '#/components/schemas/Categories'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
Category:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
parent:
$ref: '#/components/schemas/Category'
Categories:
type: array
maxItems: 100
items:
$ref: '#/components/schemas/Category'
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
openapi: 3.1.0
info:
title: Test Simple Recursion
description: Example of the simple recursion issue.
version: 1.0.0
servers:
- url: 'http://localhost'
paths:
/categories:
get:
summary: List all categories
operationId: listCategories
parameters:
- name: limit
in: query
description: How many categories to return at one time (max 100)
schema:
type: integer
maximum: 50
format: int32
nullable: true
- name: offset
in: query
description: The number of categories to skip before starting to collect the result set
required: false
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of categories
content:
application/json:
schema:
$ref: '#/components/schemas/Categories'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
Category:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
parent:
$ref: '#/components/schemas/Category'
children:
description: Sub categories
type: array
items:
$ref: '#/components/schemas/Category'
Categories:
type: array
maxItems: 100
items:
$ref: '#/components/schemas/Category'
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string

0 comments on commit b541a88

Please sign in to comment.