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

api-platform / core / 7473959644

10 Jan 2024 11:03AM UTC coverage: 37.227% (+0.04%) from 37.183%
7473959644

push

github

web-flow
fix(openapi): compatibility with OpenAPI 3.0 (#6065)

fixes #5978

Co-authored-by: soyuka <soyuka@users.noreply.github.com>

29 of 39 new or added lines in 4 files covered. (74.36%)

10330 of 27749 relevant lines covered (37.23%)

28.69 hits per line

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

37.5
/src/OpenApi/Serializer/LegacyOpenApiNormalizer.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 Symfony\Component\Serializer\Normalizer\NormalizerInterface;
17

18
final class LegacyOpenApiNormalizer implements NormalizerInterface
19
{
20
    public const SPEC_VERSION = 'spec_version';
21
    private array $defaultContext = [
22
        self::SPEC_VERSION => '3.1.0',
23
    ];
24

25
    public function __construct(private readonly NormalizerInterface $decorated, $defaultContext = [])
26
    {
27
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
116✔
28
    }
29

30
    public function normalize(mixed $object, string $format = null, array $context = []): array
31
    {
32
        $openapi = $this->decorated->normalize($object, $format, $context);
20✔
33

34
        if ('3.0.0' !== ($context['spec_version'] ?? null)) {
20✔
35
            return $openapi;
20✔
36
        }
37

NEW
38
        $schemas = &$openapi['components']['schemas'];
×
NEW
39
        $openapi['openapi'] = '3.0.0';
×
NEW
40
        foreach ($openapi['components']['schemas'] as $name => $component) {
×
NEW
41
            foreach ($component['properties'] ?? [] as $property => $value) {
×
NEW
42
                if (\is_array($value['type'] ?? false)) {
×
NEW
43
                    foreach ($value['type'] as $type) {
×
NEW
44
                        $schemas[$name]['properties'][$property]['anyOf'][] = ['type' => $type];
×
45
                    }
NEW
46
                    unset($schemas[$name]['properties'][$property]['type']);
×
47
                }
NEW
48
                unset($schemas[$name]['properties'][$property]['owl:maxCardinality']);
×
49
            }
50
        }
51

NEW
52
        return $openapi;
×
53
    }
54

55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
59
    {
60
        return $this->decorated->supportsNormalization($data, $format, $context);
20✔
61
    }
62

63
    public function getSupportedTypes($format): array
64
    {
65
        return $this->decorated->getSupportedTypes($format);
116✔
66
    }
67
}
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