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

Objects with computed properties returned from an extended prisma client return true for any property name when calling hasOwn or hasOwnProperty on it #18462

Closed
ludwigbacklund opened this issue Mar 23, 2023 · 2 comments · Fixed by #18519
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: clientExtensions
Milestone

Comments

@ludwigbacklund
Copy link

ludwigbacklund commented Mar 23, 2023

Bug description

Objects with computed properties returned from an extended prisma client return true for any property name when calling .hasOwn or .hasOwnProperty on it.

Spreading the returned object into a new object and then running .hasOwn or .hasOwnProperty again with any property name works as expected, returning false for names that don't exist in the object.

Does this have something to do with the returned object being a proxy when it has a computed property from $extends on it?

How to reproduce

  1. Git clone this repro repo: https://github.com/ludwigbacklund/prisma-hasown-bug-repro (it is based on your rest-nextjs-api-routes example)
  2. Create a draft post
  3. Go to the index page and check the server console

OR

Run the following code (replace post with any prisma model)

const testExtension = Prisma.defineExtension((client) => {
  return client.$extends({
    result: { post: { computed: { needs: {}, compute: () => 'whatever' } } },
  });
});

const prisma = new PrismaClient({
  datasources: { db: { url: 'SOME_DB_URL' } },
}).$extends(testExtension);

const post = await prisma.post.findFirst();
console.log(Object.hasOwn(post, 'this should not return true but it does')); // -> true
console.log(post.hasOwnProperty('this should not return true but it does')); // -> true

Expected behavior

.hasOwn and .hasOwnProperty should not always return true, only when the property name actually exists on the returned object.

Prisma information

model Post {
  id     String @id @default(cuid())
  title  String
  text   String
  userId String @map("user_id")

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @default(now()) @updatedAt @map("updated_at")

  @@map("posts")
}

Environment & setup

  • OS: macOS Ventura 13.0
  • Database: PostgreSQL 14
  • Node.js version: 18.11.0

Prisma Version

5.11.0
@ludwigbacklund ludwigbacklund added the kind/bug A reported bug. label Mar 23, 2023
@ludwigbacklund
Copy link
Author

Tried reproducing in a fresh prisma example repo but cannot... not sure what is going on here.

@ludwigbacklund ludwigbacklund changed the title Objects returned from findFirst return true for any property name when calling hasOwn or hasOwnProperty on it Objects returned from an extended prisma client return true for any property name when calling hasOwn or hasOwnProperty on it Mar 23, 2023
@ludwigbacklund ludwigbacklund changed the title Objects returned from an extended prisma client return true for any property name when calling hasOwn or hasOwnProperty on it Objects with computed properties returned from an extended prisma client return true for any property name when calling hasOwn or hasOwnProperty on it Mar 23, 2023
@ludwigbacklund
Copy link
Author

ludwigbacklund commented Mar 23, 2023

Okay I narrowed it down to client extensions adding computed properties to an object makes it act this way. It's all yours prisma team! 🙏

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: clientExtensions team/client Issue for team Client. labels Mar 24, 2023
@SevInf SevInf added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Mar 27, 2023
@SevInf SevInf self-assigned this Mar 27, 2023
SevInf added a commit that referenced this issue Mar 27, 2023
Problem is in compositeProxy implamentation: we always returned a
descriptor from `getOwnPropertyDescriptor`, which caused `hasOwnProperty`
to always return `true` regardless of propery existence.

Fix #18462
SevInf added a commit that referenced this issue Mar 27, 2023
)

Problem is in compositeProxy implamentation: we always returned a
descriptor from `getOwnPropertyDescriptor`, which caused `hasOwnProperty`
to always return `true` regardless of propery existence.

Fix #18462
@janpio janpio added this to the 4.12.0 milestone Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: clientExtensions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants