diff --git a/src/Resolve/Handler/EntityPropertyHandler.php b/src/Resolve/Handler/EntityPropertyHandler.php index 8c44dac..9e642d3 100644 --- a/src/Resolve/Handler/EntityPropertyHandler.php +++ b/src/Resolve/Handler/EntityPropertyHandler.php @@ -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 @@ -100,6 +102,7 @@ public function handle( continue; } + $hasInputBoundVirtuals = true; $resolved = $this->propertyResolver->resolve($virtualMeta, $accessor, $virtualDefaultBag, $prefix); if (null !== $resolved) { @@ -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) @@ -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, diff --git a/tests/Feature/Resolver/NestedFindOneByLiteralTest.php b/tests/Feature/Resolver/NestedFindOneByLiteralTest.php new file mode 100644 index 0000000..cd14534 --- /dev/null +++ b/tests/Feature/Resolver/NestedFindOneByLiteralTest.php @@ -0,0 +1,83 @@ +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']); + } +} diff --git a/tests/Fixture/Dto/NestedFindOneByLiteralFilesDto.php b/tests/Fixture/Dto/NestedFindOneByLiteralFilesDto.php new file mode 100644 index 0000000..fd2a50a --- /dev/null +++ b/tests/Fixture/Dto/NestedFindOneByLiteralFilesDto.php @@ -0,0 +1,17 @@ + + */ + #[Assert\Valid] + public array $files = []; +} diff --git a/tests/Fixture/Dto/NestedFindOneByLiteralLeafDto.php b/tests/Fixture/Dto/NestedFindOneByLiteralLeafDto.php new file mode 100644 index 0000000..cee9ca8 --- /dev/null +++ b/tests/Fixture/Dto/NestedFindOneByLiteralLeafDto.php @@ -0,0 +1,25 @@ +find($criteria, $meta); + })] + #[Assert\NotNull(message: 'entity is required')] + public \stdClass|null $entity = null; +} diff --git a/tests/Fixture/Dto/NestedFindOneByLiteralRootDto.php b/tests/Fixture/Dto/NestedFindOneByLiteralRootDto.php new file mode 100644 index 0000000..8e60fee --- /dev/null +++ b/tests/Fixture/Dto/NestedFindOneByLiteralRootDto.php @@ -0,0 +1,17 @@ + + */ + #[Assert\Valid] + public array $attachments = []; +}