fix: ensure default values are not shown when value is hidden (#13074)

Fixes #12834 

`loginAttempts` was being shown in the admin panel when it should be
hidden. The field is set to `hidden: true` therefore the value is
removed from siblingData and passes the `allowDefaultValue` check -
showing inconsistent data.

This PR ensures the default value is not returned if the field has a
value but was removed due to the field being hidden.
This commit is contained in:
Jarrod Flesch
2025-07-08 13:34:10 -04:00
committed by GitHub
parent aa97f3cddb
commit 855a320474
3 changed files with 31 additions and 2 deletions

View File

@@ -114,6 +114,7 @@ export const promise = async ({
const pathSegments = path ? path.split('.') : []
const schemaPathSegments = schemaPath ? schemaPath.split('.') : []
const indexPathSegments = indexPath ? indexPath.split('-').filter(Boolean)?.map(Number) : []
let removedFieldValue = false
if (
fieldAffectsData(field) &&
@@ -121,6 +122,7 @@ export const promise = async ({
typeof siblingDoc[field.name!] !== 'undefined' &&
!showHiddenFields
) {
removedFieldValue = true
delete siblingDoc[field.name!]
}
@@ -331,7 +333,7 @@ export const promise = async ({
// Execute access control
let allowDefaultValue = true
if (triggerAccessControl && field.access && field.access.read) {
const result = overrideAccess
const canReadField = overrideAccess
? true
: await field.access.read({
id: doc.id as number | string,
@@ -342,7 +344,7 @@ export const promise = async ({
siblingData: siblingDoc,
})
if (!result) {
if (!canReadField) {
allowDefaultValue = false
delete siblingDoc[field.name!]
}
@@ -351,6 +353,7 @@ export const promise = async ({
// Set defaultValue on the field for globals being returned without being first created
// or collection documents created prior to having a default
if (
!removedFieldValue &&
allowDefaultValue &&
typeof siblingDoc[field.name!] === 'undefined' &&
typeof field.defaultValue !== 'undefined'

View File

@@ -484,6 +484,12 @@ export default buildConfigWithDefaults(
type: 'checkbox',
hidden: true,
},
{
name: 'hiddenWithDefault',
type: 'text',
hidden: true,
defaultValue: 'default value',
},
],
},
{

View File

@@ -231,6 +231,26 @@ describe('Access Control', () => {
expect(updatedDoc.cannotMutateRequired).toBe('cannotMutateRequired')
expect(updatedDoc.cannotMutateNotRequired).toBe('cannotMutateNotRequired')
})
it('should not return default values for hidden fields with values', async () => {
const doc = await payload.create({
collection: hiddenFieldsSlug,
data: {
title: 'Test Title',
},
showHiddenFields: true,
})
expect(doc.hiddenWithDefault).toBe('default value')
const findDoc2 = await payload.findByID({
id: doc.id,
collection: hiddenFieldsSlug,
overrideAccess: false,
})
expect(findDoc2.hiddenWithDefault).toBeUndefined()
})
})
describe('Collections', () => {
describe('restricted collection', () => {