Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate streamfield block duplicates UUID #7712

Closed
fabienheureux opened this issue Nov 16, 2021 · 3 comments · Fixed by #9108
Closed

Duplicate streamfield block duplicates UUID #7712

fabienheureux opened this issue Nov 16, 2021 · 3 comments · Fixed by #9108

Comments

@fabienheureux
Copy link
Member

fabienheureux commented Nov 16, 2021

Issue Summary

I noticed streamfield's blocks uuid can be not unique.

It can be an issue, in my case in a headless setup, I cache blocks based on their UUIDs.

Steps to Reproduce

For example, let's consider a streamfield, containing a structblock, containing a streamblock.
Duplicating an instance of this structblock, with existing streamblocks, cause the nested streamblock UUIDs to remain identical to the original ones

I created an example repo here https://github.com/fabienheureux/wagtail-duplicate-streamfield-uuid

  1. git clone https://github.com/fabienheureux/wagtail-duplicate-streamfield-uuid
  2. ./manage.py runserver
  3. ./manage.py shell
  4. from home.models import HomePage
  5. print(HomePage.objects.first().blocks.raw_data)
[
   {
      "type":"simple_question",
      "value":{
         "extended_answer":{
            "body":[
               {
                  "type":"text",
                  "value":"<p data-block-key=\"a7uom\">Test 1 2 1 2</p>",
                  "id":"ff7f1a7a-42a6-46c9-915c-5ca453b23698" 
               }
            ]
         }
      },
      "id":"520971d8-3d49-4779-b833-46222fdd48e1"
   },
   {
      "type":"simple_question",
      "value":{
         "extended_answer":{
            "body":[
               {
                  "type":"text",
                  "value":"<p data-block-key=\"a7uom\">Coucou</p>",
                  "id":"ff7f1a7a-42a6-46c9-915c-5ca453b23698"
               }
            ]
         }
      },
      "id":"ec72605c-b58a-4d57-9ead-2af3196575b4"
   }
]

➡️ The body's UUIDs are the same

How to reproduce from Wagtail admin

  1. Create a Streamfield block
  2. Fill some data for the StreamBlock inside this block
  3. Duplicate the Streamfield block

Technical details

  • Python version: any version
  • Django version: 3.2.9
  • Wagtail version: 2.15.1
@fabienheureux
Copy link
Member Author

fabienheureux commented Nov 17, 2021

To add to this, I tried to debug a bit and I placed a console.log in this file: https://github.com/wagtail/wagtail/blob/main/client/src/components/StreamField/blocks/StreamBlock.js#L294
Just after line 294

It appears that the id of a block is removed when duplicating but not its nested blocks' ids

// console.log(childState)
{
  "type": "simple_question",
  "value": {
    "extended_answer": {
      "body": [
        {
          "type": "text",
          "value": "{\"blocks\":[{\"key\":\"kpdv3\",\"text\":\"coucou\",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[],\"data\":{}}],\"entityMap\":{}}",
          "id": "f076404d-f25a-4199-9aab-a668151fae31"
        }
      ]
    }
  },
  "id": null
}

@fabienheureux
Copy link
Member Author

I tried to fix this @gasman but I am very new to StreamField JavaScript's architecture.
https://github.com/wagtail/wagtail/pull/7713/files

From my point of view, it seems to work (ids are properly removed, and then added server side).
However, I might lack some knowledge to know where exactly do this change.

Let me know if you are happy with these changes, I will cleanup the PR and write a few tests.
Thanks !

@kbayliss
Copy link
Collaborator

Just a note that this is still an issue in wagtail==3.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants