Skip to content

Commit fd6d969

Browse files
octoperposva
andauthored
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 <posva13@gmail.com>
1 parent d8ea789 commit fd6d969

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/pinia/__tests__/ssr.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @vitest-environment node
33
*/
44
import { describe, it, expect } from 'vitest'
5-
import { createPinia, defineStore } from '../src'
5+
import { createPinia, defineStore, shouldHydrate } from '../src'
66
import { Component, createSSRApp, inject, ref, computed, customRef } from 'vue'
77
import { renderToString, ssrInterpolate } from '@vue/server-renderer'
88
import { useUserStore } from './pinia/stores/user'
@@ -152,6 +152,16 @@ describe('SSR', () => {
152152
expect(store.$state).toEqual({ start: 'start' })
153153
})
154154

155+
// NOTE: added to make things easier but people should avoid creating objects
156+
// with `Object.create(null)` and store them in pinia stores
157+
// https://github.com/vuejs/pinia/issues/2837
158+
it('can hydrate objects without a a null prototype chain', () => {
159+
const obj = Object.create(null)
160+
expect(() => {
161+
shouldHydrate(obj)
162+
}).not.toThrow()
163+
})
164+
155165
describe('Setup Store', () => {
156166
const useStore = defineStore('main', () => {
157167
const count = ref(0)

packages/pinia/src/store.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ export function skipHydrate<T = any>(obj: T): T {
133133
* @returns true if `obj` should be hydrated
134134
*/
135135
export function shouldHydrate(obj: any) {
136-
return !isPlainObject(obj) || !obj.hasOwnProperty(skipHydrateSymbol)
136+
return (
137+
!isPlainObject(obj) ||
138+
!Object.prototype.hasOwnProperty.call(obj, skipHydrateSymbol)
139+
)
137140
}
138141

139142
const { assign } = Object

0 commit comments

Comments
 (0)