Skip to content

IBX-11687: Fixed ContentTypeDomainMapper to not unset the default field value#755

Open
Sztig wants to merge 3 commits into
4.6from
IBX-11687-content-type-field-default-value-fix
Open

IBX-11687: Fixed ContentTypeDomainMapper to not unset the default field value#755
Sztig wants to merge 3 commits into
4.6from
IBX-11687-content-type-field-default-value-fix

Conversation

@Sztig
Copy link
Copy Markdown
Contributor

@Sztig Sztig commented May 25, 2026

🎫 Issue IBX-11687

Description:

This is a fix to prevent ContentTypeDomainMapper from unsetting the default field value when updating content type through API. This happens when the default value is not provided during the update.

Added test coverage for this case as well.

@Sztig Sztig requested a review from a team May 25, 2026 10:19

self::assertNotNull($updatedFieldDefinition);
self::assertEquals($fieldDefinition->defaultValue, $updatedFieldDefinition->defaultValue);
self::assertSame(100, $updatedFieldDefinition->position);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be good to check position before updating the field definition.

$fieldType->method('validateValidatorConfiguration')->willReturn([]);
$fieldType->method('validateFieldSettings')->willReturn([]);
$fieldType->method('isSearchable')->willReturn(true);
$fieldType->expects(self::once())->method('acceptValue')->with($expectedInput)->willReturn($expectedInput);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
$fieldType->expects(self::once())->method('acceptValue')->with($expectedInput)->willReturn($expectedInput);
$fieldType
->expects(self::once())
->method('acceptValue')
->with($expectedInput)
->willReturn($expectedInput);

$fieldType->method('validateFieldSettings')->willReturn([]);
$fieldType->method('isSearchable')->willReturn(true);
$fieldType->expects(self::once())->method('acceptValue')->with($expectedInput)->willReturn($expectedInput);
$fieldType->expects(self::once())->method('toPersistenceValue')->with($expectedInput)->willReturn($persistedValue);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
$fieldType->expects(self::once())->method('toPersistenceValue')->with($expectedInput)->willReturn($persistedValue);
$fieldType
->expects(self::once())
->method('toPersistenceValue')
->with($expectedInput)
->willReturn($persistedValue);


$spiFieldDefinition->fieldTypeConstraints->validators = $validatorConfiguration;
$spiFieldDefinition->fieldTypeConstraints->fieldSettings = $fieldSettings;
$defaultValue = $fieldDefinitionUpdateStruct->defaultValue ?? $fieldDefinition->defaultValue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One thing that troubles me here is that the defaultValue in update struct could be set to null and it would be overriden by field definition's one because of ??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If the intention is to have the field empty then the field value should be set to an empty string instead of null IMO. Entire struct here is set up in this way where null means that it was not updated.

Copy link
Copy Markdown
Contributor

@barw4 barw4 May 25, 2026

Choose a reason for hiding this comment

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

That is true for values sent from the Back Office - it's an empty string.

However, we also have an API. If one browses through contracts, one spots:

    /**
     * If set the default value for this field is changed to the given value.
     */
    public mixed $defaultValue = null;

one would think that setting it to null also sets the db column to null, but it's not the case, it skips the current value of FieldDefinitionCreateStruct. Maybe it would be enough to update description for this field in contracts? Open for discussion

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can see your point, this would probably require some dedicated flag just for defaultValue I guess.
If clearing to null is a valid use case then it is kinda a separate API design problem and how this defaultValue in particular differs from the rest of the struct.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yes, this is an API problem, however, imo in this PR we are fixing one issue with unitialized value that has a default but we are introducing a new one because the behavior is now changed for an initialized field with null value.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we should populate defaultValue with current default at creation then?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we handle this without ??? Right now, both "not provided" and "explicitly set to null" end up with the current default value, so API clients cannot intentionally clear an existing default value.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@mikadamczyk mikadamczyk left a comment

Choose a reason for hiding this comment

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

Could we add an integration test for this API case? Something like existing field default value is set, then FieldDefinitionUpdateStruct->defaultValue is explicitly set to null? This would clearly document the intended contract and prevent regressions.


$spiFieldDefinition->fieldTypeConstraints->validators = $validatorConfiguration;
$spiFieldDefinition->fieldTypeConstraints->fieldSettings = $fieldSettings;
$defaultValue = $fieldDefinitionUpdateStruct->defaultValue ?? $fieldDefinition->defaultValue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we handle this without ??? Right now, both "not provided" and "explicitly set to null" end up with the current default value, so API clients cannot intentionally clear an existing default value.

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.

7 participants