]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): retain readonly proxies when setting as reactive property
authorEvan You <yyx990803@gmail.com>
Thu, 25 Nov 2021 04:14:20 +0000 (12:14 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 25 Nov 2021 04:14:39 +0000 (12:14 +0800)
fix #4986

packages/reactivity/__tests__/readonly.spec.ts
packages/reactivity/src/baseHandlers.ts

index 63426bb5e9c1a701aacac175973a1ca84c50571c..185bb31075980919b9133dfc837686f0d585f03d 100644 (file)
@@ -465,4 +465,13 @@ describe('reactivity/readonly', () => {
       'Set operation on key "randomProperty" failed: target is readonly.'
     ).toHaveBeenWarned()
   })
+
+  // #4986
+  test('setting a readonly object as a property of a reactive object should retain readonly proxy', () => {
+    const r = readonly({})
+    const rr = reactive({}) as any
+    rr.foo = r
+    expect(rr.foo).toBe(r)
+    expect(isReadonly(rr.foo)).toBe(true)
+  })
 })
index 29367007c1381bded8f8ca97794090d785171ac5..0fa1c4bc2ea541b8ff20b117b20017e07b57c53e 100644 (file)
@@ -7,7 +7,8 @@ import {
   readonlyMap,
   reactiveMap,
   shallowReactiveMap,
-  shallowReadonlyMap
+  shallowReadonlyMap,
+  isReadonly
 } from './reactive'
 import { TrackOpTypes, TriggerOpTypes } from './operations'
 import {
@@ -146,7 +147,7 @@ function createSetter(shallow = false) {
     receiver: object
   ): boolean {
     let oldValue = (target as any)[key]
-    if (!shallow) {
+    if (!shallow && !isReadonly(value)) {
       value = toRaw(value)
       oldValue = toRaw(oldValue)
       if (!isArray(target) && isRef(oldValue) && !isRef(value)) {