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

api-platform / core / 3711198772

pending completion
3711198772

push

github

GitHub
fix(jsonschema): remove @id @type @context from input schema  (#5267)

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

7447 of 12203 relevant lines covered (61.03%)

82.13 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\Api\IriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Util\ClassInfoTrait;
20
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22

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

31
    public const FORMAT = 'jsonapi';
32

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

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

45
    public function hasCacheableSupportsMethod(): bool
46
    {
47
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
589✔
48
    }
49

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

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

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

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

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