Skip to content

marshaling weirdness#19157

Draft
LordSimal wants to merge 1 commit into6.xfrom
6.x-marshaling-weirdness
Draft

marshaling weirdness#19157
LordSimal wants to merge 1 commit into6.xfrom
6.x-marshaling-weirdness

Conversation

@LordSimal
Copy link
Contributor

This was caused by the discussion in #19153

TLDR: I think its weird, that we fallback to null for unknown values on certain/most of our Database Types in the marshaling process. This PR tries to illustrate the weird behavior currently present in our marshaling process.

The Problem

Under certain (maybe uncommon) circumstances, inconsistent behavior is present when trying to marshal data (from a request or "manually" built data passed down the entity).

Our current default behavior is, that we default to null for unknown - not parsable values in the marshaling process which imho can cause weird edgecases as shown in the test added in this PR. (This is of course just one DB type of many, where similar behavior is present)

As can be seen in the attached test I was only able to reproduce this when having no validation present for that field in the table validator, but maybe there are other cases also present which I haven't found yet.

Proposed solution

I'd argue we need to make DB type marshaling more "strict" and only allow passable/valid/correct data to be passed through and not default to null if its not valid.

This would also of course enforce users to actually pass "null valid values" if they want null (usually its null or '')

All other non-valid values being passed down to a field which don't fit (e.g. an array for a string field) would cause the mentioned exception which helps the user find the problem more easily and now wonder, why there is null in their column all of a sudden.

@LordSimal LordSimal force-pushed the 6.x-marshaling-weirdness branch from e7d125d to d316f86 Compare December 27, 2025 09:27
@dereuromark dereuromark added this to the 6.0 milestone Dec 27, 2025
Comment on lines +234 to +235
$this->assertInstanceOf(EntityInterface::class, $newEntity);
$this->assertEquals('invalid', $newEntity->floaty); // which is weird but ok
Copy link
Member

Choose a reason for hiding this comment

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

It would be better if the entity reflected the state of the database after the save is complete. That would require us to reload the entity though.

Copy link
Contributor Author

@LordSimal LordSimal Dec 30, 2025

Choose a reason for hiding this comment

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

That would require us to reload the entity though.

Wouldn't it be enough if we set the value from the toDatabase() method of the used DB Type into the entity field back?

But I guess this only works for certain DB types as e.g. the DateTime type passes a formated string to the DB but we need a Date object inside the entity.

Comment on lines +251 to +255
$this->assertNull($newEntity->floaty);

// re-fetching the entity now results in 0, not null anymore as the DB value is not the same as the marshaled one
$newEntity = $table->get($newEntity->id);
$this->assertEquals(0, $newEntity->floaty);
Copy link
Member

Choose a reason for hiding this comment

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

This is strange. If the field was set to null, and the record was persisted, shouldn't the value be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thats exactly what I mean what is weird. We default to null in the marshaling process for unknown values, but the value being written to the DB goes through the toDatabase() method and therefore - in this case - always gets casted to a float unless its actually null or ''

Thats why null marshalled value will become 0 in this case in the DB, but we have other cases of this as well.

The base problem is still imho the fact the "lets default to null for unknown values" in marshalling.

@LordSimal LordSimal force-pushed the 6.x-marshaling-weirdness branch from d316f86 to e52b695 Compare February 7, 2026 09:08
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.

3 participants