fix: refactor openAPI log (#24182)

* fix: refactor openAPI log

* fix: move logs to debug level
This commit is contained in:
Ziyi 2025-08-18 11:19:19 +02:00 committed by GitHub
parent e4700a5963
commit 6b37320810
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,8 @@ import { transformUidToValidOpenApiName } from '@strapi/utils';
import type { Internal } from '@strapi/types'; import type { Internal } from '@strapi/types';
import * as z from 'zod/v4'; import * as z from 'zod/v4';
// Schema generation happens on-demand when schemas don't exist in the registry
/** /**
* Safely adds or updates a schema in Zod's global registry. * Safely adds or updates a schema in Zod's global registry.
* *
@ -24,7 +26,7 @@ export const safeGlobalRegistrySet = (id: Internal.UID.Schema, schema: z.ZodType
if (idMap.has(transformedId)) { if (idMap.has(transformedId)) {
// Remove existing schema to prevent conflicts // Remove existing schema to prevent conflicts
strapi.log.debug(`Removing existing schema ${transformedId} from registry`); strapi.log.debug(`Replacing existing schema ${transformedId} in registry`);
idMap.delete(transformedId); idMap.delete(transformedId);
} }
@ -77,13 +79,27 @@ export const safeSchemaCreation = (id: Internal.UID.Schema, callback: () => z.Zo
// Return existing schema if already registered // Return existing schema if already registered
const mapItem = idMap.get(transformedId); const mapItem = idMap.get(transformedId);
if (mapItem) { if (mapItem) {
strapi.log.debug(`Schema ${transformedId} found in registry, returning existing schema`); // Schema already exists, return it silently
return mapItem; return mapItem;
} }
strapi.log.warn( strapi.log.debug(`Schema ${transformedId} not found in registry, generating new schema`);
`Schema ${transformedId} not found in global registry, creating an any placeholder`
); // Determine if this is a built-in schema or user content
const isBuiltInSchema = id.startsWith('plugin::') || id.startsWith('admin');
if (isBuiltInSchema) {
// Built-in schemas keep at debug level to avoid clutter
strapi.log.debug(`Initializing validation schema for ${transformedId}`);
} else {
// User content
const schemaName = transformedId
.replace('Document', '')
.replace('Entry', '')
.replace(/([A-Z])/g, ' $1')
.trim();
strapi.log.debug(`📝 Generating validation schema for "${schemaName}"`);
}
// Temporary any placeholder before replacing with the actual schema type // Temporary any placeholder before replacing with the actual schema type
// Used to prevent infinite loops in cyclical data structures // Used to prevent infinite loops in cyclical data structures
@ -95,7 +111,16 @@ export const safeSchemaCreation = (id: Internal.UID.Schema, callback: () => z.Zo
// Replace the placeholder with the real schema // Replace the placeholder with the real schema
safeGlobalRegistrySet(id, schema); safeGlobalRegistrySet(id, schema);
strapi.log.debug(`Schema ${transformedId} successfully created and registered`); // Show completion for user content only
if (!isBuiltInSchema) {
const fieldCount = Object.keys((schema as any)?._def?.shape || {}).length || 0;
const schemaName = transformedId
.replace('Document', '')
.replace('Entry', '')
.replace(/([A-Z])/g, ' $1')
.trim();
strapi.log.debug(` ✅ "${schemaName}" schema created with ${fieldCount} fields`);
}
return schema; return schema;
} catch (error) { } catch (error) {