Skip to content
Open
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
60 changes: 60 additions & 0 deletions src/openapi/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,51 @@ async function jsonSchemaToOpenapiSchema(schema: JSONSchema4): Promise<OpenAPIV3
return await (_jsonSchemaToOpenapiSchema as any)(schema)
}

// Helper function to check if PhoneNumber reference exists in generated schemas
// This is more reliable than checking field types since phoneNumberField from plugin
// may not expose its type directly
const hasPhoneNumberReference = (payload: PayloadRequest['payload']): boolean => {
// Check all collections by generating schema and looking for PhoneNumber reference
for (const collection of Object.values(payload.collections)) {
try {
const schema = entityToJSONSchema(
payload.config,
removeInterfaceNames(collection.config),
new Map(),
'text',
undefined,
)
// Check if schema contains PhoneNumber reference
const schemaStr = JSON.stringify(schema)
if (schemaStr.includes('"#/definitions/PhoneNumber"') || schemaStr.includes('PhoneNumber')) {
return true
}
} catch (e) {
// If schema generation fails, continue checking other collections
continue
}
}
// Check all globals
for (const global of payload.globals.config) {
try {
const schema = entityToJSONSchema(
payload.config,
removeInterfaceNames(global),
new Map(),
'text',
undefined,
)
const schemaStr = JSON.stringify(schema)
if (schemaStr.includes('"#/definitions/PhoneNumber"') || schemaStr.includes('PhoneNumber')) {
return true
}
} catch (e) {
continue
}
}
return false
}

const adjustRefTargets = (
payload: PayloadRequest['payload'],
spec: Record<string, unknown>,
Expand All @@ -55,6 +100,11 @@ const adjustRefTargets = (
return '#/components/schemas/supportedTimezones'
}

// Handle PhoneNumber custom field type from payload-phone-number-plugin
if (name === 'PhoneNumber') {
return '#/components/schemas/PhoneNumber'
}

const collection = payload.collections[name]
if (collection !== undefined) {
name = collectionName(payload.collections[name]).singular
Expand Down Expand Up @@ -593,6 +643,16 @@ const generateComponents = (req: Pick<PayloadRequest, 'payload'>) => {
},
}

// Only add PhoneNumber schema if it's actually used (has PhoneNumber reference in any schema)
if (hasPhoneNumberReference(req.payload)) {
schemas.PhoneNumber = {
type: 'string',
format: 'tel',
description: 'Phone number in E.164 format (e.g., +84901230000)',
example: '+84901230000',
}
}

for (const collection of Object.values(req.payload.collections)) {
const { singular } = collectionName(collection)
schemas[componentName('schemas', singular)] = generateSchemaObject(
Expand Down