Skip to content

fix(runtime-core): avoid symbol coercion during props validation #8539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ describe('component props', () => {
props: {
bool: { type: Boolean },
str: { type: String },
symStr: { type: String },
sym: { type: Symbol },
num: { type: Number },
arr: { type: Array },
obj: { type: Object },
Expand All @@ -346,6 +348,8 @@ describe('component props', () => {
h(Comp, {
bool: 'true',
str: 100,
symStr: Symbol(),
sym: 'symbol',
num: '100',
arr: {},
obj: 'false',
Expand All @@ -361,6 +365,12 @@ describe('component props', () => {
expect(
`Invalid prop: type check failed for prop "str". Expected String with value "100", got Number with value 100.`
).toHaveBeenWarned()
expect(
`Invalid prop: type check failed for prop "symStr". Expected String, got Symbol`
).toHaveBeenWarned()
expect(
`Invalid prop: type check failed for prop "sym". Expected Symbol, got String with value "symbol".`
).toHaveBeenWarned()
expect(
`Invalid prop: type check failed for prop "num". Expected Number with value 100, got String with value "100".`
).toHaveBeenWarned()
Expand Down
14 changes: 10 additions & 4 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
hyphenate,
capitalize,
isString,
isSymbol,
isFunction,
isArray,
isObject,
Expand Down Expand Up @@ -736,7 +737,7 @@ function getInvalidTypeMessage(
if (
expectedTypes.length === 1 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)
isCoercible(expectedType, receivedType)
) {
message += ` with value ${expectedValue}`
}
Expand All @@ -752,7 +753,9 @@ function getInvalidTypeMessage(
* dev only
*/
function styleValue(value: unknown, type: string): string {
if (type === 'String') {
if (isSymbol(value)) {
return value.toString()
} else if (type === 'String') {
return `"${value}"`
} else if (type === 'Number') {
return `${Number(value)}`
Expand All @@ -772,6 +775,9 @@ function isExplicable(type: string): boolean {
/**
* dev only
*/
function isBoolean(...args: string[]): boolean {
return args.some(elem => elem.toLowerCase() === 'boolean')
function isCoercible(...args: string[]): boolean {
return args.every(elem => {
const value = elem.toLowerCase()
return value !== 'boolean' && value !== 'symbol'
})
}