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
1 change: 1 addition & 0 deletions .changepacks/changepack_log_RFrGmzUhQg013UxX2kc_T.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"packages/generator/package.json":"Patch"},"note":"Fix ref zod issue","date":"2026-03-30T05:29:19.021573100Z"}
53 changes: 52 additions & 1 deletion packages/generator/src/generate-zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ interface CollectedSchemas {
requestSchemas: Record<string, SchemaInfo>
responseSchemas: Record<string, SchemaInfo>
errorSchemas: Record<string, SchemaInfo>
/** Schemas referenced via $ref within category schemas but not themselves a category schema */
dependencySchemas: Record<string, SchemaInfo>
pathMappings: Record<
'get' | 'post' | 'put' | 'delete' | 'patch',
Record<string, PathSchemaMapping>
Expand Down Expand Up @@ -621,7 +623,45 @@ function generateSchemasForDocument(
}
}

return { requestSchemas, responseSchemas, errorSchemas, pathMappings }
// Collect all transitively referenced schemas from category schemas
const allCategoryNames = new Set([
...requestSchemaNames,
...responseSchemaNames,
...errorSchemaNames,
])
const allReferencedNames = new Set<string>()
for (const schemaName of allCategoryNames) {
const schemaDef = schema.components?.schemas?.[schemaName]
if (schemaDef) {
collectSchemaNames(
schemaDef as OpenAPIV3_1.SchemaObject | OpenAPIV3_1.ReferenceObject,
allReferencedNames,
{ followComponentRefs: true, document: schema },
)
}
}

// Generate dependency schemas (referenced via $ref but need _Name variables)
const dependencySchemas: Record<string, SchemaInfo> = {}
for (const name of allReferencedNames) {
const schemaDef = schema.components?.schemas?.[name]
if (!schemaDef) continue
const schemaRef = schemaDef as
| OpenAPIV3_1.SchemaObject
| OpenAPIV3_1.ReferenceObject
dependencySchemas[name] = {
code: schemaToZod(schemaRef, schema, schemaRefs),
type: schemaToZodType(schemaRef, schema),
}
}

return {
requestSchemas,
responseSchemas,
errorSchemas,
dependencySchemas,
pathMappings,
}
}

// =============================================================================
Expand Down Expand Up @@ -657,6 +697,17 @@ export function generateZodSchemas(
for (const [serverName, collected] of Object.entries(serverSchemas)) {
const safeServerName = serverName.replace(/[^a-zA-Z0-9]/g, '_')

// Dependency schemas (referenced via $ref, need _Name variables for z.lazy)
if (Object.keys(collected.dependencySchemas).length > 0) {
lines.push(`// Shared dependency schemas for ${serverName}`)
for (const [name, schemaInfo] of Object.entries(
collected.dependencySchemas,
)) {
lines.push(`const _${name} = ${schemaInfo.code};`)
}
lines.push('')
}

// Request schemas
if (Object.keys(collected.requestSchemas).length > 0) {
lines.push(`// Request schemas for ${serverName}`)
Expand Down