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
27.66k stars 1.72k forks source link

UI crash in collection with JSON field when data is a JSON array #9111

Open rbruels opened 1 week ago

rbruels commented 1 week ago

Describe the Bug

The collection list view crashes if the collection has a JSON field which contains an array with greater than 100 items.

The error is:

TypeError: t.substring is not a function

Link to the code that reproduces this issue

https://github.com/rbruels/payload-json-issue

Reproduction Steps

Add json field

I just added this to the default Media collection:

fields: [
    {
      name: 'alt',
      type: 'text',
      required: true,
    - }
    + },
    + {
    +  name: 'data',
    +  type: 'json',
    +  required: true,
    + }
  ],

Insert data

Insert a new Media object and add an image/alt text and then in the new data field, add a json array exceeding 100 items. You can copy paste this if you're so inclined:

[{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "},{"test":"test "}]

Save the object.

Reload the list view

Go back to the list view for media (http://localhost:3000/admin/collections/media)

You'll immediately see an error:

Image

The problematic code is here:

https://github.com/payloadcms/payload/blob/beta/packages/ui/src/elements/Table/DefaultCell/fields/JSON/index.tsx#L11

In this case, cellData is a json array. So cellData.length > 100 is valid code ... but when the # of objects in the array exceeds 100, it calls cellData.substring which is not valid for the array.

Which area(s) are affected? (Select all that apply)

area: ui

Environment Info

Binaries:
  Node: 20.12.2
  npm: 10.5.0
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.0.0-beta.127
  next: 15.0.0
  @payloadcms/db-mongodb: 3.0.0-beta.127
  @payloadcms/email-nodemailer: 3.0.0-beta.127
  @payloadcms/graphql: 3.0.0-beta.127
  @payloadcms/next/utilities: 3.0.0-beta.127
  @payloadcms/payload-cloud: 3.0.0-beta.127
  @payloadcms/richtext-lexical: 3.0.0-beta.127
  @payloadcms/translations: 3.0.0-beta.127
  @payloadcms/ui/shared: 3.0.0-beta.127
  react: 19.0.0-rc-65a56d0e-20241020
  react-dom: 19.0.0-rc-65a56d0e-20241020
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.0.0: Tue Sep 24 23:37:25 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T6030
  Available memory (MB): 36864
  Available CPU cores: 12
kendelljoseph commented 1 week ago

Hi @rbruels ,

I've encountered this same error when using just an empty array as a JSON field value. [].
I think it may be an admin visual parsing error that happens when the field contains an Array.

I'll see if this is connected.

rbruels commented 1 week ago

Hi @kendelljoseph — it definitely occurs when the value is a JSON array with multiple elements. We discovered it because our JSON field had an array with >100 elements. See my diagnosis above:

The problematic code is here:

https://github.com/payloadcms/payload/blob/beta/packages/ui/src/elements/Table/DefaultCell/fields/JSON/index.tsx#L11

In this case, cellData is a json array. So cellData.length > 100 is valid code ... but when the # of objects in the array exceeds 100, it calls cellData.substring which is not valid for the array.
kendelljoseph commented 1 week ago

Yep. I have reproduced it on my end. They are connected. The cellData can also be an array, and arrays don't have the substring property.

I should have a fix for this ready soon!

rbruels commented 1 week ago

Rad!! Now we just need work on migrating to 3.0 😅 Thanks @kendelljoseph