Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
147 changes: 144 additions & 3 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,66 @@ interface RecordProperty extends BaseProperty {
jsonType: 'object'
}

interface ListProperty extends BaseProperty {
interface BaseListProperty extends BaseProperty {
format: 'list'
jsonType: 'array'
itemFormat: string
}

interface StringListProperty extends BaseListProperty {
itemFormat: 'string'
}

interface NumberListProperty extends BaseListProperty {
itemFormat: 'number'
}

interface BooleanListProperty extends BaseListProperty {
itemFormat: 'boolean'
}

interface DatetimeListProperty extends BaseListProperty {
itemFormat: 'datetime'
}

interface IdListProperty extends BaseListProperty {
itemFormat: 'id'
}

interface EnumListProperty extends BaseListProperty {
itemFormat: 'enum'
itemEnumValues: EnumValue[]
}

interface ObjectListProperty extends BaseListProperty {
itemFormat: 'object'
itemProperties: Property[]
}

interface RecordListProperty extends BaseListProperty {
itemFormat: 'record'
}

interface DiscriminatedListProperty extends BaseListProperty {
itemFormat: 'discriminated_object'
discriminator: string
variants: Array<{
properties: Property[]
description: BaseProperty['description']
}>
}

type ListProperty =
| StringListProperty
| NumberListProperty
| BooleanListProperty
| DatetimeListProperty
| IdListProperty
| EnumListProperty
| ObjectListProperty
| RecordListProperty
| DiscriminatedListProperty

interface BooleanProperty extends BaseProperty {
format: 'boolean'
jsonType: 'boolean'
Expand Down Expand Up @@ -1142,8 +1197,94 @@ const createProperty = (
return { ...baseProperty, format: 'string', jsonType: 'string' }
case 'boolean':
return { ...baseProperty, format: 'boolean', jsonType: 'boolean' }
case 'array':
return { ...baseProperty, format: 'list', jsonType: 'array' }
case 'array': {
function createListProperty<T extends BaseListProperty>(
format: string,
extraProps = {},
): T {
const baseListProps = {
...baseProperty,
format: 'list' as const,
jsonType: 'array' as const,
itemFormat: format,
...extraProps,
}

return baseListProps as T
}

const fallbackListProperty =
createListProperty<RecordListProperty>('record')

if (prop.items == null) {
return fallbackListProperty
}

if ('oneOf' in prop.items) {
if (!prop.items.oneOf.every((schema) => schema.type === 'object')) {
return fallbackListProperty
}

if (prop.items.discriminator?.propertyName == null) {
throw new Error(
`Missing discriminator property name for ${name} in ${parentPaths.join('.')}`,
)
}

return createListProperty<DiscriminatedListProperty>(
'discriminated_object',
{
discriminator: prop.items.discriminator.propertyName,
variants: prop.items.oneOf.map((schema) => ({
properties: createProperties(schema.properties ?? {}, [
...parentPaths,
name,
]),
description: schema.description ?? '',
})),
},
)
}

const itemProperty = createProperty('item', prop.items, [
...parentPaths,
name,
])

switch (itemProperty.format) {
case 'string':
return createListProperty<StringListProperty>('string')

case 'number':
return createListProperty<NumberListProperty>('number')

case 'boolean':
return createListProperty<BooleanListProperty>('boolean')

case 'datetime':
return createListProperty<DatetimeListProperty>('datetime')

case 'id':
return createListProperty<IdListProperty>('id')

case 'enum':
return createListProperty<EnumListProperty>('enum', {
itemEnumValues: itemProperty.values,
})

case 'object':
return createListProperty<ObjectListProperty>('object', {
itemProperties: itemProperty.properties,
})

case 'record':
return createListProperty<RecordListProperty>('record')

default:
return fallbackListProperty
}
}

case 'object':
if (prop.properties !== undefined) {
return {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/openapi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export interface OpenapiSchema {
>
oneOf?: OpenapiSchema[]
allOf?: OpenapiSchema[]
discriminator?: {
propertyName: string
}
}

export interface OpenapiComponents {
Expand Down
2 changes: 2 additions & 0 deletions test/snapshots/blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ Generated by [AVA](https://avajs.dev).
isDeprecated: false,
isDraft: false,
isUndocumented: false,
itemFormat: 'string',
jsonType: 'array',
name: 'array_prop',
undocumentedMessage: '',
Expand Down Expand Up @@ -1618,6 +1619,7 @@ Generated by [AVA](https://avajs.dev).
isDeprecated: false,
isDraft: false,
isUndocumented: false,
itemFormat: 'string',
jsonType: 'array',
name: 'array_prop',
undocumentedMessage: '',
Expand Down
Binary file modified test/snapshots/blueprint.test.ts.snap
Binary file not shown.
Loading