]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): mutating a readonly ref nested in a reactive object should fail...
authorThorsten Lünborg <t.luenborg@googlemail.com>
Fri, 21 Jan 2022 07:33:18 +0000 (08:33 +0100)
committerGitHub <noreply@github.com>
Fri, 21 Jan 2022 07:33:18 +0000 (02:33 -0500)
fix: #5042

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

index 1a7df0804e08be4a28d3d03d56211de629762740..72a873a016ee94790c2bd0d304bddc734b388e4d 100644 (file)
@@ -474,4 +474,15 @@ describe('reactivity/readonly', () => {
     expect(rr.foo).toBe(r)
     expect(isReadonly(rr.foo)).toBe(true)
   })
+
+  test('attemptingt to write to a readonly ref nested in a reactive object should fail', () => {
+    const r = ref(false)
+    const ror = readonly(r)
+    const obj = reactive({ ror })
+    try {
+      obj.ror = true
+    } catch (e) {}
+
+    expect(obj.ror).toBe(false)
+  })
 })
index 97acf063f67aaab5ba348964763b62df1e3442bf..eed9dc8509f7c7cbbb688db66c865165d80aa317 100644 (file)
@@ -150,6 +150,9 @@ function createSetter(shallow = false) {
     receiver: object
   ): boolean {
     let oldValue = (target as any)[key]
+    if (isReadonly(oldValue) && isRef(oldValue)) {
+      return false
+    }
     if (!shallow && !isReadonly(value)) {
       if (!isShallow(value)) {
         value = toRaw(value)