@@ -15,7 +15,6 @@ import type { SanitizedGlobalConfig } from '../globals/config/types.js'
15
15
import { MissingEditorProp } from '../errors/MissingEditorProp.js'
16
16
import { fieldAffectsData } from '../fields/config/types.js'
17
17
import { generateJobsJSONSchemas } from '../queues/config/generateJobsJSONSchemas.js'
18
- import { deepCopyObject } from './deepCopyObject.js'
19
18
import { toWords } from './formatLabels.js'
20
19
import { getCollectionIDFieldTypes } from './getCollectionIDFieldTypes.js'
21
20
@@ -719,7 +718,7 @@ export function fieldsToJSONSchema(
719
718
// This function is part of the public API and is exported through payload/utilities
720
719
export function entityToJSONSchema (
721
720
config : SanitizedConfig ,
722
- incomingEntity : SanitizedCollectionConfig | SanitizedGlobalConfig ,
721
+ entity : SanitizedCollectionConfig | SanitizedGlobalConfig ,
723
722
interfaceNameDefinitions : Map < string , JSONSchema4 > ,
724
723
defaultIDType : 'number' | 'text' ,
725
724
collectionIDFieldTypes ?: { [ key : string ] : 'number' | 'string' } ,
@@ -729,25 +728,30 @@ export function entityToJSONSchema(
729
728
collectionIDFieldTypes = getCollectionIDFieldTypes ( { config, defaultIDType } )
730
729
}
731
730
732
- const entity : SanitizedCollectionConfig | SanitizedGlobalConfig = deepCopyObject ( incomingEntity )
733
731
const title = entity . typescript ?. interface
734
732
? entity . typescript . interface
735
733
: singular ( toWords ( entity . slug , true ) )
736
734
735
+ let mutableFields = [ ...entity . flattenedFields ]
736
+
737
737
const idField : FieldAffectingData = { name : 'id' , type : defaultIDType as 'text' , required : true }
738
- const customIdField = entity . flattenedFields . find (
739
- ( field ) => field . name === 'id' ,
740
- ) as FieldAffectingData
738
+ const customIdField = mutableFields . find ( ( field ) => field . name === 'id' ) as FieldAffectingData
741
739
742
740
if ( customIdField && customIdField . type !== 'group' && customIdField . type !== 'tab' ) {
743
- customIdField . required = true
741
+ mutableFields = mutableFields . map ( ( field ) => {
742
+ if ( field === customIdField ) {
743
+ return { ...field , required : true }
744
+ }
745
+
746
+ return field
747
+ } )
744
748
} else {
745
- entity . flattenedFields . unshift ( idField )
749
+ mutableFields . unshift ( idField )
746
750
}
747
751
748
752
// mark timestamp fields required
749
753
if ( 'timestamps' in entity && entity . timestamps !== false ) {
750
- entity . flattenedFields = entity . flattenedFields . map ( ( field ) => {
754
+ mutableFields = mutableFields . map ( ( field ) => {
751
755
if ( field . name === 'createdAt' || field . name === 'updatedAt' ) {
752
756
return {
753
757
...field ,
@@ -765,7 +769,7 @@ export function entityToJSONSchema(
765
769
( typeof entity . auth ?. disableLocalStrategy === 'object' &&
766
770
entity . auth . disableLocalStrategy . enableFields ) )
767
771
) {
768
- entity . flattenedFields . push ( {
772
+ mutableFields . push ( {
769
773
name : 'password' ,
770
774
type : 'text' ,
771
775
} )
@@ -777,7 +781,7 @@ export function entityToJSONSchema(
777
781
title,
778
782
...fieldsToJSONSchema (
779
783
collectionIDFieldTypes ,
780
- entity . flattenedFields ,
784
+ mutableFields ,
781
785
interfaceNameDefinitions ,
782
786
config ,
783
787
i18n ,
0 commit comments