Skip to content

Commit

Permalink
Merge pull request #53 from HiDeoo/hd-fix-anyof-oneof-nested-allof
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo authored Nov 25, 2024
2 parents af65983 + 4ae0454 commit afc9856
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const isNegated = schemaObject.not !== undefined
{isSchemaObjectObject(schemaObject) ? (
<SchemaObjectObject {parents} {nested} {schemaObject} />
) : isSchemaObjectAllOf(schemaObject) ? (
<SchemaObjectAllOf {parents} {schemaObject} />
<SchemaObjectAllOf {nested} {parents} {schemaObject} />
) : (
<Items {parents} items={schemaObject} {negated} nullable={getNullable(schemaObject)} />
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,55 @@
import {
getNullable,
getProperties,
getSchemaObjects,
isSchemaObject,
isSchemaObjectObject,
type SchemaObject,
} from '../../libs/schemaObject'
import Items from '../Items.astro'
import SchemaObjectObjectProperties from './SchemaObjectObjectProperties.astro'
import SchemaObjects from './SchemaObjects.astro'
interface Props {
nested: boolean
parents?: SchemaObject[]
schemaObject: SchemaObject
}
const { schemaObject, parents = [] } = Astro.props
const { nested, schemaObject, parents = [] } = Astro.props
---

{
schemaObject.allOf &&
schemaObject.allOf.map((allOfSchemaObject) =>
isSchemaObjectObject(schemaObject) && isSchemaObject(allOfSchemaObject) ? (
<SchemaObjectObjectProperties
parents={[...parents, schemaObject]}
properties={getProperties(allOfSchemaObject)}
required={allOfSchemaObject.required}
/>
) : isSchemaObject(allOfSchemaObject) ? (
schemaObject.allOf.map((allOfSchemaObject) => {
if (!isSchemaObject(allOfSchemaObject)) return null
const schemaObjects = getSchemaObjects(allOfSchemaObject)
if (schemaObjects !== undefined) {
return (
<SchemaObjects
parents={[...parents, schemaObject]}
discriminator={schemaObject.discriminator}
{nested}
{schemaObjects}
/>
)
} else if (isSchemaObjectObject(schemaObject)) {
return (
<SchemaObjectObjectProperties
parents={[...parents, schemaObject]}
properties={getProperties(allOfSchemaObject)}
required={allOfSchemaObject.required}
/>
)
}
return (
<Items
items={allOfSchemaObject}
negated={allOfSchemaObject.not !== undefined}
nullable={getNullable(allOfSchemaObject)}
parents={[...parents, schemaObject]}
/>
) : null,
)
)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const properties = getProperties(schemaObject)
]}
/>
<SchemaObjectObjectProperties parents={[...parents, schemaObject]} {properties} required={schemaObject.required} />
<SchemaObjectAllOf {parents} {schemaObject} />
<SchemaObjectAllOf {nested} {parents} {schemaObject} />
{
schemaObject.additionalProperties && (
<Key additional name="key">
Expand Down
14 changes: 14 additions & 0 deletions packages/starlight-openapi/tests/requestBody.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ test('supports schema object `allOf` property for non-objects', async ({ docPage
await expect(typeParameter.getByText('Allowed values: berkshire tamworth')).toBeVisible()
})

test('supports schema object `allOf` property with nested schema objects like `anyOf`', async ({ docPage }) => {
await docPage.goto('/v3/animals/operations/okapi/')

const requestBody = docPage.getRequestBody()

await expect(requestBody.getByText('Any of:')).toBeVisible()

await expect(requestBody.getByRole('tab')).toContainText(['basic details', 'advanced details'])

await requestBody.getByRole('tab', { name: 'advanced details' }).click()

await expect(requestBody.getByText('age')).toBeVisible()
})

test('supports schema object `oneOf` property', async ({ docPage }) => {
await docPage.goto('/v2/animals/operations/addbird/')

Expand Down
47 changes: 47 additions & 0 deletions schemas/v3.0/animals.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,53 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/okapi:
post:
summary: Creates a new okapi
tags:
- animals
requestBody:
description: Okapi to add
required: true
content:
application/json:
schema:
allOf:
- type: object
properties:
id:
type: integer
format: int64
- type: object
properties:
name:
type: string
- anyOf:
- type: object
title: 'basic details'
properties:
color:
type: string
- type: object
title: 'advanced details'
properties:
color:
type: string
age:
type: integer
responses:
'200':
description: animal response
content:
application/json:
schema:
$ref: '#/components/schemas/Animal'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
webhooks:
newAnimal:
post:
Expand Down

0 comments on commit afc9856

Please sign in to comment.