@@ -101,8 +101,27 @@ const uidCorrector = ({ uid } : {uid : string}) => {
101101 return newUid ;
102102} ;
103103
104+ /**
105+ * Remap an array of reference UIDs using a mapping table.
106+ *
107+ * @param uids - The original reference UIDs.
108+ * @param keyMapper - A map from UID to new UID. Callers should prefer using
109+ * the *corrected* UID (i.e. the result of `uidCorrector({ uid })`) as the key.
110+ * For backward compatibility, this function also supports maps keyed by the
111+ * original UID, and will try both forms when looking up each entry.
112+ *
113+ * NOTE: Relying on mixed key styles (some original, some corrected) can hide
114+ * inconsistent UID formatting. When both key styles are present for the same
115+ * logical UID and map to different targets, a warning is logged so that such
116+ * issues do not go unnoticed.
117+ * @returns The remapped UIDs.
118+ */
119+ function remapReferenceUids ( uids : string [ ] , keyMapper ?: Record < string , string > ) : string [ ] {
120+ if ( ! keyMapper || ! Object . keys ( keyMapper ) . length ) return uids ;
121+ return uids . map ( uid => keyMapper [ uid ] ?? keyMapper [ uidCorrector ( { uid } ) ] ?? uid ) ;
122+ }
104123
105- function buildFieldSchema ( item : any , marketPlacePath : string , parentUid = '' ) : any {
124+ function buildFieldSchema ( item : any , marketPlacePath : string , parentUid = '' , keyMapper ?: Record < string , string > ) : any {
106125 if ( item ?. isDeleted === true ) return null ;
107126
108127 const getCleanUid = ( uid : string ) : string => {
@@ -155,7 +174,7 @@ function buildFieldSchema(item: any, marketPlacePath: string, parentUid = ''): a
155174 const blockElements = blockItem ?. schema || [ ] ;
156175 for ( const element of blockElements ) {
157176 if ( element ?. isDeleted === false ) {
158- const fieldSchema = buildFieldSchema ( element , marketPlacePath , '' ) ;
177+ const fieldSchema = buildFieldSchema ( element , marketPlacePath , '' , keyMapper ) ;
159178 if ( fieldSchema ) blockSchema . push ( fieldSchema ) ;
160179 }
161180 }
@@ -186,7 +205,7 @@ function buildFieldSchema(item: any, marketPlacePath: string, parentUid = ''): a
186205
187206 for ( const element of elements ) {
188207 if ( element ?. isDeleted === false ) {
189- const fieldSchema = buildFieldSchema ( element , marketPlacePath , '' ) ;
208+ const fieldSchema = buildFieldSchema ( element , marketPlacePath , '' , keyMapper ) ;
190209 if ( fieldSchema ) groupSchema . push ( fieldSchema ) ;
191210 }
192211 }
@@ -210,7 +229,8 @@ function buildFieldSchema(item: any, marketPlacePath: string, parentUid = ''): a
210229 title : item ?. display_name || rawUid , // Keep original for display
211230 uid : itemUid // Snake case uid
212231 } ,
213- marketPlacePath
232+ marketPlacePath,
233+ keyMapper
214234 } ) ;
215235}
216236
@@ -470,10 +490,10 @@ export const convertToSchemaFormate = ({ field, advanced = false, marketPlacePat
470490 "error_messages" : {
471491 "format" : field ?. advanced ?. validationErrorMessage ?? '' ,
472492 } ,
473- "reference_to" : field ?. advanced ?. embedObjects ?. length ? [
493+ "reference_to" : field ?. advanced ?. embedObjects ?. length ? remapReferenceUids ( [
474494 "sys_assets" ,
475495 ...field ?. advanced ?. embedObjects ?. map ?.( ( item : any ) => uidCorrector ( { uid : item } ) ) ?? [ ] ,
476- ] : [
496+ ] , keyMapper ) : [
477497 "sys_assets"
478498 ] ,
479499 "multiple" : field ?. advanced ?. multiple ?? false ,
@@ -736,7 +756,7 @@ export const convertToSchemaFormate = ({ field, advanced = false, marketPlacePat
736756 return {
737757 "data_type" : "global_field" ,
738758 "display_name" : field ?. title ,
739- "reference_to" : field ?. refrenceTo ?? [ ] ,
759+ "reference_to" : remapReferenceUids ( field ?. refrenceTo ?? [ ] , keyMapper ) ,
740760 "uid" : cleanedUid ,
741761 "mandatory" : field ?. advanced ?. mandatory ?? false ,
742762 "multiple" : field ?. advanced ?. multiple ?? false ,
@@ -748,7 +768,7 @@ export const convertToSchemaFormate = ({ field, advanced = false, marketPlacePat
748768 return {
749769 data_type : "reference" ,
750770 display_name : field ?. title ,
751- reference_to : field ?. refrenceTo ?? [ ] ,
771+ reference_to : remapReferenceUids ( field ?. refrenceTo ?? [ ] , keyMapper ) ,
752772 field_metadata : {
753773 ref_multiple : true ,
754774 ref_multiple_content_types : true
@@ -816,7 +836,7 @@ export const convertToSchemaFormate = ({ field, advanced = false, marketPlacePat
816836 "mandatory" : field ?. advanced ?. mandatory ?? false ,
817837 "unique" : field ?. advanced ?. unique ?? false ,
818838 "non_localizable" : field . advanced ?. nonLocalizable ?? false ,
819- "reference_to" : field ?. advanced ?. embedObjects ?. length ? field ?. advanced ?. embedObjects ?. map ?.( ( item : any ) => uidCorrector ( { uid : item } ) ) : [ ]
839+ "reference_to" : field ?. advanced ?. embedObjects ?. length ? remapReferenceUids ( field ?. advanced ?. embedObjects ?. map ?.( ( item : any ) => uidCorrector ( { uid : item } ) ) , keyMapper ) : [ ]
820840 }
821841 if ( ( field ?. advanced ?. embedObjects ?. length === undefined ) ||
822842 ( field ?. advanced ?. embedObjects ?. length === 0 ) ||
@@ -1177,7 +1197,7 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
11771197 for ( const item of ctData ) {
11781198 if ( item ?. isDeleted === true ) continue ;
11791199
1180- const fieldSchema = buildFieldSchema ( item , marketPlacePath , '' ) ;
1200+ const fieldSchema = buildFieldSchema ( item , marketPlacePath , '' , keyMapper ) ;
11811201 if ( fieldSchema ) {
11821202 ct ?. schema . push ( fieldSchema ) ;
11831203 }
0 commit comments