Skip to content

Commit

Permalink
fix(client): Correct result for hasOwn/hasOwnProperty on proxies (#18519
Browse files Browse the repository at this point in the history
)

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
  • Loading branch information
SevInf committed Mar 27, 2023
1 parent 5b3ec51 commit ba6ea8e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Expand Up @@ -36,6 +36,11 @@ test('allows to add extra properties via layers', () => {
expect(proxy).toHaveProperty('second', 2)
})

test('preserves correct Object.prototype.hasOwnProperty result', () => {
const proxy = createCompositeProxy({}, [])
expect(Object.prototype.hasOwnProperty.call(proxy, 'notThere')).toBe(false)
})

test('allows to add multiple properties via single layer', () => {
const proxy = createCompositeProxy({}, [
{
Expand Down
@@ -1,4 +1,4 @@
import { type InspectOptions, inspect } from 'util'
import { inspect, type InspectOptions } from 'util'

import { defaultPropertyDescriptor } from '../model/utils/defaultProxyHandlers'

Expand Down Expand Up @@ -91,13 +91,17 @@ export function createCompositeProxy<T extends object>(target: T, layers: Compos

getOwnPropertyDescriptor(target, prop) {
const layer = keysToLayerMap.get(prop)
if (layer && layer.getPropertyDescriptor) {
return {
...defaultPropertyDescriptor,
...layer.getPropertyDescriptor(prop),
if (layer) {
if (layer.getPropertyDescriptor) {
return {
...defaultPropertyDescriptor,
...layer?.getPropertyDescriptor(prop),
}
}
return defaultPropertyDescriptor
}
return defaultPropertyDescriptor

return Reflect.getOwnPropertyDescriptor(target, prop)
},

defineProperty(target, property, attributes) {
Expand Down

0 comments on commit ba6ea8e

Please sign in to comment.