]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix: shouldHydrate error when null object is passed (#2867)
authorVaggelis Yfantis <me@octoper.me>
Wed, 9 Apr 2025 14:17:20 +0000 (17:17 +0300)
committerGitHub <noreply@github.com>
Wed, 9 Apr 2025 14:17:20 +0000 (16:17 +0200)
* fix: shouldHydrate error when null object is passed

* test(nuxt): Add tests

* ci: Trigger CI

* refactor: simplify

---------

Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
packages/pinia/__tests__/ssr.spec.ts
packages/pinia/src/store.ts

index 5f5a364d8156451c6d2ab1bb1adefc5eb1e4cccb..f9c80778fd85e6bfbf3d0202fc8ea4f5549b1eb1 100644 (file)
@@ -2,7 +2,7 @@
  * @vitest-environment node
  */
 import { describe, it, expect } from 'vitest'
-import { createPinia, defineStore } from '../src'
+import { createPinia, defineStore, shouldHydrate } from '../src'
 import { Component, createSSRApp, inject, ref, computed, customRef } from 'vue'
 import { renderToString, ssrInterpolate } from '@vue/server-renderer'
 import { useUserStore } from './pinia/stores/user'
@@ -152,6 +152,16 @@ describe('SSR', () => {
     expect(store.$state).toEqual({ start: 'start' })
   })
 
+  // NOTE: added to make things easier but people should avoid creating objects
+  // with `Object.create(null)` and store them in pinia stores
+  // https://github.com/vuejs/pinia/issues/2837
+  it('can hydrate objects without a a null prototype chain', () => {
+    const obj = Object.create(null)
+    expect(() => {
+      shouldHydrate(obj)
+    }).not.toThrow()
+  })
+
   describe('Setup Store', () => {
     const useStore = defineStore('main', () => {
       const count = ref(0)
index bed44b203841564e87633e681fd2426e58bcb187..4d10c690d532f1a4ee7fffd0b39ef55c41862d1a 100644 (file)
@@ -133,7 +133,10 @@ export function skipHydrate<T = any>(obj: T): T {
  * @returns true if `obj` should be hydrated
  */
 export function shouldHydrate(obj: any) {
-  return !isPlainObject(obj) || !obj.hasOwnProperty(skipHydrateSymbol)
+  return (
+    !isPlainObject(obj) ||
+    !Object.prototype.hasOwnProperty.call(obj, skipHydrateSymbol)
+  )
 }
 
 const { assign } = Object