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

api-platform / core / 15256141101

26 May 2025 02:16PM UTC coverage: 21.714% (-4.8%) from 26.526%
15256141101

push

github

web-flow
Merge 4.1

6 of 387 new or added lines in 28 files covered. (1.55%)

1891 existing lines in 117 files now uncovered.

11118 of 51201 relevant lines covered (21.71%)

29.33 hits per line

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

88.89
/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
    {
32
        $inputOrOutput = ['class' => $className];
15✔
33
        $inputOrOutput = Schema::TYPE_OUTPUT === $type ? ($operation->getOutput() ?? $inputOrOutput) : ($operation->getInput() ?? $inputOrOutput);
15✔
34
        $forceSubschema = $serializerContext[SchemaFactory::FORCE_SUBSCHEMA] ?? false;
15✔
35

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

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

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

57
            return $this->findOperationForType($resourceMetadataCollection, $type, $operation, $forceSubschema ? null : $format);
15✔
58
        }
59

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

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

68
            return $this->findOperationForType($resourceMetadataCollection, $type, $operation, $format);
15✔
69
        }
70

71
        return $operation;
15✔
72
    }
73

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

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

89
                if ($format && Schema::TYPE_OUTPUT === $type && \array_key_exists($format, $op->getOutputFormats() ?? [])) {
15✔
90
                    $operation = $op;
15✔
91
                    break 2;
15✔
92
                }
93
            }
94
        }
95

96
        return $operation;
15✔
97
    }
98

99
    private function getSerializerContext(Operation $operation, string $type = Schema::TYPE_OUTPUT): array
100
    {
101
        return Schema::TYPE_OUTPUT === $type ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
15✔
102
    }
103

104
    private function getShortClassName(string $fullyQualifiedName): string
105
    {
106
        $parts = explode('\\', $fullyQualifiedName);
15✔
107

108
        return end($parts);
15✔
109
    }
110
}
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