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

api-platform / core / 13175753759

06 Feb 2025 09:30AM UTC coverage: 0.0% (-7.7%) from 7.663%
13175753759

Pull #6947

github

web-flow
Merge 432a515ad into de2d298e3
Pull Request #6947: fix(metadata): remove temporary fix for ArrayCollection

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

11655 existing lines in 385 files now uncovered.

0 of 47351 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/JsonSchema/ResourceMetadataTrait.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\JsonSchema;
15

16
use ApiPlatform\Metadata\CollectionOperationInterface;
17
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
21
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
22

23
/**
24
 * @internal
25
 */
26
trait ResourceMetadataTrait
27
{
28
    use ResourceClassInfoTrait;
29

30
    private function findOutputClass(string $className, string $type, Operation $operation, ?array $serializerContext): ?string
31
    {
UNCOV
32
        $inputOrOutput = ['class' => $className];
×
UNCOV
33
        $inputOrOutput = Schema::TYPE_OUTPUT === $type ? ($operation->getOutput() ?? $inputOrOutput) : ($operation->getInput() ?? $inputOrOutput);
×
UNCOV
34
        $forceSubschema = $serializerContext[SchemaFactory::FORCE_SUBSCHEMA] ?? false;
×
35

UNCOV
36
        return $forceSubschema ? ($inputOrOutput['class'] ?? $inputOrOutput->class ?? $operation->getClass()) : ($inputOrOutput['class'] ?? $inputOrOutput->class ?? null);
×
37
    }
38

39
    private function findOperation(string $className, string $type, ?Operation $operation, ?array $serializerContext): Operation
40
    {
UNCOV
41
        if (null === $operation) {
×
UNCOV
42
            if (null === $this->resourceMetadataFactory) {
×
43
                return new HttpOperation();
×
44
            }
UNCOV
45
            $resourceMetadataCollection = $this->resourceMetadataFactory->create($className);
×
46

47
            try {
UNCOV
48
                $operation = $resourceMetadataCollection->getOperation();
×
49
            } catch (OperationNotFoundException $e) {
×
50
                $operation = new HttpOperation();
×
51
            }
UNCOV
52
            $forceSubschema = $serializerContext[SchemaFactory::FORCE_SUBSCHEMA] ?? false;
×
UNCOV
53
            if ($operation->getShortName() === $this->getShortClassName($className) && $forceSubschema) {
×
UNCOV
54
                $operation = new HttpOperation();
×
55
            }
56

UNCOV
57
            return $this->findOperationForType($resourceMetadataCollection, $type, $operation);
×
58
        }
59

60
        // The best here is to use an Operation when calling `buildSchema`, we try to do a smart guess otherwise
UNCOV
61
        if ($this->resourceMetadataFactory && !$operation->getClass()) {
×
UNCOV
62
            $resourceMetadataCollection = $this->resourceMetadataFactory->create($className);
×
63

UNCOV
64
            if ($operation->getName()) {
×
UNCOV
65
                return $resourceMetadataCollection->getOperation($operation->getName());
×
66
            }
67

UNCOV
68
            return $this->findOperationForType($resourceMetadataCollection, $type, $operation);
×
69
        }
70

UNCOV
71
        return $operation;
×
72
    }
73

74
    private function findOperationForType(ResourceMetadataCollection $resourceMetadataCollection, string $type, Operation $operation): Operation
75
    {
76
        // Find the operation and use the first one that matches criterias
UNCOV
77
        foreach ($resourceMetadataCollection as $resourceMetadata) {
×
UNCOV
78
            foreach ($resourceMetadata->getOperations() ?? [] as $op) {
×
UNCOV
79
                if ($operation instanceof CollectionOperationInterface && $op instanceof CollectionOperationInterface) {
×
80
                    $operation = $op;
×
81
                    break 2;
×
82
                }
83

UNCOV
84
                if (Schema::TYPE_INPUT === $type && \in_array($op->getMethod(), ['POST', 'PATCH', 'PUT'], true)) {
×
UNCOV
85
                    $operation = $op;
×
UNCOV
86
                    break 2;
×
87
                }
88
            }
89
        }
90

UNCOV
91
        return $operation;
×
92
    }
93

94
    private function getSerializerContext(Operation $operation, string $type = Schema::TYPE_OUTPUT): array
95
    {
UNCOV
96
        return Schema::TYPE_OUTPUT === $type ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
×
97
    }
98

99
    private function getShortClassName(string $fullyQualifiedName): string
100
    {
UNCOV
101
        $parts = explode('\\', $fullyQualifiedName);
×
102

UNCOV
103
        return end($parts);
×
104
    }
105
}
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