Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib/Repository/Mapper/ContentTypeDomainMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,9 @@ public function buildSPIFieldDefinitionFromUpdateStruct(

$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.

$spiFieldDefinition->defaultValue = $fieldType->toPersistenceValue(
$fieldType->acceptValue($fieldDefinitionUpdateStruct->defaultValue)
$fieldType->acceptValue($defaultValue)
);

return $spiFieldDefinition;
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/Core/Repository/ContentTypeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,30 @@ public function testUpdateFieldDefinitionWithEmptyStruct()
);
}

public function testUpdateFieldDefinitionPreservesDefaultValueWhenNotInUpdateStruct(): void
{
$repository = $this->getRepository();
$contentTypeService = $repository->getContentTypeService();

$contentTypeDraft = $this->createContentTypeDraft();
$fieldDefinition = $contentTypeDraft->getFieldDefinition('body');

self::assertNotNull($fieldDefinition);
self::assertNotNull($fieldDefinition->defaultValue);
self::assertSame(2, $fieldDefinition->position);

$updateStruct = $contentTypeService->newFieldDefinitionUpdateStruct();
$updateStruct->position = 100;

$contentTypeService->updateFieldDefinition($contentTypeDraft, $fieldDefinition, $updateStruct);
$contentTypeDraft = $contentTypeService->loadContentTypeDraft($contentTypeDraft->id);
$updatedFieldDefinition = $contentTypeDraft->getFieldDefinition('body');

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.

}

/**
* Test for the updateFieldDefinition() method with already defined field identifier.
*
Expand Down
117 changes: 117 additions & 0 deletions tests/lib/Repository/Mapper/ContentTypeDomainMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Core\Repository\Mapper;

use Ibexa\Contracts\Core\FieldType\FieldType as FieldTypeInterface;
use Ibexa\Contracts\Core\Persistence\Content\FieldValue;
use Ibexa\Contracts\Core\Persistence\Content\Language\Handler as SPILanguageHandler;
use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as SPITypeHandler;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinitionUpdateStruct;
use Ibexa\Core\FieldType\FieldTypeRegistry;
use Ibexa\Core\FieldType\TextLine\Value as TextLineValue;
use Ibexa\Core\Repository\Mapper\ContentTypeDomainMapper;
use Ibexa\Core\Repository\Values\ContentType\FieldDefinition;
use PHPUnit\Framework\TestCase;

/**
* @covers \Ibexa\Core\Repository\Mapper\ContentTypeDomainMapper
*/
final class ContentTypeDomainMapperTest extends TestCase
{
private ContentTypeDomainMapper $mapper;

/** @var \Ibexa\Core\FieldType\FieldTypeRegistry&\PHPUnit\Framework\MockObject\MockObject */
private FieldTypeRegistry $fieldTypeRegistry;

protected function setUp(): void
{
$this->fieldTypeRegistry = $this->createMock(FieldTypeRegistry::class);

$this->mapper = new ContentTypeDomainMapper(
$this->createMock(SPITypeHandler::class),
$this->createMock(SPILanguageHandler::class),
$this->fieldTypeRegistry,
);
}

public function testBuildSPIFieldDefinitionFromUpdateStructPreservesDefaultValueWhenNotSet(): void
{
$existingDefaultValue = new TextLineValue('Foo');
$persistedValue = new FieldValue(['data' => 'Foo']);

$this->configureFieldTypeRegistry($existingDefaultValue, $persistedValue);

$updateStruct = new FieldDefinitionUpdateStruct();
$updateStruct->position = 100;

$spiFieldDefinition = $this->mapper->buildSPIFieldDefinitionFromUpdateStruct(
$updateStruct,
$this->buildFieldDefinition($existingDefaultValue),
'eng-GB'
);

self::assertSame($persistedValue, $spiFieldDefinition->defaultValue);
self::assertSame(100, $spiFieldDefinition->position);
}

public function testBuildSPIFieldDefinitionFromUpdateStructOverridesDefaultValueWhenExplicitlySet(): void
{
$newDefaultValue = new TextLineValue('Bar');
$persistedValue = new FieldValue(['data' => 'Bar']);

$this->configureFieldTypeRegistry($newDefaultValue, $persistedValue);

$updateStruct = new FieldDefinitionUpdateStruct();
$updateStruct->defaultValue = $newDefaultValue;

$spiFieldDefinition = $this->mapper->buildSPIFieldDefinitionFromUpdateStruct(
$updateStruct,
$this->buildFieldDefinition(new TextLineValue('Foo')),
'eng-GB'
);

self::assertSame($persistedValue, $spiFieldDefinition->defaultValue);
}

private function buildFieldDefinition(TextLineValue $defaultValue): FieldDefinition
{
return new FieldDefinition([
'id' => 1,
'identifier' => 'my_name',
'fieldTypeIdentifier' => 'ezstring',
'defaultValue' => $defaultValue,
'isTranslatable' => false,
'isRequired' => false,
'isInfoCollector' => false,
'isThumbnail' => false,
'isSearchable' => true,
'position' => 1,
]);
}

private function configureFieldTypeRegistry(TextLineValue $expectedInput, FieldValue $persistedValue): void
{
$fieldType = $this->createMock(FieldTypeInterface::class);
$fieldType->method('validateValidatorConfiguration')->willReturn([]);
$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);

$this->fieldTypeRegistry->method('getFieldType')->with('ezstring')->willReturn($fieldType);
}
}
Loading