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