Skip to content

fix: polymorphic relationship returns null when target is trashed#16403

Open
KibbeWater wants to merge 5 commits intopayloadcms:mainfrom
KibbeWater:fix/polymorphic-rel-trash-single
Open

fix: polymorphic relationship returns null when target is trashed#16403
KibbeWater wants to merge 5 commits intopayloadcms:mainfrom
KibbeWater:fix/polymorphic-rel-trash-single

Conversation

@KibbeWater
Copy link
Copy Markdown

What?

Polymorphic single relationships to a trashed document return null instead of { relationTo, value: null }. Single relationships to a trash: true collection are also nullable in generated types regardless of
required.

Why?

The runtime emitted a shape the types said was impossible. typeof reference.value === 'object' && reference.value.slug typechecked but crashed on trashed targets — typeof null === 'object'.

#16102 set "for single relationships, the field is set to null" but implemented it for non-polymorphic only. Polymorphic single (and localized polymorphic single) were missed. The schema widening covers required: true rels to trashable collections, where the runtime nulls the field but the type used to forbid it.

How?

  • Runtime (relationshipPopulationPromise.ts): null the whole wrapper, not just wrapper.value, when the populated value is null. Covers single polymorphic and localized polymorphic single. Polymorphic
    hasMany was already filtered.
  • Schema (configToJSONSchema.ts): widen single rels to trash: true targets to T | null regardless of required. payload-locked-documents.user and payload-preferences.user widen automatically.
  • Templates / examples: truthy guard on reference.value in CMSLink (3 templates, 2 examples).
  • Tests: three polymorphic fields on Pages; five integration tests; payload-types.ts regenerated.
@KibbeWater
Copy link
Copy Markdown
Author

@denolfe This PR targets at main but would likely benefit being added to 3.x as well. Would you like me to open a separate PR and just cherry-pick the commits over?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant