Skip to content

Commit 42b759e

Browse files
committed
Throw exception if the value could not converted to database representation
1 parent 3719b08 commit 42b759e

File tree

2 files changed

+13
-50
lines changed

2 files changed

+13
-50
lines changed

src/Doctrine/Orm/Filter/AbstractUuidFilter.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,19 @@ private function convertValuesToTheDatabaseRepresentation(QueryBuilder $queryBui
122122
$doctrineType = Type::getType($doctrineFieldType);
123123
$platform = $queryBuilder->getEntityManager()->getConnection()->getDatabasePlatform();
124124

125-
if (\is_array($value)) {
126-
$databaseValues = [];
127-
foreach ($value as $val) {
128-
try {
129-
$databaseValues[] = $doctrineType->convertToDatabaseValue($val, $platform);
130-
} catch (ConversionException $e) {
131-
$this->logger->notice('Invalid value conversion to database representation', [
132-
'exception' => $e,
133-
]);
134-
}
125+
$convertValue = static function (mixed $value) use ($doctrineType, $platform) {
126+
try {
127+
return $doctrineType->convertToDatabaseValue($value, $platform);
128+
} catch (ConversionException $e) {
129+
throw new InvalidArgumentException(\sprintf('The value "%s" could not be converted to database representation.', $value), previous: $e);
135130
}
131+
};
136132

137-
return $databaseValues;
133+
if (\is_array($value)) {
134+
return array_map($convertValue, $value);
138135
}
139136

140-
try {
141-
return $doctrineType->convertToDatabaseValue($value, $platform);
142-
} catch (ConversionException $e) {
143-
$this->logger->notice('Invalid value conversion to database representation', [
144-
'exception' => $e,
145-
]);
146-
147-
return null;
148-
}
137+
return $convertValue($value);
149138
}
150139

151140
/**

tests/Functional/Uuid/UuidFilterBaseTestCase.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
1717
use ApiPlatform\Tests\RecreateSchemaTrait;
1818
use ApiPlatform\Tests\SetupClassResourcesTrait;
19+
use Symfony\Component\HttpFoundation\Response;
1920

2021
abstract class UuidFilterBaseTestCase extends ApiTestCase
2122
{
@@ -147,16 +148,7 @@ public function testSearchFilterByInvalidUuid(): void
147148
],
148149
]);
149150

150-
self::assertResponseIsSuccessful();
151-
$json = $response->toArray();
152-
153-
self::assertArraySubset(['hydra:totalItems' => 0], $json);
154-
self::assertArraySubset(
155-
[
156-
'hydra:member' => [],
157-
],
158-
$json
159-
);
151+
$this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST);
160152
}
161153

162154
public function testSearchFilterByManyInvalidUuid(): void
@@ -174,16 +166,7 @@ public function testSearchFilterByManyInvalidUuid(): void
174166
],
175167
]);
176168

177-
self::assertResponseIsSuccessful();
178-
$json = $response->toArray();
179-
180-
self::assertArraySubset(['hydra:totalItems' => 0], $json);
181-
self::assertArraySubset(
182-
[
183-
'hydra:member' => [],
184-
],
185-
$json
186-
);
169+
$this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST);
187170
}
188171

189172
public function testSearchFilterOnManyToOneRelationByUuid(): void
@@ -288,16 +271,7 @@ public function testSearchFilterOnManyToOneRelationByInvalidUuids(): void
288271
],
289272
]);
290273

291-
self::assertResponseIsSuccessful();
292-
$json = $response->toArray();
293-
294-
self::assertArraySubset(['hydra:totalItems' => 0], $json);
295-
self::assertArraySubset(
296-
[
297-
'hydra:member' => [],
298-
],
299-
$json
300-
);
274+
$this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST);
301275
}
302276

303277
public function testGetOpenApiDescription(): void

0 commit comments

Comments
 (0)