Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
62 changes: 60 additions & 2 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ interface RecordProperty extends BaseProperty {
interface ListProperty extends BaseProperty {
format: 'list'
jsonType: 'array'
itemFormat?: Property['format'] | 'discriminated_object'
Copy link
Copy Markdown
Member

@razor-x razor-x Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of all of these optional values, we should follow the pattern already established by properly defining what kind of array properties we expect.

interface StringListProperty extends ListProperty {
  itemFormat: 'string'
}

interface NumberListProperty extends ListProperty {
  itemFormat: 'number'
}

interface EnumListProperty extends ListProperty {
  itemFormat: 'enum'
  itemsValues: EnumValue[]
}

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

interface DiscriminatedListProperty extends ListProperty {
  itemFormat: 'object'
	itemDiscriminatorKey: string
  itemVariants: Array<{
    variantName: string // the discriminator value
    properties: Property[]
    description: string
  }>
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

itemProperties?: Property[]
itemEnumValues?: EnumValue[]
discriminator?: string
variants?: Array<{
jsonType: string
properties: Property[]
description: BaseProperty['description']
}>
}

interface BooleanProperty extends BaseProperty {
Expand Down Expand Up @@ -1113,8 +1122,57 @@ 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': {
const baseListProperty: ListProperty = {
...baseProperty,
format: 'list',
jsonType: 'array',
}

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

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

return {
...baseListProperty,
itemFormat: 'discriminated_object',
variants: prop.items.oneOf.map((schema) => ({
jsonType: schema.type ?? '',
properties: createProperties(schema.properties ?? {}, [
...parentPaths,
name,
]),
description: schema.description ?? '',
})),
...(prop.items.discriminator?.propertyName != null && {
discriminator: prop.items.discriminator.propertyName,
}),
}
}

const itemProperty = createProperty('items', prop.items, [
...parentPaths,
name,
])
return {
...baseProperty,
format: 'list',
jsonType: 'array',
itemFormat: itemProperty.format,
...(itemProperty.format === 'object' && {
itemProperties: itemProperty.properties,
}),
...(itemProperty.format === 'enum' && {
itemEnumValues: itemProperty.values,
}),
}
}

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 @@ -91,6 +91,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 @@ -393,6 +393,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 @@ -1575,6 +1576,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