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

api-platform / core / 17729190580

15 Sep 2025 09:57AM UTC coverage: 22.227% (-0.4%) from 22.578%
17729190580

push

github

web-flow
fix(metadata): compute isWritable during updates (#7383)

fixes #7382

4 of 532 new or added lines in 16 files covered. (0.75%)

167 existing lines in 22 files now uncovered.

11127 of 50061 relevant lines covered (22.23%)

23.49 hits per line

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

83.33
/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];
90✔
33
        $inputOrOutput = Schema::TYPE_OUTPUT === $type ? ($operation->getOutput() ?? $inputOrOutput) : ($operation->getInput() ?? $inputOrOutput);
90✔
34
        $forceSubschema = $serializerContext[SchemaFactory::FORCE_SUBSCHEMA] ?? false;
90✔
35

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

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

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

57
            return $this->findOperationForType($resourceMetadataCollection, $type, $operation, $forceSubschema ? null : $format);
52✔
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()) {
66✔
62
            $resourceMetadataCollection = $this->resourceMetadataFactory->create($className);
8✔
63

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

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

71
        return $operation;
58✔
72
    }
73

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

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

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

96
        return $operation;
52✔
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() ?? []);
50✔
102
    }
103

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

108
        return end($parts);
52✔
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

© 2026 Coveralls, Inc