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

api-platform / core / 10903050455

17 Sep 2024 12:29PM UTC coverage: 7.684% (+0.7%) from 6.96%
10903050455

push

github

web-flow
fix: swagger ui with route identifier (#6616)

2 of 6 new or added lines in 6 files covered. (33.33%)

9000 existing lines in 286 files now uncovered.

12668 of 164863 relevant lines covered (7.68%)

22.93 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
    {
UNCOV
34
    }
2,262✔
35

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

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

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

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

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

UNCOV
68
        return $data;
39✔
69
    }
70

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

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

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

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

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

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

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

UNCOV
119
                return $paths;
39✔
120
            }
121

122
            return [];
×
UNCOV
123
        };
39✔
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