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

Relationship filterOptions id in [null] exception #6010

Open
franciscolourenco opened this issue Apr 24, 2024 · 0 comments
Open

Relationship filterOptions id in [null] exception #6010

franciscolourenco opened this issue Apr 24, 2024 · 0 comments
Labels
[possible-bug] Possible bug which hasn't been reproduced yet

Comments

@franciscolourenco
Copy link
Contributor

franciscolourenco commented Apr 24, 2024

Link to reproduction

main...franciscolourenco:payload:reproduce-relationship-null

Describe the Bug

I came across this issue when using a relationship field while inside of a new document drawer. It happens because the document id passed in filterOptions({id}) is null when the field is inside of a new document drawer. Normally id is undefined when inside of a normal new document creation page. There are a few issues relating to this:

1. FilterOptionProps is incorrectly typed

FilterOptionsProps.id is currently typed as id: number | string. However the reality is that it can also be undefined (when inside of a normal document creation page) or null (when inside a document creation drawer): id?: number | string | null

2. Mismatch between the local API and the REST API. The REST API finds documents when it should find none in some situations

    payload.find
      ✓ should find 1 doc (4 ms)
      ✓ should find 0 docs with id equals null (4 ms)
      ✓ should find 0 docs with id equals undefined (3 ms)
      ✓ should find 0 docs with id in [] (4 ms)
      ✓ should find 0 docs with id in [null] (3 ms)
      ✓ should find 0 docs with id in [undefined] (4 ms)
      ✓ payload.find with id in [] (3 ms)
    REST API
      ✓ should find 1 doc (9 ms)
      ✓ should find 0 docs with id equals null (11 ms)
->    ✕ should find 0 docs with {id: {equals: undefined}} (9 ms)
->    ✕ should find 0 docs with id in [] (8 ms)
->    ✕ should find 0 docs with id in [undefined] (9 ms)
      ✕ should find 0 docs with id in [null] (9 ms)

3. Payload throws an exception when the where query is {id: {in: [null]}}

    payload.find
      ✓ should find 1 doc (4 ms)
      ✓ should find 0 docs with id equals null (4 ms)
      ✓ should find 0 docs with id equals undefined (3 ms)
      ✓ should find 0 docs with id in [] (4 ms)
      ✓ should find 0 docs with id in [null] (3 ms)
      ✓ should find 0 docs with id in [undefined] (4 ms)
      ✓ payload.find with id in [] (3 ms)
    REST API
      ✓ should find 1 doc (9 ms)
      ✓ should find 0 docs with id equals null (11 ms)
      ✕ should find 0 docs with {id: {equals: undefined}} (9 ms)
      ✕ should find 0 docs with id in [] (8 ms)
      ✕ should find 0 docs with id in [undefined] (9 ms)
->    ✕ should find 0 docs with id in [null] (9 ms)
[20:20:37] ERROR (payload): CastError: Cast to ObjectId failed for value "" (type string) at path "_id" for model "posts"
    at model.Query.exec (/Users/user/code/amboss/payload/node_modules/.pnpm/mongoose@6.12.3/node_modules/mongoose/lib/query.js:4921:21)
    at Function.paginate (/Users/user/code/amboss/payload/node_modules/.pnpm/mongoose-paginate-v2@1.7.22/node_modules/mongoose-paginate-v2/dist/index.js:147:53)
    at Object.find (/Users/user/code/amboss/payload/packages/db-mongodb/src/find.ts:84:30)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async find (/Users/user/code/amboss/payload/packages/payload/src/collections/operations/find.ts:145:16)
    at async findHandler (/Users/user/code/amboss/payload/packages/payload/src/collections/requestHandlers/find.ts:30:20)

4. The previous issue can cause the relationship field to error when used inside a new document drawer

Screen.Recording.2024-04-24.at.20.26.08.mov

To Reproduce

Check the tests in the reproduction links and also the screenrecording:

Screen.Recording.2024-04-24.at.20.26.08.mov

Payload Version

2.13.0

Adapters and Plugins

No response

@franciscolourenco franciscolourenco added the [possible-bug] Possible bug which hasn't been reproduced yet label Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[possible-bug] Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

1 participant