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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

31.82
/src/JsonApi/Serializer/ObjectNormalizer.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\JsonApi\Serializer;
15

16
use ApiPlatform\Metadata\IriConverterInterface;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\Metadata\ResourceClassResolverInterface;
19
use ApiPlatform\Metadata\Util\ClassInfoTrait;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21

22
/**
23
 * Decorates the output with JSON API metadata when appropriate, but otherwise
24
 * just passes through to the decorated normalizer.
25
 */
26
final class ObjectNormalizer implements NormalizerInterface
27
{
28
    use ClassInfoTrait;
29

30
    public const FORMAT = 'jsonapi';
31

32
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
33
    {
UNCOV
34
    }
950✔
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
40
    {
41
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
11✔
42
    }
43

44
    public function getSupportedTypes($format): array
45
    {
UNCOV
46
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
806✔
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
53
    {
54
        if (isset($context['api_resource'])) {
11✔
55
            $originalResource = $context['api_resource'];
×
56
            unset($context['api_resource']);
×
57
        }
58

59
        $data = $this->decorated->normalize($object, $format, $context);
11✔
60
        if (!\is_array($data) || isset($context['api_attribute'])) {
11✔
61
            return $data;
11✔
62
        }
63

64
        if (isset($originalResource)) {
×
65
            $resourceClass = $this->resourceClassResolver->getResourceClass($originalResource);
×
66
            $resourceData = [
×
67
                'id' => $this->iriConverter->getIriFromResource($originalResource),
×
68
                'type' => $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getShortName(),
×
69
            ];
×
70
        } else {
71
            $resourceData = [
×
72
                'id' => $this->iriConverter->getIriFromResource($object),
×
73
                'type' => (new \ReflectionClass($this->getObjectClass($object)))->getShortName(),
×
74
            ];
×
75
        }
76

77
        if ($data) {
×
78
            $resourceData['attributes'] = $data;
×
79
        }
80

81
        return ['data' => $resourceData];
×
82
    }
83
}
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

© 2025 Coveralls, Inc