Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@seamapi/types": "^1.804.0",
"@seamapi/types": "^1.922.0",
"@swc/core": "^1.11.29",
"@types/node": "^24.10.9",
"ava": "^6.0.1",
Expand Down
16 changes: 16 additions & 0 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ export interface DiscriminatedListProperty extends BaseListProperty {
discriminator: string
variantGroups: VariantGroup[]
variants: Array<{
resourceType: string | null
variantGroupKey: string | null
properties: Property[]
description: BaseProperty['description']
Expand Down Expand Up @@ -1794,6 +1795,20 @@ const createArrayProperty = (
discriminator: prop.items.discriminator.propertyName,
variantGroups,
variants: prop.items.oneOf.map((schema) => {
const errorCode = schema.properties?.['error_code']?.enum?.[0]

if (
errorCode != null &&
!(
'x-resource-type' in schema &&
schema['x-resource-type'] != null &&
schema['x-resource-type'].length > 0
)
) {
throw new Error(`Missing resource_type for error code ${errorCode}`)
}

const resourceType = schema['x-resource-type'] ?? null
const variantGroupKey = schema['x-variant-group-key'] ?? null
validateGroupKey(
variantGroupKey,
Expand All @@ -1802,6 +1817,7 @@ const createArrayProperty = (
variantGroups,
)
return {
resourceType,
variantGroupKey,
properties: createProperties(
schema.properties ?? {},
Expand Down
1 change: 1 addition & 0 deletions src/lib/openapi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface OpenapiSchema {
'x-draft'?: string
'x-undocumented'?: string
'x-route-path'?: string
'x-resource-type'?: string
'x-variant-group-key'?: string
'x-enums'?: Record<
string,
Expand Down
37 changes: 37 additions & 0 deletions test/blueprint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,40 @@ test('createBlueprint: throws when a documented endpoint references an undocumen
/Documented endpoints must not reference undocumented resources\. Found:\n.*\/widgets\/get.*secret_widget/,
})
})

test('createBlueprint: throws when an error code is missing resource_type', async (t) => {
const typesModule = TypesModuleSchema.parse(types)
const openapi = structuredClone(typesModule.openapi)

const fooSchema = openapi.components.schemas['foo']
if (fooSchema?.properties == null) {
t.fail('Expected foo schema to have properties')
return
}

fooSchema.properties['errors'] = {
type: 'array',
items: {
discriminator: { propertyName: 'error_code' },
oneOf: [
{
type: 'object',
properties: {
error_code: {
type: 'string',
enum: ['foo_error'],
},
message: {
type: 'string',
},
},
required: ['error_code', 'message'],
},
],
},
}

await t.throwsAsync(() => createBlueprint({ ...typesModule, openapi }), {
message: /Missing resource_type for error code foo_error/,
})
})
Loading
Loading