Skip to content

Commit b039008

Browse files
committed
fix(laravel): name convert validation property path
1 parent 730d17a commit b039008

File tree

5 files changed

+16
-75
lines changed

5 files changed

+16
-75
lines changed

src/Laravel/ApiPlatformProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
use ApiPlatform\Laravel\Eloquent\Metadata\ResourceClassResolver as EloquentResourceClassResolver;
8989
use ApiPlatform\Laravel\Eloquent\PropertyAccess\PropertyAccessor as EloquentPropertyAccessor;
9090
use ApiPlatform\Laravel\Eloquent\Serializer\SerializerContextBuilder as EloquentSerializerContextBuilder;
91-
use ApiPlatform\Laravel\Eloquent\Serializer\SnakeCaseToCamelCaseNameConverter;
9291
use ApiPlatform\Laravel\Exception\ErrorHandler;
9392
use ApiPlatform\Laravel\GraphQl\Controller\EntrypointController as GraphQlEntrypointController;
9493
use ApiPlatform\Laravel\GraphQl\Controller\GraphiQlController;
@@ -178,6 +177,7 @@
178177
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
179178
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
180179
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
180+
use Symfony\Component\Serializer\NameConverter\SnakeCaseToCamelCaseNameConverter;
181181
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
182182
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
183183
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
@@ -348,7 +348,13 @@ public function register(): void
348348
});
349349

350350
$this->app->singleton(ValidateProvider::class, function (Application $app) {
351-
return new ValidateProvider($app->make(DeserializeProvider::class), $app, $app->make(ObjectNormalizer::class));
351+
$config = $app['config'];
352+
$nameConverter = $config->get('api-platform.name_converter', SnakeCaseToCamelCaseNameConverter::class);
353+
if ($nameConverter && class_exists($nameConverter)) {
354+
$nameConverter = $app->make($nameConverter);
355+
}
356+
357+
return new ValidateProvider($app->make(DeserializeProvider::class), $app, $app->make(ObjectNormalizer::class), $nameConverter);
352358
});
353359

354360
if (class_exists(JsonApiProvider::class)) {

src/Laravel/Eloquent/Serializer/SnakeCaseToCamelCaseNameConverter.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/Laravel/State/ValidateProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Illuminate\Foundation\Http\FormRequest;
2323
use Illuminate\Support\Facades\Validator;
2424
use Illuminate\Validation\ValidationException;
25+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2526
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2627

2728
/**
@@ -39,7 +40,9 @@ public function __construct(
3940
private readonly Application $app,
4041
// TODO: trigger deprecation in API Platform 4.2 when this is not defined
4142
private readonly ?NormalizerInterface $normalizer = null,
43+
?NameConverterInterface $nameConverter = null,
4244
) {
45+
$this->nameConverter = $nameConverter;
4346
}
4447

4548
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null

src/Laravel/State/ValidationErrorTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
use ApiPlatform\Laravel\ApiResource\ValidationError;
1717
use Illuminate\Contracts\Validation\Validator;
1818
use Illuminate\Validation\ValidationException;
19+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1920

2021
trait ValidationErrorTrait
2122
{
23+
private ?NameConverterInterface $nameConverter = null;
24+
2225
private function getValidationError(Validator $validator, ValidationException $e): ValidationError
2326
{
2427
$errors = $validator->errors();
2528
$violations = [];
2629
$id = hash('xxh3', implode(',', $errors->keys()));
2730
foreach ($errors->messages() as $prop => $message) {
28-
$violations[] = ['propertyPath' => $prop, 'message' => implode(\PHP_EOL, $message)];
31+
$violations[] = ['propertyPath' => $this->nameConverter ? $this->nameConverter->normalize($prop) : $prop, 'message' => implode(\PHP_EOL, $message)];
2932
}
3033

3134
return new ValidationError($e->getMessage(), $id, $e, $violations);

src/Laravel/config/api-platform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
declare(strict_types=1);
1313

14-
use ApiPlatform\Laravel\Eloquent\Serializer\SnakeCaseToCamelCaseNameConverter;
1514
use ApiPlatform\Metadata\UrlGeneratorInterface;
1615
use Illuminate\Auth\Access\AuthorizationException;
1716
use Illuminate\Auth\AuthenticationException;
17+
use Symfony\Component\Serializer\NameConverter\SnakeCaseToCamelCaseNameConverter;
1818

1919
return [
2020
'title' => 'API Platform',

0 commit comments

Comments
 (0)