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

api-platform / core / 14246153067

03 Apr 2025 02:56PM UTC coverage: 7.286% (-0.002%) from 7.288%
14246153067

push

github

web-flow
Merge commit from fork

12 of 160 new or added lines in 7 files covered. (7.5%)

2343 existing lines in 152 files now uncovered.

12450 of 170870 relevant lines covered (7.29%)

12.06 hits per line

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

97.87
/src/OpenApi/Serializer/OpenApiNormalizer.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\OpenApi\Serializer;
15

16
use ApiPlatform\OpenApi\Model\Paths;
17
use ApiPlatform\OpenApi\OpenApi;
18
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
19
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21

22
/**
23
 * Generates an OpenAPI v3 specification.
24
 */
25
final class OpenApiNormalizer implements NormalizerInterface
26
{
27
    public const FORMAT = 'json';
28
    public const JSON_FORMAT = 'jsonopenapi';
29
    public const YAML_FORMAT = 'yamlopenapi';
30
    private const EXTENSION_PROPERTIES_KEY = 'extensionProperties';
31

32
    public function __construct(private readonly NormalizerInterface $decorated)
33
    {
34
    }
1,108✔
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
40
    {
41
        $pathsCallback = $this->getPathsCallBack();
33✔
42
        $context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] = true;
33✔
43
        $context[AbstractObjectNormalizer::SKIP_NULL_VALUES] = true;
33✔
44
        $context[AbstractNormalizer::CALLBACKS] = [
33✔
45
            'paths' => $pathsCallback,
33✔
46
        ];
33✔
47

48
        return $this->recursiveClean($this->decorated->normalize($object, $format, $context));
33✔
49
    }
50

51
    private function recursiveClean(array $data): array
52
    {
53
        foreach ($data as $key => $value) {
33✔
54
            if (self::EXTENSION_PROPERTIES_KEY === $key) {
33✔
55
                foreach ($data[self::EXTENSION_PROPERTIES_KEY] as $extensionPropertyKey => $extensionPropertyValue) {
33✔
UNCOV
56
                    $data[$extensionPropertyKey] = $extensionPropertyValue;
12✔
57
                }
58
                continue;
33✔
59
            }
60

61
            if (\is_array($value)) {
33✔
62
                $data[$key] = $this->recursiveClean($value);
33✔
63
            }
64
        }
65

66
        unset($data[self::EXTENSION_PROPERTIES_KEY]);
33✔
67

68
        return $data;
33✔
69
    }
70

71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
75
    {
76
        return (self::FORMAT === $format || self::JSON_FORMAT === $format || self::YAML_FORMAT === $format) && $data instanceof OpenApi;
33✔
77
    }
78

79
    public function getSupportedTypes($format): array
80
    {
81
        return (self::FORMAT === $format || self::JSON_FORMAT === $format || self::YAML_FORMAT === $format) ? [OpenApi::class => true] : [];
1,063✔
82
    }
83

84
    private function getPathsCallBack(): \Closure
85
    {
86
        return static function ($decoratedObject): array {
33✔
87
            if ($decoratedObject instanceof Paths) {
33✔
88
                $paths = $decoratedObject->getPaths();
33✔
89

90
                // sort paths by tags, then by path for each tag
91
                uksort($paths, function ($keyA, $keyB) use ($paths) {
33✔
92
                    $a = $paths[$keyA];
33✔
93
                    $b = $paths[$keyB];
33✔
94

95
                    $tagsA = [
33✔
96
                        ...($a->getGet()?->getTags() ?? []),
33✔
97
                        ...($a->getPost()?->getTags() ?? []),
33✔
98
                        ...($a->getPatch()?->getTags() ?? []),
33✔
99
                        ...($a->getPut()?->getTags() ?? []),
33✔
100
                        ...($a->getDelete()?->getTags() ?? []),
33✔
101
                    ];
33✔
102
                    sort($tagsA);
33✔
103

104
                    $tagsB = [
33✔
105
                        ...($b->getGet()?->getTags() ?? []),
33✔
106
                        ...($b->getPost()?->getTags() ?? []),
33✔
107
                        ...($b->getPatch()?->getTags() ?? []),
33✔
108
                        ...($b->getPut()?->getTags() ?? []),
33✔
109
                        ...($b->getDelete()?->getTags() ?? []),
33✔
110
                    ];
33✔
111
                    sort($tagsB);
33✔
112

113
                    return match (true) {
114
                        current($tagsA) === current($tagsB) => $keyA <=> $keyB,
33✔
115
                        default => current($tagsA) <=> current($tagsB),
33✔
116
                    };
117
                });
33✔
118

119
                return $paths;
33✔
120
            }
121

122
            return [];
×
123
        };
33✔
124
    }
125
}
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