Skip to content
Prev Previous commit
Next Next commit
fix: relationships to trashable collections admit null in generated t…
…ypes
  • Loading branch information
KibbeWater committed Apr 28, 2026
commit 8750215183961ba7e3e04d90c0acf9226ad6e812
25 changes: 23 additions & 2 deletions packages/payload/src/utilities/configToJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,23 @@ export function withNullableJSONSchemaType(
return fieldTypes
}

/**
* Single relationships whose target collection has `trash: true` can be
* silently nulled at read time when the target document is trashed (see
* `relationshipPopulationPromise`). The generated type must reflect that
* even when the field is `required`.
*/
function isAnyRelationTargetTrashable(
relationTo: string | string[],
collections?: SanitizedCollectionConfig[],
): boolean {
if (!collections) {
return false
}
const targets = Array.isArray(relationTo) ? relationTo : [relationTo]
return targets.some((slug) => collections.find((c) => c.slug === slug)?.trash === true)
}

function entityOrFieldToJsDocs({
entity,
i18n,
Expand Down Expand Up @@ -669,11 +686,13 @@ export function fieldsToJSONSchema(
},
}
} else {
const isRequiredAndStable =
isRequired && !isAnyRelationTargetTrashable(field.relationTo, config?.collections)
fieldSchema = {
...baseFieldSchema,
oneOf: field.relationTo.map((relation) => {
return {
type: withNullableJSONSchemaType('object', isRequired),
type: withNullableJSONSchemaType('object', isRequiredAndStable),
additionalProperties: false,
properties: {
relationTo: {
Expand Down Expand Up @@ -711,13 +730,15 @@ export function fieldsToJSONSchema(
},
}
} else {
const isRequiredAndStable =
isRequired && !isAnyRelationTargetTrashable(field.relationTo, config?.collections)
fieldSchema = {
...baseFieldSchema,
oneOf: [
{
type: withNullableJSONSchemaType(
collectionIDFieldTypes[field.relationTo]!,
isRequired,
isRequiredAndStable,
),
},
{ $ref: `#/definitions/${field.relationTo}` },
Expand Down