• 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/JsonStreamer/WritePropertyMetadataLoader.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\JsonStreamer;
15

16
use ApiPlatform\Hydra\Collection;
17
use ApiPlatform\Hydra\IriTemplate;
18
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22
use ApiPlatform\Metadata\Util\TypeHelper;
23
use Symfony\Component\JsonStreamer\Mapping\PropertyMetadata;
24
use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoaderInterface;
25
use Symfony\Component\TypeInfo\Type;
26

27
final class WritePropertyMetadataLoader implements PropertyMetadataLoaderInterface
28
{
29
    public function __construct(
30
        private readonly PropertyMetadataLoaderInterface $loader,
31
        private readonly ResourceClassResolverInterface $resourceClassResolver,
32
        private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
33
        private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory,
34
    ) {
UNCOV
35
    }
×
36

37
    public function load(string $className, array $options = [], array $context = []): array
38
    {
UNCOV
39
        $properties = $this->loader->load($className, $options, $context);
×
40

UNCOV
41
        if (IriTemplate::class === $className) {
×
UNCOV
42
            $properties['template'] = new PropertyMetadata(
×
UNCOV
43
                'template',
×
UNCOV
44
                Type::string(),
×
UNCOV
45
                ['api_platform.hydra.json_streamer.write.value_transformer.template'],
×
UNCOV
46
            );
×
47

UNCOV
48
            return $properties;
×
49
        }
50

UNCOV
51
        if (Collection::class !== $className && !$this->resourceClassResolver->isResourceClass($className)) {
×
UNCOV
52
            return $properties;
×
53
        }
54

55

NEW
56
        $originalClassName = TypeHelper::getClassName($context['original_type']);
×
NEW
57
        $hasIri = true;
×
NEW
58
        $virtualProperty = 'id';
×
59

NEW
60
        if ($className !== $originalClassName) {
×
NEW
61
            foreach ($this->propertyNameCollectionFactory->create($originalClassName) as $property) {
×
NEW
62
                $propertyMetadata = $this->propertyMetadataFactory->create($originalClassName, $property);
×
NEW
63
                if ($propertyMetadata->isIdentifier()) {
×
NEW
64
                    $virtualProperty = $property;
×
65
                }
66

NEW
67
                if ($propertyMetadata->getNativeType()->isIdentifiedBy($className)) {
×
NEW
68
                    $hasIri = $propertyMetadata->getGenId();
×
NEW
69
                    $virtualProperty = iterator_to_array($this->propertyNameCollectionFactory->create($className))[0];
×
70
                }
71
            }
72
        }
73

NEW
74
        if ($hasIri) {
×
NEW
75
            $properties['@id'] = new PropertyMetadata(
×
NEW
76
                $virtualProperty, // virtual property
×
NEW
77
                Type::mixed(), // virtual property
×
NEW
78
                ['api_platform.jsonld.json_streamer.write.value_transformer.iri'],
×
NEW
79
            );
×
80
        }
81

UNCOV
82
        $properties['@type'] = new PropertyMetadata(
×
NEW
83
            $virtualProperty, // virtual property
×
UNCOV
84
            Type::mixed(), // virtual property
×
UNCOV
85
            ['api_platform.jsonld.json_streamer.write.value_transformer.type'],
×
UNCOV
86
        );
×
87

NEW
88
        if ($className !== $originalClassName) {
×
NEW
89
            return $properties;
×
90
        }
91

UNCOV
92
        if (Collection::class === $originalClassName || ($this->resourceClassResolver->isResourceClass($originalClassName) && !isset($context['generated_classes'][Collection::class]))) {
×
UNCOV
93
            $properties['@context'] = new PropertyMetadata(
×
NEW
94
                $virtualProperty, // virual property
×
UNCOV
95
                Type::string(), // virtual property
×
UNCOV
96
                ['api_platform.jsonld.json_streamer.write.value_transformer.context'],
×
UNCOV
97
            );
×
98
        }
99

UNCOV
100
        return $properties;
×
101
    }
102
}
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