Skip to content

Fix "Document HydratorFactory does not support nullable date field" #2753 #2755

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 4 commits into
base: 2.11.x
Choose a base branch
from

Conversation

frenchcomp
Copy link

Update code generated by the HydratorFactory for date to support nullable datetime field.

Q A
Type bug
BC Break no
Fixed issues #2753

Summary

These PR update only the HydratorFactory class to update the if condition to support nullable field like for other type.
It will update also the clone $return value, because $return can be 'null' and we can not clone null value in PHP.

Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Hello @frenchcomp, thanks for contributing this fix.
Apparently it's breaking an existing test case. Also, the feature needs to be covered by a new non-regression test.

1) Doctrine\ODM\MongoDB\Tests\Functional\DateTest::testDateInstanceValueChangeDoesCauseUpdateIfValueIsTheSame
Failed asserting that an array is not empty.

/home/runner/work/mongodb-odm/mongodb-odm/tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php:102

@@ -185,10 +185,13 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
<<<'EOF'

// Field(type: "date")
if (isset($data['%1$s'])) {
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) {
Copy link
Member

Choose a reason for hiding this comment

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

We avoid usage of empty as it covers too many cases. Maybe we can refactor the condition like this;

Suggested change
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) {
if (array_key_exists('%1$s', $data) && ($data['%1$s'] !== null || $this->class->fieldMappings['%2$s']['nullable'] ?? false)) {

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I understand, I had just copied another part of the factory. In an external project I try to maintain consistency.
I forgeted the flag WIP. I just wanted to run the tests, not being able to do it on my machine

Copy link
Member

@GromNaN GromNaN Apr 11, 2025

Choose a reason for hiding this comment

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

Indeed, I wasn't aware that empty is used a lot in this class.

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.

2 participants