| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
/** |
| 4 |
* @license Apache 2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace OpenApi\Processors; |
| 8 |
|
| 9 |
use OpenApi\Analysis; |
| 10 |
use OpenApi\Annotations as OA; |
| 11 |
use OpenApi\Context; |
| 12 |
use OpenApi\Generator; |
| 13 |
|
| 14 |
/** |
| 15 |
* Use the Schema context to extract useful information and inject that into the annotation. |
| 16 |
* |
| 17 |
* Merges properties. |
| 18 |
*/ |
| 19 |
class AugmentSchemas |
| 20 |
{ |
| 21 |
public function __invoke(Analysis $analysis): void |
| 22 |
{ |
| 23 |
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class); |
| 24 |
|
| 25 |
$this->augmentSchema($schemas); |
| 26 |
$this->mergeUnmergedProperties($analysis); |
| 27 |
$this->augmentType($analysis, $schemas); |
| 28 |
$this->mergeAllOf($analysis, $schemas); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param array<OA\Schema> $schemas |
| 33 |
*/ |
| 34 |
protected function augmentSchema(array $schemas): void |
| 35 |
{ |
| 36 |
foreach ($schemas as $schema) { |
| 37 |
if (!$schema->isRoot(OA\Schema::class)) { |
| 38 |
continue; |
| 39 |
} |
| 40 |
if (Generator::isDefault($schema->schema)) { |
| 41 |
if ($schema->_context->is('class')) { |
| 42 |
$schema->schema = $schema->_context->class; |
| 43 |
} elseif ($schema->_context->is('interface')) { |
| 44 |
$schema->schema = $schema->_context->interface; |
| 45 |
} elseif ($schema->_context->is('trait')) { |
| 46 |
$schema->schema = $schema->_context->trait; |
| 47 |
} elseif ($schema->_context->is('enum')) { |
| 48 |
$schema->schema = $schema->_context->enum; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Merge unmerged @OA\Property annotations into the @OA\Schema of the class. |
| 56 |
*/ |
| 57 |
protected function mergeUnmergedProperties(Analysis $analysis): void |
| 58 |
{ |
| 59 |
// Merge unmerged @OA\Property annotations into the @OA\Schema of the class |
| 60 |
$unmergedProperties = $analysis->unmerged()->getAnnotationsOfType(OA\Property::class); |
| 61 |
foreach ($unmergedProperties as $property) { |
| 62 |
if ($property->_context->nested) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
$schemaContext = $property->_context->with('class') |
| 67 |
?: $property->_context->with('interface') |
| 68 |
?: $property->_context->with('trait') |
| 69 |
?: $property->_context->with('enum'); |
| 70 |
if ($schemaContext->annotations) { |
| 71 |
foreach ($schemaContext->annotations as $annotation) { |
| 72 |
if ($annotation instanceof OA\Schema) { |
| 73 |
if ($annotation->_context->nested) { |
| 74 |
// we shouldn't merge property into nested schemas |
| 75 |
continue; |
| 76 |
} |
| 77 |
|
| 78 |
$analysis->mergeAnnotations($annotation, [$property], true); |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Set schema type based on various properties. |
| 88 |
* |
| 89 |
* @param array<OA\Schema> $schemas |
| 90 |
*/ |
| 91 |
protected function augmentType(Analysis $analysis, array $schemas): void |
| 92 |
{ |
| 93 |
foreach ($schemas as $schema) { |
| 94 |
if (Generator::isDefault($schema->type)) { |
| 95 |
if (is_array($schema->properties) && $schema->properties !== []) { |
| 96 |
$schema->type = 'object'; |
| 97 |
} elseif (is_array($schema->additionalProperties) && $schema->additionalProperties !== []) { |
| 98 |
$schema->type = 'object'; |
| 99 |
} elseif (is_array($schema->patternProperties) && $schema->patternProperties !== []) { |
| 100 |
$schema->type = 'object'; |
| 101 |
} elseif (is_array($schema->unevaluatedProperties) && $schema->unevaluatedProperties !== []) { |
| 102 |
$schema->type = 'object'; |
| 103 |
} elseif (is_array($schema->propertyNames) && $schema->propertyNames !== []) { |
| 104 |
$schema->type = 'object'; |
| 105 |
} |
| 106 |
} else { |
| 107 |
if (is_string($schema->type) && $typeSchema = $analysis->getAnnotationForSource($schema->type)) { |
| 108 |
if (Generator::isDefault($schema->format)) { |
| 109 |
$schema->ref = OA\Components::ref($typeSchema); |
| 110 |
$schema->type = Generator::UNDEFINED; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Merge schema properties into <code>allOf</code> if both exist. |
| 119 |
* |
| 120 |
* @param array<OA\Schema> $schemas |
| 121 |
*/ |
| 122 |
protected function mergeAllOf(Analysis $analysis, array $schemas): void |
| 123 |
{ |
| 124 |
foreach ($schemas as $schema) { |
| 125 |
if (!Generator::isDefault($schema->properties) && !Generator::isDefault($schema->allOf)) { |
| 126 |
$allOfPropertiesSchema = null; |
| 127 |
foreach ($schema->allOf as $allOfSchema) { |
| 128 |
if (!Generator::isDefault($allOfSchema->properties)) { |
| 129 |
$allOfPropertiesSchema = $allOfSchema; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
if (!$allOfPropertiesSchema) { |
| 134 |
$allOfPropertiesSchema = new OA\Schema([ |
| 135 |
'properties' => [], |
| 136 |
'type' => 'object', |
| 137 |
'_context' => new Context(['generated' => true], $schema->_context), |
| 138 |
]); |
| 139 |
$analysis->addAnnotation($allOfPropertiesSchema, $allOfPropertiesSchema->_context); |
| 140 |
$schema->allOf[] = $allOfPropertiesSchema; |
| 141 |
} |
| 142 |
$allOfPropertiesSchema->properties = array_merge($allOfPropertiesSchema->properties, $schema->properties); |
| 143 |
/* @phpstan-ignore assign.propertyType */ |
| 144 |
$schema->properties = Generator::UNDEFINED; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |