payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.25k stars 1.47k forks source link

nested-docs-plugin error if child has invalid forward draft #7694

Closed yobottehg closed 2 weeks ago

yobottehg commented 4 weeks ago

Link to reproduction

Reproduction

Payload Version

3.0.0-beta.68

Node Version

20 LTS

Next.js Version

15.0.0-carary.57

Describe the Bug

When using the nested docs plugin and a parent document is published it tries to resave it's children.

If the children have invalid drafts the plugin fails to do so resulting in a lot of error messages and possible deadlocks (I have both in the logs.)

[12:13:41] ERROR: Nested Docs plugin has had an error while re-saving a child document as published.
[12:13:41] ERROR: The following field is invalid: content.2.consultant
    err: {
      "type": "l",
      "message": "The following field is invalid: content.2.consultant",
      "stack":
          l: The following field is invalid: content.2.consultant
              ...
      "data": {
        "collection": "pages",
        "errors": [
          {
            "field": "content.2.consultant",
            "message": "This field is required."
          }
        ]
      },
      "isOperational": true,
      "isPublic": false,
      "status": 400,
      "name": "l"
    }

Shouldn't the nested-docs-plugin just re-publish the latest published version of the children instead with the new url instead of creating new published version of their drafts?

Reproduction Steps

  1. Add 2 pages which use the nested docs plugin.
  2. Make one a parent and one a child.
  3. Now create an unpublished draft version of the child.
  4. Go to the parent and change it's slug.
  5. See an error as the child url can not be updated.

Adapters and Plugins

nested-docs, db-postgres

paulpopus commented 4 weeks ago

Hey @yobottehg I tested a few workflows here, on the latest beta and next canary 104+, with publishing and draft modes and in both mongo and postgres and I can't reproduce this.

Could you provide a reproduction please? Or more detailed steps?

yobottehg commented 4 weeks ago

@paulpopus

I found an easy way to reproduce this also using the demo:

  1. create page 1 with slug "parent"
  2. create page 2 with slug "child" and add an image (but any reference will do)
  3. delete the image via the image collection
  4. go to page 1 and change the slug

I think this goes more into the direction on how to handle deleted references instead of being only a nested docs problem.

paulpopus commented 3 weeks ago

Thanks @yobottehg I followed those exact steps in both mongo and postgres on the latest beta and I couldn't get this to error, the referenced category is removed as expected in postgres

Can you test on the latest and let me know if you can still reproduce? Is there anything else related to your setup, eg hooks etc. that run?

yobottehg commented 3 weeks ago

@paulpopus I added a reproduction. Only tested locally with postgres.

My steps to reproduce were wrong. Issue only appears if the deleted reference is inside a block.

I added all necessary steps to reproduce to the onInit function here: https://github.com/yobottehg/payload/blob/beta-repro-7694/test/_community/config.ts#L26

tiago-meireles commented 3 weeks ago

Following this since I'm also dealing with this same error

paulpopus commented 2 weeks ago

Will be fixed in the next release via https://github.com/payloadcms/payload/pull/7977

We will throw an error to the UI explaining that the children are no longer passing validation.

In the future we'll look into how deleting references in this manner affects validation of where it's used.

github-actions[bot] commented 1 week ago

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.