diff --git a/.idea/runConfigurations/_template__of_JavaScriptTestRunnerJest.xml b/.idea/runConfigurations/_template__of_JavaScriptTestRunnerJest.xml deleted file mode 100644 index 7fe3092814..0000000000 --- a/.idea/runConfigurations/_template__of_JavaScriptTestRunnerJest.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/packages/payload/src/fields/hooks/afterRead/promise.ts b/packages/payload/src/fields/hooks/afterRead/promise.ts index a06c7ab706..daefdf77e5 100644 --- a/packages/payload/src/fields/hooks/afterRead/promise.ts +++ b/packages/payload/src/fields/hooks/afterRead/promise.ts @@ -307,7 +307,11 @@ export const promise = async ({ } } - if ('virtual' in field && typeof field.virtual === 'string') { + if ( + 'virtual' in field && + typeof field.virtual === 'string' && + (!field.hidden || showHiddenFields) + ) { populationPromises.push( virtualFieldPopulationPromise({ name: field.name, diff --git a/test/database/config.ts b/test/database/config.ts index a25e358a0c..1302c5b95b 100644 --- a/test/database/config.ts +++ b/test/database/config.ts @@ -466,6 +466,12 @@ export default buildConfigWithDefaults({ type: 'text', virtual: 'post.title', }, + { + name: 'postTitleHidden', + type: 'text', + virtual: 'post.title', + hidden: true, + }, { name: 'postCategoryTitle', type: 'text', diff --git a/test/database/int.spec.ts b/test/database/int.spec.ts index a7dbf81032..100bac8069 100644 --- a/test/database/int.spec.ts +++ b/test/database/int.spec.ts @@ -2014,6 +2014,26 @@ describe('database', () => { expect(doc.postTitle).toBe('my-title-10') }) + it('should respect hidden: true for virtual fields with reference', async () => { + const post = await payload.create({ collection: 'posts', data: { title: 'my-title-3' } }) + const { id } = await payload.create({ + collection: 'virtual-relations', + depth: 0, + data: { post: post.id }, + }) + + const doc = await payload.findByID({ collection: 'virtual-relations', depth: 0, id }) + expect(doc.postTitleHidden).toBeUndefined() + + const doc_show = await payload.findByID({ + collection: 'virtual-relations', + depth: 0, + id, + showHiddenFields: true, + }) + expect(doc_show.postTitleHidden).toBe('my-title-3') + }) + it('should allow virtual field as reference to ID', async () => { const post = await payload.create({ collection: 'posts', data: { title: 'my-title' } }) const { id } = await payload.create({ diff --git a/test/database/payload-types.ts b/test/database/payload-types.ts index 88bc6d131d..ce2a3354d2 100644 --- a/test/database/payload-types.ts +++ b/test/database/payload-types.ts @@ -374,6 +374,7 @@ export interface Place { export interface VirtualRelation { id: string; postTitle?: string | null; + postTitleHidden?: string | null; postCategoryTitle?: string | null; postCategoryID?: | { @@ -828,6 +829,7 @@ export interface PlacesSelect { */ export interface VirtualRelationsSelect { postTitle?: T; + postTitleHidden?: T; postCategoryTitle?: T; postCategoryID?: T; postID?: T;