| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
/** |
| 4 |
* @license Apache 2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace OpenApi; |
| 8 |
|
| 9 |
use OpenApi\Annotations as OA; |
| 10 |
use Symfony\Component\Yaml\Yaml; |
| 11 |
|
| 12 |
/** |
| 13 |
* Allows to serialize/de-serialize annotations from/to JSON. |
| 14 |
*/ |
| 15 |
class Serializer |
| 16 |
{ |
| 17 |
private static array $VALID_ANNOTATIONS = [ |
| 18 |
OA\AdditionalProperties::class, |
| 19 |
OA\Attachable::class, |
| 20 |
OA\Components::class, |
| 21 |
OA\Contact::class, |
| 22 |
OA\Delete::class, |
| 23 |
OA\Discriminator::class, |
| 24 |
OA\Encoding::class, |
| 25 |
OA\Examples::class, |
| 26 |
OA\ExternalDocumentation::class, |
| 27 |
OA\Flow::class, |
| 28 |
OA\Get::class, |
| 29 |
OA\Head::class, |
| 30 |
OA\Header::class, |
| 31 |
OA\Info::class, |
| 32 |
OA\Items::class, |
| 33 |
OA\JsonContent::class, |
| 34 |
OA\License::class, |
| 35 |
OA\Link::class, |
| 36 |
OA\MediaType::class, |
| 37 |
OA\OpenApi::class, |
| 38 |
OA\Operation::class, |
| 39 |
OA\Options::class, |
| 40 |
OA\Parameter::class, |
| 41 |
OA\PathParameter::class, |
| 42 |
OA\QueryParameter::class, |
| 43 |
OA\CookieParameter::class, |
| 44 |
OA\HeaderParameter::class, |
| 45 |
OA\Patch::class, |
| 46 |
OA\PathItem::class, |
| 47 |
OA\Post::class, |
| 48 |
OA\Property::class, |
| 49 |
OA\Put::class, |
| 50 |
OA\Query::class, |
| 51 |
OA\RequestBody::class, |
| 52 |
OA\Response::class, |
| 53 |
OA\Schema::class, |
| 54 |
OA\SecurityScheme::class, |
| 55 |
OA\Server::class, |
| 56 |
OA\ServerVariable::class, |
| 57 |
OA\Tag::class, |
| 58 |
OA\Trace::class, |
| 59 |
OA\Webhook::class, |
| 60 |
OA\Xml::class, |
| 61 |
OA\XmlContent::class, |
| 62 |
]; |
| 63 |
|
| 64 |
/** |
| 65 |
* @param class-string<OA\AbstractAnnotation> $className |
| 66 |
*/ |
| 67 |
protected static function isValidAnnotationClass(string $className): bool |
| 68 |
{ |
| 69 |
return in_array($className, self::$VALID_ANNOTATIONS); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Deserialize a string. |
| 74 |
* |
| 75 |
* @param class-string<OA\AbstractAnnotation> $className |
| 76 |
*/ |
| 77 |
public function deserialize(string $jsonString, string $className, ?Context $context = null): OA\AbstractAnnotation |
| 78 |
{ |
| 79 |
if (!static::isValidAnnotationClass($className)) { |
| 80 |
throw new OpenApiException($className . ' is not defined in OpenApi PHP Annotations'); |
| 81 |
} |
| 82 |
|
| 83 |
return $this->doDeserialize(json_decode($jsonString), $className, $context ?? new Context(['generated' => true])); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Deserialize a file. |
| 88 |
* |
| 89 |
* @param class-string<OA\AbstractAnnotation> $className |
| 90 |
*/ |
| 91 |
public function deserializeFile(string $filename, string $format = 'json', string $className = OA\OpenApi::class, ?Context $context = null): OA\AbstractAnnotation |
| 92 |
{ |
| 93 |
if (!static::isValidAnnotationClass($className)) { |
| 94 |
throw new OpenApiException($className . ' is not a valid OpenApi PHP Annotations'); |
| 95 |
} |
| 96 |
|
| 97 |
$contents = file_get_contents($filename); |
| 98 |
|
| 99 |
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
| 100 |
if ('yaml' === $format || in_array($ext, ['yml', 'yaml'], strict: true)) { |
| 101 |
$contents = json_encode(Yaml::parse($contents)); |
| 102 |
} |
| 103 |
|
| 104 |
return $this->doDeserialize(json_decode($contents), $className, $context ?? new Context(['generated' => true])); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Do deserialization. |
| 109 |
* |
| 110 |
* @param class-string<OA\AbstractAnnotation> $className |
| 111 |
*/ |
| 112 |
protected function doDeserialize(\stdClass $c, string $className, Context $context): OA\AbstractAnnotation |
| 113 |
{ |
| 114 |
$annotation = new $className(['_context' => $context]); |
| 115 |
foreach ((array) $c as $property => $value) { |
| 116 |
if ($property === '$ref') { |
| 117 |
$property = 'ref'; |
| 118 |
} |
| 119 |
|
| 120 |
if (str_starts_with((string) $property, 'x-')) { |
| 121 |
if (Generator::isDefault($annotation->x)) { |
| 122 |
$annotation->x = []; |
| 123 |
} |
| 124 |
$custom = substr((string) $property, 2); |
| 125 |
$annotation->x[$custom] = $value; |
| 126 |
} else { |
| 127 |
$annotation->{$property} = $this->doDeserializeProperty($annotation, $property, $value, $context); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if ($annotation instanceof OA\OpenApi) { |
| 132 |
$context->root()->version = $annotation->openapi; |
| 133 |
} |
| 134 |
|
| 135 |
return $annotation; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Deserialize the annotation's property. |
| 140 |
*/ |
| 141 |
protected function doDeserializeProperty(OA\AbstractAnnotation $annotation, string $property, $value, Context $context) |
| 142 |
{ |
| 143 |
// property is primitive type |
| 144 |
if (array_key_exists($property, $annotation::$_types)) { |
| 145 |
return $this->doDeserializeBaseProperty($annotation::$_types[$property], $value, $context); |
| 146 |
} |
| 147 |
|
| 148 |
// property is embedded annotation |
| 149 |
// note: this does not support custom nested annotation classes |
| 150 |
foreach ($annotation::$_nested as $nestedClass => $declaration) { |
| 151 |
if (is_string($declaration)) { |
| 152 |
// property is an annotation |
| 153 |
if ($declaration === $property) { |
| 154 |
if (is_object($value)) { |
| 155 |
return $this->doDeserialize($value, $nestedClass, $context); |
| 156 |
} else { |
| 157 |
return $value; |
| 158 |
} |
| 159 |
} |
| 160 |
} elseif (count($declaration) === 1 && $declaration[0] === $property) { |
| 161 |
// property is an annotation array |
| 162 |
$annotationArr = []; |
| 163 |
foreach ($value as $v) { |
| 164 |
$annotationArr[] = $this->doDeserialize($v, $nestedClass, $context); |
| 165 |
} |
| 166 |
|
| 167 |
return $annotationArr; |
| 168 |
} elseif (count($declaration) === 2 && $declaration[0] === $property) { |
| 169 |
$key = $declaration[1]; |
| 170 |
$annotationHash = []; |
| 171 |
foreach ($value as $k => $v) { |
| 172 |
$annotation = $this->doDeserialize($v, $nestedClass, $context); |
| 173 |
$annotation->{$key} = $k; |
| 174 |
$annotationHash[$k] = $annotation; |
| 175 |
} |
| 176 |
|
| 177 |
return $annotationHash; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return $value; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Deserialize base annotation property. |
| 186 |
* |
| 187 |
* @param array|string $type The property type |
| 188 |
* @param mixed $value The value to deserialization |
| 189 |
* |
| 190 |
* @return array|OA\AbstractAnnotation |
| 191 |
*/ |
| 192 |
protected function doDeserializeBaseProperty($type, mixed $value, Context $context) |
| 193 |
{ |
| 194 |
$isAnnotationClass = is_string($type) && is_subclass_of(trim($type, '[]'), OA\AbstractAnnotation::class); |
| 195 |
|
| 196 |
if ($isAnnotationClass) { |
| 197 |
$isArray = str_starts_with($type, '[') && str_ends_with($type, ']'); |
| 198 |
|
| 199 |
if ($isArray) { |
| 200 |
$annotationArr = []; |
| 201 |
$class = trim($type, '[]'); |
| 202 |
|
| 203 |
foreach ($value as $v) { |
| 204 |
$annotationArr[] = $this->doDeserialize($v, $class, $context); |
| 205 |
} |
| 206 |
|
| 207 |
return $annotationArr; |
| 208 |
} |
| 209 |
|
| 210 |
return $this->doDeserialize($value, $type, $context); |
| 211 |
} |
| 212 |
|
| 213 |
return $value; |
| 214 |
} |
| 215 |
} |