Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 111 additions & 23 deletions src/OpenApi/Serializer/LegacyOpenApiNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
final class LegacyOpenApiNormalizer implements NormalizerInterface
{
public const SPEC_VERSION = 'spec_version';

private const SCHEMA_BRANCH_KEYS = ['properties', 'patternProperties'];
private const SCHEMA_LIST_KEYS = ['allOf', 'oneOf', 'anyOf'];
private const SCHEMA_NESTED_KEYS = ['items', 'additionalProperties', 'not', 'contains', 'propertyNames', 'if', 'then', 'else'];

private array $defaultContext = [
self::SPEC_VERSION => '3.1.0',
];
Expand All @@ -27,9 +32,6 @@ public function __construct(private readonly NormalizerInterface $decorated, arr
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}

/**
* {@inheritdoc}
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
$openapi = $this->decorated->normalize($data, $format, $context);
Expand All @@ -38,40 +40,126 @@ public function normalize(mixed $data, ?string $format = null, array $context =
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']);
}

if (\is_array($value['examples'] ?? false)) {
$schemas[$name]['properties'][$property]['example'] = $value['examples'];
unset($schemas[$name]['properties'][$property]['examples']);
}
}
foreach ($openapi['components']['schemas'] ?? [] as $name => $component) {
$openapi['components']['schemas'][$name] = $this->downgradeSchema($component);
}

foreach ($openapi['paths'] ?? [] as $path => $operations) {
$openapi['paths'][$path] = $this->downgradePathItem($operations);
}

return $openapi;
}

/**
* {@inheritdoc}
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $this->decorated->supportsNormalization($data, $format, $context);
}

/**
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array
{
return $this->decorated->getSupportedTypes($format);
}

private function downgradePathItem(mixed $pathItem): mixed
{
if (!\is_array($pathItem)) {
return $pathItem;
}

foreach ($pathItem as $method => $operation) {
if (!\is_array($operation)) {
continue;
}

if (isset($operation['requestBody']['content']) && \is_array($operation['requestBody']['content'])) {
foreach ($operation['requestBody']['content'] as $mediaType => $media) {
if (isset($media['schema'])) {
$pathItem[$method]['requestBody']['content'][$mediaType]['schema'] = $this->downgradeSchema($media['schema']);
}
}
}

foreach ($operation['responses'] ?? [] as $status => $response) {
if (!\is_array($response)) {
continue;
}
foreach ($response['content'] ?? [] as $mediaType => $media) {
if (isset($media['schema'])) {
$pathItem[$method]['responses'][$status]['content'][$mediaType]['schema'] = $this->downgradeSchema($media['schema']);
}
}
}

foreach ($operation['parameters'] ?? [] as $index => $parameter) {
if (isset($parameter['schema'])) {
$pathItem[$method]['parameters'][$index]['schema'] = $this->downgradeSchema($parameter['schema']);
}
}
}

return $pathItem;
}

private function downgradeSchema(mixed $schema): mixed
{
if (!\is_array($schema)) {
return $schema;
}

if (\is_array($schema['type'] ?? null)) {
$types = array_values($schema['type']);
$nullable = \in_array('null', $types, true);
$nonNull = array_values(array_filter($types, static fn ($t) => 'null' !== $t));

if (1 === \count($nonNull)) {
$schema['type'] = $nonNull[0];
} elseif ([] === $nonNull) {
unset($schema['type']);
} else {
unset($schema['type']);
$schema['anyOf'] = array_map(static fn ($t) => ['type' => $t], $nonNull);
}

if ($nullable) {
$schema['nullable'] = true;
}
}

if (\array_key_exists('examples', $schema)) {
$schema['example'] = $schema['examples'];
unset($schema['examples']);
}

foreach (self::SCHEMA_BRANCH_KEYS as $key) {
if (!isset($schema[$key]) || !\is_array($schema[$key])) {
continue;
}
foreach ($schema[$key] as $name => $child) {
$schema[$key][$name] = $this->downgradeSchema($child);
}
}

foreach (self::SCHEMA_LIST_KEYS as $key) {
if (!isset($schema[$key]) || !\is_array($schema[$key])) {
continue;
}
foreach ($schema[$key] as $index => $child) {
$schema[$key][$index] = $this->downgradeSchema($child);
}
}

foreach (self::SCHEMA_NESTED_KEYS as $key) {
if (!isset($schema[$key])) {
continue;
}
if (\is_array($schema[$key])) {
$schema[$key] = $this->downgradeSchema($schema[$key]);
}
}

return $schema;
}
}
Loading
Loading