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

api-platform / core / 10323454690

09 Aug 2024 05:46PM UTC coverage: 7.841%. Remained the same
10323454690

push

github

soyuka
Merge 3.4

2 of 2 new or added lines in 1 file covered. (100.0%)

1896 existing lines in 118 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 hits per line

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

100.0
/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
    }
2,612✔
35

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

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

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

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

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

68
        return $data;
57✔
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;
57✔
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] : [];
2,503✔
82
    }
83
}
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