Skip to content

Keep null for nullable scalar fields in weak mode#111

Merged
Crell merged 2 commits into
Crell:masterfrom
SAY-5:fix-weak-mode-nullable-scalars
Jun 16, 2026
Merged

Keep null for nullable scalar fields in weak mode#111
Crell merged 2 commits 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
Contributor

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>
Comment thread tests/ArrayFormatterTest.php Outdated
@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.

@SAY-5

SAY-5 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

Good call. Moved it into round_trip_examples() so it runs across all formatters, and dropped the standalone test from ArrayFormatterTest.

@Crell Crell merged commit b5acd92 into Crell:master Jun 16, 2026
6 checks passed
@Crell

Crell commented Jun 16, 2026

Copy link
Copy Markdown
Owner

Thanks! I'm going to tag a new release soonish.

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