| 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\Generator; |
| 12 |
|
| 13 |
class AugmentRefs |
| 14 |
{ |
| 15 |
use Concerns\RefTrait; |
| 16 |
|
| 17 |
public function __invoke(Analysis $analysis): void |
| 18 |
{ |
| 19 |
$this->resolveAllOfRefs($analysis); |
| 20 |
$this->resolveFQCNRefs($analysis); |
| 21 |
$this->removeDuplicateRefs($analysis); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Update refs broken due to <code>allOf</code> augmenting. |
| 26 |
*/ |
| 27 |
protected function resolveAllOfRefs(Analysis $analysis): void |
| 28 |
{ |
| 29 |
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class); |
| 30 |
|
| 31 |
// ref rewriting |
| 32 |
$updatedRefs = []; |
| 33 |
foreach ($schemas as $schema) { |
| 34 |
if (!Generator::isDefault($schema->allOf)) { |
| 35 |
// do we have to keep track of property refs that need updating? |
| 36 |
foreach ($schema->allOf as $ii => $allOfSchema) { |
| 37 |
if (!Generator::isDefault($allOfSchema->properties)) { |
| 38 |
$updatedRefs[OA\Components::ref($schema->schema . '/properties', false)] = OA\Components::ref($schema->schema . '/allOf/' . $ii . '/properties', false); |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if ($updatedRefs) { |
| 46 |
foreach ($analysis->annotations as $annotation) { |
| 47 |
if (property_exists($annotation, 'ref') && !Generator::isDefault($annotation->ref) && $annotation->ref !== null) { |
| 48 |
foreach ($updatedRefs as $origRef => $updatedRef) { |
| 49 |
if (str_starts_with((string) $annotation->ref, $origRef)) { |
| 50 |
$annotation->ref = str_replace($origRef, $updatedRef, (string) $annotation->ref); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
protected function resolveFQCNRefs(Analysis $analysis): void |
| 59 |
{ |
| 60 |
$annotations = $analysis->getAnnotationsOfType(OA\Components::componentTypes()); |
| 61 |
|
| 62 |
foreach ($annotations as $annotation) { |
| 63 |
if (property_exists($annotation, 'ref') && !Generator::isDefault($annotation->ref) && is_string($annotation->ref) && !$this->isRef($annotation->ref)) { |
| 64 |
// check if we can resolve the ref to a component |
| 65 |
$resolved = false; |
| 66 |
foreach (OA\Components::componentTypes() as $type) { |
| 67 |
if ($refSchema = $analysis->getAnnotationForSource($annotation->ref, $type)) { |
| 68 |
$resolved = true; |
| 69 |
$annotation->ref = OA\Components::ref($refSchema); |
| 70 |
} |
| 71 |
} |
| 72 |
if (!$resolved && ($refAnnotation = $analysis->getAnnotationForSource($annotation->ref, $annotation::class))) { |
| 73 |
$annotation->ref = OA\Components::ref($refAnnotation); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
protected function removeDuplicateRefs(Analysis $analysis): void |
| 80 |
{ |
| 81 |
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class); |
| 82 |
|
| 83 |
foreach ($schemas as $schema) { |
| 84 |
if (!Generator::isDefault($schema->allOf)) { |
| 85 |
$refs = []; |
| 86 |
$dupes = []; |
| 87 |
foreach ($schema->allOf as $ii => $allOfSchema) { |
| 88 |
if (!Generator::isDefault($allOfSchema->ref)) { |
| 89 |
if (in_array($allOfSchema->ref, $refs)) { |
| 90 |
$dupes[] = $allOfSchema->ref; |
| 91 |
$analysis->removeAnnotation($allOfSchema); |
| 92 |
unset($schema->allOf[$ii]); |
| 93 |
continue; |
| 94 |
} |
| 95 |
$refs[] = $allOfSchema->ref; |
| 96 |
} |
| 97 |
} |
| 98 |
if ($dupes) { |
| 99 |
$schema->allOf = array_values($schema->allOf); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |