From: Vaggelis Yfantis Date: Wed, 9 Apr 2025 14:17:20 +0000 (+0300) Subject: fix: shouldHydrate error when null object is passed (#2867) X-Git-Tag: @pinia/nuxt@0.11.0~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fd6d9698e1b9a1bd63949dabae7ec488b33358e8;p=thirdparty%2Fvuejs%2Fpinia.git fix: shouldHydrate error when null object is passed (#2867) * fix: shouldHydrate error when null object is passed * test(nuxt): Add tests * ci: Trigger CI * refactor: simplify --------- Co-authored-by: Eduardo San Martin Morote --- diff --git a/packages/pinia/__tests__/ssr.spec.ts b/packages/pinia/__tests__/ssr.spec.ts index 5f5a364d..f9c80778 100644 --- a/packages/pinia/__tests__/ssr.spec.ts +++ b/packages/pinia/__tests__/ssr.spec.ts @@ -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) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index bed44b20..4d10c690 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -133,7 +133,10 @@ export function skipHydrate(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