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

api-platform / core / 17562281609

08 Sep 2025 07:47PM UTC coverage: 0.0% (-22.6%) from 22.604%
17562281609

Pull #7374

github

web-flow
Merge 0c2b4a90a into 6db55be8c
Pull Request #7374: fix(jsonld): various json streamer fixes

0 of 33 new or added lines in 2 files covered. (0.0%)

12082 existing lines in 401 files now uncovered.

0 of 52792 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    }
×
34

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

43
    /**
44
     * @param string|null $format
45
     */
46
    public function getSupportedTypes($format): array
47
    {
UNCOV
48
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
×
49
    }
50

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

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

UNCOV
73
        $data = $this->decorated->normalize($object, $format, $context);
×
UNCOV
74
        if (!\is_array($data) || !$data) {
×
UNCOV
75
            return $data;
×
76
        }
77

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

UNCOV
87
        $metadata = $this->createJsonLdContext($this->anonymousContextBuilder, $object, $context);
×
88

UNCOV
89
        return $metadata + $data;
×
90
    }
91
}
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