Skip to content
Merged
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
19 changes: 19 additions & 0 deletions src/Resolve/Handler/EntityPropertyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function handle(
// virtual itself didn't declare one.
$virtualDefaultBag = $meta->bag ?? $defaultBag;
$anyInputVisited = false;
$hasInputBoundVirtuals = false;
$missingInputBoundCount = 0;

foreach ($meta->virtual as $target => $virtualMeta) {
if ($virtualMeta instanceof Dynamic
Expand All @@ -100,6 +102,7 @@ public function handle(
continue;
}

$hasInputBoundVirtuals = true;
$resolved = $this->propertyResolver->resolve($virtualMeta, $accessor, $virtualDefaultBag, $prefix);

if (null !== $resolved) {
Expand All @@ -108,6 +111,7 @@ public function handle(
$dto->visit($name, $target);
$anyInputVisited = true;
} else {
++$missingInputBoundCount;
$raw = TypeInfoUtils::isCollection($virtualMeta->type) ? [] : null;
$coercion = null !== $virtualMeta->coercer
? $this->coercerRegistry->get($virtualMeta->coercer)->coerce($virtualMeta)
Expand Down Expand Up @@ -162,6 +166,21 @@ public function handle(
};
}

// If any input-bound virtual didn't receive its input, short-circuit the
// loader to the type-appropriate empty value. Running it with partial /
// null criteria would crash strict DB drivers (e.g. Doctrine on a `null`
// identifier) without telling the user anything useful. Constraints on
// the virtual still validate as before — explicit NotNull is what the
// user uses to surface a per-field violation; absent that, the entity
// simply stays at its default and a property-level constraint (e.g.
// NotNull on the property) reports the missing entity.
// DTOs whose criteria come entirely from Literal/Dynamic virtuals
// (no input-bound fields at all) load unconditionally.
if ($hasInputBoundVirtuals && $missingInputBoundCount > 0) {
$many = $find->many;
$loader = static fn (): array|null => $many ? [] : null;
}

$pending[] = new PendingEntityValue(
$dto,
$name,
Expand Down
83 changes: 83 additions & 0 deletions tests/Feature/Resolver/NestedFindOneByLiteralTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace DualMedia\DtoRequestBundle\Tests\Feature\Resolver;

use DualMedia\DtoRequestBundle\Resolve\DtoResolver;
use DualMedia\DtoRequestBundle\Tests\Fixture\Dto\NestedFindOneByLiteralRootDto;
use DualMedia\DtoRequestBundle\Tests\Fixture\Service\TestObjectProvider;
use DualMedia\DtoRequestBundle\Tests\PHPUnit\KernelTestCase;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\HttpFoundation\Request;

#[Group('feature')]
#[Group('resolver')]
class NestedFindOneByLiteralTest extends KernelTestCase
{
private DtoResolver $resolver;

private TestObjectProvider $provider;

protected function setUp(): void
{
$this->resolver = static::getService(DtoResolver::class);
$this->provider = static::getService(TestObjectProvider::class);
$this->provider->store = [];
$this->provider->calls = [];
}

public function testDeeplyNestedFindOneByValidatesNotNullWhenInputMissing(): void
{
$dto = $this->resolver->resolve(
NestedFindOneByLiteralRootDto::class,
new Request(request: [
'attachments' => [
['files' => [[]]], // file entry exists, but no someId
],
])
);

// Resolution must complete without exceptions and produce a validation
// failure (NotNull on the leaf entity), not a 500-style provider crash.
static::assertFalse($dto->isValid(), 'expected NotNull violation on the leaf entity');

$messages = [];

foreach ($dto->getConstraintViolationList() as $violation) {
$messages[] = $violation->getMessage();
}

static::assertContains('entity is required', $messages);

// Provider must not have been invoked when no input identifier was sent —
// otherwise the entity loader would run with criteria=['id' => null, ...]
// and trip the underlying database driver.
static::assertSame([], $this->provider->calls);
}

public function testDeeplyNestedFindOneByLoadsEntityWhenInputProvided(): void
{
$expected = new \stdClass();
$this->provider->store['42'] = $expected;

$dto = $this->resolver->resolve(
NestedFindOneByLiteralRootDto::class,
new Request(request: [
'attachments' => [
['files' => [['someId' => '42']]],
],
])
);

static::assertTrue($dto->isValid(), (string)$dto->getConstraintViolationList());
static::assertCount(1, $dto->attachments);
static::assertCount(1, $dto->attachments[0]->files);
static::assertSame($expected, $dto->attachments[0]->files[0]->entity);

// Provider was called exactly once with the resolved id and the literal.
static::assertCount(1, $this->provider->calls);
static::assertSame('42', $this->provider->calls[0]['criteria']['id']);
static::assertTrue($this->provider->calls[0]['criteria']['other']);
}
}
17 changes: 17 additions & 0 deletions tests/Fixture/Dto/NestedFindOneByLiteralFilesDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DualMedia\DtoRequestBundle\Tests\Fixture\Dto;

use DualMedia\DtoRequestBundle\Dto\AbstractDto;
use Symfony\Component\Validator\Constraints as Assert;

class NestedFindOneByLiteralFilesDto extends AbstractDto
{
/**
* @var list<NestedFindOneByLiteralLeafDto>
*/
#[Assert\Valid]
public array $files = [];
}
25 changes: 25 additions & 0 deletions tests/Fixture/Dto/NestedFindOneByLiteralLeafDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace DualMedia\DtoRequestBundle\Tests\Fixture\Dto;

use DualMedia\DtoRequestBundle\Dto\AbstractDto;
use DualMedia\DtoRequestBundle\Dto\Attribute\Field;
use DualMedia\DtoRequestBundle\Dto\Attribute\FindOneBy;
use DualMedia\DtoRequestBundle\Dto\Attribute\WithObjectProvider;
use DualMedia\DtoRequestBundle\Dto\Model\Literal;
use DualMedia\DtoRequestBundle\Tests\Fixture\Service\TestObjectProvider;
use Symfony\Component\Validator\Constraints as Assert;

class NestedFindOneByLiteralLeafDto extends AbstractDto
{
#[FindOneBy]
#[Field('id', 'someId')]
#[Field('other', new Literal(true))]
#[WithObjectProvider(static function (TestObjectProvider $provider, array $criteria, array $meta): \stdClass|null {
return $provider->find($criteria, $meta);
})]
#[Assert\NotNull(message: 'entity is required')]
public \stdClass|null $entity = null;
}
17 changes: 17 additions & 0 deletions tests/Fixture/Dto/NestedFindOneByLiteralRootDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DualMedia\DtoRequestBundle\Tests\Fixture\Dto;

use DualMedia\DtoRequestBundle\Dto\AbstractDto;
use Symfony\Component\Validator\Constraints as Assert;

class NestedFindOneByLiteralRootDto extends AbstractDto
{
/**
* @var list<NestedFindOneByLiteralFilesDto>
*/
#[Assert\Valid]
public array $attachments = [];
}
Loading