Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Serializer] Handle type error constructing input#51003

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
kylekatarnls wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromkylekatarnls:patch-5

Conversation

kylekatarnls
Copy link
Contributor

@kylekatarnlskylekatarnls commentedJul 17, 2023
edited
Loading

QA
Branch?6.4
Bug fix?no
New feature?yes
Deprecations?no
Issues?Fix#50904
LicenseMIT

When mapping a input to an object, for instance with:

publicfunctionmyApiRoute(#[MapQueryString]MyInput$input){}

AndMyInput has an hard-type:

class MyInput{publicfunction__construct(publicreadonly ?string$id =null,    ) {    }}

Then you try to call this route with invalid type:/api/endpoint?id[]=abc

The construction ofMyInput will fail with aTypeErrorMyInput::__construct(): Argument #1 ($id) must be of type ?string, array given.

But this is caught inAbstractNormalizer then you get a 500 error (unhandled)Typed property MyInput::id must not be accessed before initialization.

With this PR, I propose to handle it instead as a 400 error (bad request)The type of the "id" parameter for class "MyInput" must be of type "?string" ("array" given).

@carsonbotcarsonbot added this to the6.4 milestoneJul 17, 2023
@kylekatarnlskylekatarnls changed the title[Serialize] Handle type error constructing input[Serializer] Handle type error constructing inputJul 17, 2023
Comment on lines +430 to +447
if (preg_match('/#\d+ \(\$([^)]+)\) must be of type ([^,]+), ([^,]+) given/', $message, $match)) {
$message = sprintf(
'The type of the "%s" parameter for class "%s" must be of type "%s" ("%s" given).',
$match[1],
$class,
$match[2],
$match[3],
);
}

$exception = NotNormalizableValueException::createForUnexpectedDataType(
$message,
$data,
[$class],
$context['deserialization_path'] ?? null,
true,
previous: $e,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Perhaps it would be better this way

Suggested change
if (preg_match('/#\d+ \(\$([^)]+)\) must be of type ([^,]+), ([^,]+) given/',$message,$match)) {
$message = sprintf(
'The type of the "%s" parameter for class "%s" must be of type "%s" ("%s" given).',
$match[1],
$class,
$match[2],
$match[3],
);
}
$exception = NotNormalizableValueException::createForUnexpectedDataType(
$message,
$data,
[$class],
$context['deserialization_path'] ??null,
true,
previous:$e,
);
$type =$class;
if (preg_match('/#\d+ \(\$([^)]+)\) must be of type ([^,]+), ([^,]+) given/',$message,$match)) {
$message = sprintf(
'The type of the "%s" parameter for class "%s" must be of type "%s" ("%s" given).',
$match[1],
$class,
$match[2],
$match[3],
);
$type =$match[2];
}
$exception = NotNormalizableValueException::createForUnexpectedDataType(
$message,
$data,
[$type],
$context['deserialization_path'] ??null,
true,
previous:$e,
);

@nicolas-grekasnicolas-grekas modified the milestones:6.4,7.1Nov 15, 2023
@xabbuhxabbuh modified the milestones:7.1,7.2May 15, 2024
@fabpotfabpot modified the milestones:7.2,7.3Nov 20, 2024
@fabpotfabpot modified the milestones:7.3,7.4May 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@Victor-TruhanovichVictor-TruhanovichVictor-Truhanovich left review comments

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

MapQueryString & MapRequestPayload empty when type missmatch
6 participants
@kylekatarnls@Victor-Truhanovich@fabpot@nicolas-grekas@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp