• 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

93.75
/src/JsonLd/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\JsonLd\Serializer;
15

16
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
17
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
18
use ApiPlatform\Metadata\IriConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20

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

29
    public const FORMAT = 'jsonld';
30

31
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private AnonymousContextBuilderInterface $anonymousContextBuilder)
32
    {
UNCOV
33
    }
954✔
34

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

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

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

58
        /*
59
         * Converts the normalized data array of a resource into an IRI, if the
60
         * normalized data array is empty.
61
         *
62
         * This is useful when traversing from a non-resource towards an attribute
63
         * which is a resource, as we do not have the benefit of {@see ApiProperty::isReadableLink}.
64
         *
65
         * It must not be propagated to resources, as {@see ApiProperty::isReadableLink}
66
         * should take effect.
67
         */
UNCOV
68
        $context['api_empty_resource_as_iri'] = true;
29✔
69

UNCOV
70
        $data = $this->decorated->normalize($object, $format, $context);
29✔
UNCOV
71
        if (!\is_array($data) || !$data) {
29✔
UNCOV
72
            return $data;
7✔
73
        }
74

UNCOV
75
        if (isset($originalResource)) {
22✔
76
            try {
UNCOV
77
                $context['output']['iri'] = $this->iriConverter->getIriFromResource($originalResource);
2✔
78
            } catch (InvalidArgumentException) {
×
79
                // The original resource has no identifiers
80
            }
UNCOV
81
            $context['api_resource'] = $originalResource;
2✔
82
        }
83

UNCOV
84
        $metadata = $this->createJsonLdContext($this->anonymousContextBuilder, $object, $context);
22✔
85

UNCOV
86
        return $metadata + $data;
22✔
87
    }
88
}
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