Keep null for nullable scalar fields in weak mode#111
Open
SAY-5 wants to merge 1 commit into
Open
Conversation
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
Crell
reviewed
Jun 13, 2026
| } | ||
|
|
||
| #[Test] | ||
| public function non_strict_null_stays_null_on_nullable_fields(): void |
Owner
There was a problem hiding this comment.
Why is this test here, rather than in SerdeTestCases? This should be tested on all formatters.
It should just be another case in round_trip_examples(), so it is automatically tested everywhere.
Owner
|
Aside from the test being in the wrong place, this looks like a good fix. Thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The weak-mode (
#[Field(strict: false)]) branches ofdeserializeInt,deserializeFloat,deserializeBoolanddeserializeStringcast the value unconditionally, so anullon a nullable field is coerced to0,0.0,falseor"". The strict branches already special-case$field->nullable && is_null($value), so I added the same guard before each weak-mode cast.Motivation and context
Deserializing
nullinto a?int(etc.) markedstrict: falseshould keep the valuenull, not turn it into0. The return types already permitnull, so only the cast was wrong. fixes #110How has this been tested?
composer testpasses (539 tests). Addednon_strict_null_stays_null_on_nullable_fieldsinArrayFormatterTest, which fails on the current code (0is not null) and passes with the guard in place. PHPStan reports no new errors.Types of changes
Checklist: