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

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

Open
rbruels opened this issue Nov 11, 2024 · 4 comments
Open

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

rbruels opened this issue Nov 11, 2024 · 4 comments
Assignees
Labels

Comments

@rbruels
Copy link

rbruels commented Nov 11, 2024

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
@rbruels rbruels added status: needs-triage Possible bug which hasn't been reproduced yet v3 validate-reproduction labels Nov 11, 2024
@rbruels rbruels changed the title UI crash in JSON field when data is a JSON array UI crash in collection with JSON field when data is a JSON array Nov 11, 2024
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Nov 11, 2024
@kendelljoseph
Copy link
Contributor

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
Copy link
Author

rbruels commented Nov 14, 2024

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
Copy link
Contributor

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
Copy link
Author

rbruels commented Nov 14, 2024

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

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

No branches or pull requests

2 participants