Skip to content

Keep null for nullable scalar fields in weak mode#111

Open
SAY-5 wants to merge 1 commit into
Crell:masterfrom
SAY-5:fix-weak-mode-nullable-scalars
Open

Keep null for nullable scalar fields in weak mode#111
SAY-5 wants to merge 1 commit into
Crell:masterfrom
SAY-5:fix-weak-mode-nullable-scalars

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jun 9, 2026

Copy link
Copy Markdown

Description

The weak-mode (#[Field(strict: false)]) branches of deserializeInt, deserializeFloat, deserializeBool and deserializeString cast the value unconditionally, so a null on a nullable field is coerced to 0, 0.0, false or "". 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 null into a ?int (etc.) marked strict: false should keep the value null, not turn it into 0. The return types already permit null, so only the cast was wrong. fixes #110

How has this been tested?

composer test passes (539 tests). Added non_strict_null_stays_null_on_nullable_fields in ArrayFormatterTest, which fails on the current code (0 is not null) and passes with the guard in place. PHPStan reports no new errors.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have read the CONTRIBUTING document.
  • My pull request addresses exactly one patch/feature.
  • I have created a branch for this patch/feature.
  • Each individual commit in the pull request is meaningful.
  • I have added tests to cover my changes.
  • If my change requires a change to the documentation, I have updated it accordingly.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
}

#[Test]
public function non_strict_null_stays_null_on_nullable_fields(): void

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

@Crell

Crell commented Jun 13, 2026

Copy link
Copy Markdown
Owner

Aside from the test being in the wrong place, this looks like a good fix. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weak-mode scalar deserializers cast null to 0/0.0/false/"" on nullable fields

2 participants