-
-
Notifications
You must be signed in to change notification settings - Fork 882
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(openapi): compatibility with OpenAPI 3.0 (#6065)
fixes #5978 Co-authored-by: soyuka <[email protected]>
- Loading branch information
Showing
7 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\OpenApi\Serializer; | ||
|
||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
final class LegacyOpenApiNormalizer implements NormalizerInterface | ||
{ | ||
public const SPEC_VERSION = 'spec_version'; | ||
private array $defaultContext = [ | ||
self::SPEC_VERSION => '3.1.0', | ||
]; | ||
|
||
public function __construct(private readonly NormalizerInterface $decorated, $defaultContext = []) | ||
{ | ||
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); | ||
} | ||
|
||
public function normalize(mixed $object, string $format = null, array $context = []): array | ||
{ | ||
$openapi = $this->decorated->normalize($object, $format, $context); | ||
|
||
if ('3.0.0' !== ($context['spec_version'] ?? null)) { | ||
return $openapi; | ||
} | ||
|
||
$schemas = &$openapi['components']['schemas']; | ||
$openapi['openapi'] = '3.0.0'; | ||
foreach ($openapi['components']['schemas'] as $name => $component) { | ||
foreach ($component['properties'] ?? [] as $property => $value) { | ||
if (\is_array($value['type'] ?? false)) { | ||
foreach ($value['type'] as $type) { | ||
$schemas[$name]['properties'][$property]['anyOf'][] = ['type' => $type]; | ||
} | ||
unset($schemas[$name]['properties'][$property]['type']); | ||
} | ||
unset($schemas[$name]['properties'][$property]['owl:maxCardinality']); | ||
} | ||
} | ||
|
||
return $openapi; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool | ||
{ | ||
return $this->decorated->supportsNormalization($data, $format, $context); | ||
} | ||
|
||
public function getSupportedTypes($format): array | ||
{ | ||
return $this->decorated->getSupportedTypes($format); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters