• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 17496868802

05 Sep 2025 03:02PM UTC coverage: 22.604% (+0.05%) from 22.559%
17496868802

push

github

soyuka
fix(symfony): remove deprecation about jsonopenapi

12072 of 53406 relevant lines covered (22.6%)

26.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Laravel/State/ValidateProvider.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Laravel\State;
15

16
use ApiPlatform\Metadata\Error;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\State\ProviderInterface;
20
use Illuminate\Contracts\Foundation\Application;
21
use Illuminate\Database\Eloquent\Model;
22
use Illuminate\Foundation\Http\FormRequest;
23
use Illuminate\Support\Facades\Validator;
24
use Illuminate\Validation\ValidationException;
25
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
27

28
/**
29
 * @implements ProviderInterface<object>
30
 */
31
final class ValidateProvider implements ProviderInterface
32
{
33
    use ValidationErrorTrait;
34

35
    /**
36
     * @param ProviderInterface<object> $inner
37
     */
38
    public function __construct(
39
        private readonly ProviderInterface $inner,
40
        private readonly Application $app,
41
        private readonly ?NormalizerInterface $normalizer = null,
42
        ?NameConverterInterface $nameConverter = null,
43
    ) {
44
        if (!$normalizer) {
×
45
            trigger_deprecation('api-platform/laravel', '4.2', 'Not using the normalizer in %s is deprecated.', self::class);
×
46
        }
47

48
        $this->nameConverter = $nameConverter;
×
49
    }
50

51
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
52
    {
53
        $body = $this->inner->provide($operation, $uriVariables, $context);
×
54

55
        if ($operation instanceof Error) {
×
56
            return $body;
×
57
        }
58

59
        $rules = $operation->getRules();
×
60
        if (\is_callable($rules)) {
×
61
            $rules = $rules();
×
62
        }
63

64
        if (\is_string($rules) && is_a($rules, FormRequest::class, true)) {
×
65
            try {
66
                // this also throws an AuthorizationException
67
                $this->app->make($rules);
×
68
            } catch (ValidationException $e) { // @phpstan-ignore-line make->($rules) may throw this
×
69
                if (!$operation->canValidate()) {
×
70
                    return $body;
×
71
                }
72

73
                throw $this->getValidationError($e->validator, $e);
×
74
            }
75

76
            return $body;
×
77
        }
78

79
        if (!$operation->canValidate()) {
×
80
            return $body;
×
81
        }
82

83
        if (!\is_array($rules)) {
×
84
            return $body;
×
85
        }
86

87
        $validationBody = $this->getBodyForValidation($body);
×
88

89
        $validator = Validator::make($validationBody, $rules);
×
90
        if ($validator->fails()) {
×
91
            throw $this->getValidationError($validator, new ValidationException($validator));
×
92
        }
93

94
        return $body;
×
95
    }
96

97
    /**
98
     * @return array<string, mixed>
99
     */
100
    private function getBodyForValidation(mixed $body): array
101
    {
102
        if (!$body) {
×
103
            return [];
×
104
        }
105

106
        if ($body instanceof Model) {
×
107
            return $body->toArray();
×
108
        }
109

110
        if ($this->normalizer) {
×
111
            if (!\is_array($v = $this->normalizer->normalize($body))) {
×
112
                throw new RuntimeException('An array is expected.');
×
113
            }
114

115
            return $v;
×
116
        }
117

118
        // hopefully this path never gets used, its there for BC-layer only
119
        // TODO: remove in 5.0
120
        if ($s = json_encode($body)) {
×
121
            return json_decode($s, true);
×
122
        }
123

124
        throw new RuntimeException('Could not transform the denormalized body in an array for validation');
×
125
    }
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc