From 74f935bfb9a17a1daf75fbc33e10160c2a8f18c1 Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Tue, 25 Mar 2025 12:19:29 -0400 Subject: [PATCH] fix: auth fields distrupt field paths within the field schema map (#11861) Within auth-enabled collections, we inject the `password` and `confirmPassword` fields into the field schema map. While this is fine within the edit view where these fields are used, this breaks field paths within the version diff view where unnamed fields are no longer able to lookup their corresponding config. This is because the presence of these injected fields increments the field indices by two. A temporary fix for this is to simply inject these fields _last_ into the schema map. This way their presence does not disrupt field path generation. A long term fix should be implemented, however, where these fields actually exist on the collection config itself. This way no config mutation would be required as the sanitized config would the single source of truth. To do this, we'd need to ensure that these fields do not appear in any APIs, and that they do not generate types, etc. --- .../views/Version/RenderFieldsToDiff/buildVersionFields.tsx | 2 ++ packages/next/src/views/Version/index.tsx | 5 +++-- packages/ui/src/utilities/buildClientFieldSchemaMap/index.ts | 3 ++- packages/ui/src/utilities/buildFieldSchemaMap/index.ts | 3 ++- tsconfig.base.json | 2 +- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/next/src/views/Version/RenderFieldsToDiff/buildVersionFields.tsx b/packages/next/src/views/Version/RenderFieldsToDiff/buildVersionFields.tsx index 9d448bf26..5e14d4848 100644 --- a/packages/next/src/views/Version/RenderFieldsToDiff/buildVersionFields.tsx +++ b/packages/next/src/views/Version/RenderFieldsToDiff/buildVersionFields.tsx @@ -75,8 +75,10 @@ export const buildVersionFields = ({ } => { const versionFields: VersionField[] = [] let fieldIndex = -1 + for (const field of fields) { fieldIndex++ + if (fieldIsID(field)) { continue } diff --git a/packages/next/src/views/Version/index.tsx b/packages/next/src/views/Version/index.tsx index e2563f658..30a53a84d 100644 --- a/packages/next/src/views/Version/index.tsx +++ b/packages/next/src/views/Version/index.tsx @@ -37,6 +37,7 @@ export async function VersionView(props: DocumentViewServerProps) { const localeCodesFromParams = searchParams.localeCodes ? JSON.parse(searchParams.localeCodes as string) : null + const comparisonVersionIDFromParams: string = searchParams.compareValue as string const modifiedOnly: boolean = searchParams.modifiedOnly === 'true' @@ -88,7 +89,7 @@ export async function VersionView(props: DocumentViewServerProps) { status: 'published', }) } - } catch (error) { + } catch (_err) { return notFound() } } @@ -129,7 +130,7 @@ export async function VersionView(props: DocumentViewServerProps) { status: 'published', }) } - } catch (error) { + } catch (_err) { return notFound() } } diff --git a/packages/ui/src/utilities/buildClientFieldSchemaMap/index.ts b/packages/ui/src/utilities/buildClientFieldSchemaMap/index.ts index 31762d178..68dce3612 100644 --- a/packages/ui/src/utilities/buildClientFieldSchemaMap/index.ts +++ b/packages/ui/src/utilities/buildClientFieldSchemaMap/index.ts @@ -49,7 +49,8 @@ export const buildClientFieldSchemaMap = (args: { if (matchedCollection.auth && !matchedCollection.auth.disableLocalStrategy) { ;(baseAuthFields[0] as TextFieldClient).label = i18n.t('general:password') ;(baseAuthFields[1] as TextFieldClient).label = i18n.t('authentication:confirmPassword') - fieldsToSet = baseAuthFields.concat(fieldsToSet) + // Place these fields _last_ to ensure they do not disrupt field paths in the field schema map + fieldsToSet = fieldsToSet.concat(baseAuthFields) } clientSchemaMap.set(collectionSlug, { diff --git a/packages/ui/src/utilities/buildFieldSchemaMap/index.ts b/packages/ui/src/utilities/buildFieldSchemaMap/index.ts index 342ea5927..404460ad7 100644 --- a/packages/ui/src/utilities/buildFieldSchemaMap/index.ts +++ b/packages/ui/src/utilities/buildFieldSchemaMap/index.ts @@ -44,7 +44,8 @@ export const buildFieldSchemaMap = (args: { if (matchedCollection.auth && !matchedCollection.auth.disableLocalStrategy) { ;(baseAuthFields[0] as TextField).label = i18n.t('general:password') ;(baseAuthFields[1] as TextField).label = i18n.t('authentication:confirmPassword') - fieldsToSet = baseAuthFields.concat(fieldsToSet) + // Place these fields _last_ to ensure they do not disrupt field paths in the field schema map + fieldsToSet = fieldsToSet.concat(baseAuthFields) } schemaMap.set(collectionSlug, { diff --git a/tsconfig.base.json b/tsconfig.base.json index daa36c721..c9793d25c 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -31,7 +31,7 @@ } ], "paths": { - "@payload-config": ["./test/query-presets/config.ts"], + "@payload-config": ["./test/_community/config.ts"], "@payloadcms/admin-bar": ["./packages/admin-bar/src"], "@payloadcms/live-preview": ["./packages/live-preview/src"], "@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],