Skip to content

Commit d8165e1

Browse files
committed
Implementation
1 parent 5156a73 commit d8165e1

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

src/generator/ApiGenerator.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ public function makeConfig():Config
456456
unset($props[$key]);
457457
}
458458
$this->config = new Config($props);
459+
$this->config->resolvedOpenApi = $this->getOpenApiWithoutReferences();
459460
$this->config->setFileRenderer(function ($template, $params) {
460461
return $this->render($template, $params);
461462
});
@@ -511,9 +512,9 @@ protected function getOpenApiWithoutReferences():OpenApi
511512
if ($this->_openApiWithoutRef === null) {
512513
$file = Yii::getAlias($this->openApiPath);
513514
if (StringHelper::endsWith($this->openApiPath, '.json', false)) {
514-
$this->_openApiWithoutRef = Reader::readFromJsonFile($file, OpenApi::class, true);
515+
$this->_openApiWithoutRef = Reader::readFromJsonFile($file, OpenApi::class, true, true);
515516
} else {
516-
$this->_openApiWithoutRef = Reader::readFromYamlFile($file, OpenApi::class, true);
517+
$this->_openApiWithoutRef = Reader::readFromYamlFile($file, OpenApi::class, true, true);
517518
}
518519
}
519520
return $this->_openApiWithoutRef;

src/lib/AttributeResolver.php

+28-9
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,19 @@ class AttributeResolver
7676
/** @var Config */
7777
private $config;
7878

79-
public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions, ?Config $config = null)
79+
public $resolvedComponentSchema;
80+
81+
public function __construct(
82+
string $schemaName,
83+
ComponentSchema $schema,
84+
JunctionSchemas $junctions,
85+
?Config $config = null,
86+
$resolvedComponentSchema = null
87+
)
8088
{
8189
$this->schemaName = $schemaName;
8290
$this->componentSchema = $schema;
91+
$this->resolvedComponentSchema = $resolvedComponentSchema;
8392
$this->tableName = $schema->resolveTableName($schemaName);
8493
$this->junctions = $junctions;
8594
$this->isJunctionSchema = $junctions->isJunctionSchema($schemaName);
@@ -94,9 +103,18 @@ public function __construct(string $schemaName, ComponentSchema $schema, Junctio
94103
*/
95104
public function resolve():DbModel
96105
{
97-
foreach ($this->componentSchema->getProperties() as $property) {
106+
foreach ($this->componentSchema->getProperties() as $key => $property) {
98107
/** @var $property \cebe\yii2openapi\lib\openapi\PropertySchema */
99108

109+
$resolvedProperty = null;
110+
if ($this->resolvedComponentSchema) {
111+
foreach ($this->resolvedComponentSchema->getProperties() as $key2 => $prop) {
112+
if ($key === $key2) {
113+
$resolvedProperty = $prop;
114+
}
115+
}
116+
}
117+
100118
$isRequired = $this->componentSchema->isRequiredProperty($property->getName());
101119
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
102120
if ($nullableValue === false) { // see docs in README regarding NOT NULL, required and nullable
@@ -106,9 +124,9 @@ public function resolve():DbModel
106124
if ($this->isJunctionSchema) {
107125
$this->resolveJunctionTableProperty($property, $isRequired);
108126
} elseif ($this->hasMany2Many) {
109-
$this->resolveHasMany2ManyTableProperty($property, $isRequired);
127+
$this->resolveHasMany2ManyTableProperty($property, $isRequired, $resolvedProperty);
110128
} else {
111-
$this->resolveProperty($property, $isRequired, $nullableValue);
129+
$this->resolveProperty($property, $isRequired, $nullableValue, $resolvedProperty);
112130
}
113131
}
114132
return Yii::createObject(DbModel::class, [
@@ -169,7 +187,7 @@ protected function resolveJunctionTableProperty(PropertySchema $property, bool $
169187
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
170188
* @throws \yii\base\InvalidConfigException
171189
*/
172-
protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bool $isRequired):void
190+
protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bool $isRequired, $resolvedProperty = null):void
173191
{
174192
if ($this->junctions->isManyToManyProperty($this->schemaName, $property->getName())) {
175193
return;
@@ -203,7 +221,7 @@ protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bo
203221
return;
204222
}
205223

206-
$this->resolveProperty($property, $isRequired);
224+
$this->resolveProperty($property, $isRequired, $resolvedProperty);
207225
}
208226

209227
/**
@@ -216,14 +234,15 @@ protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bo
216234
protected function resolveProperty(
217235
PropertySchema $property,
218236
bool $isRequired,
219-
$nullableValue = 'ARG_ABSENT'
237+
$nullableValue = 'ARG_ABSENT',
238+
$resolvedProperty = null
220239
):void {
221240
if ($nullableValue === 'ARG_ABSENT') {
222241
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
223242
}
224243
$attribute = Yii::createObject(Attribute::class, [$property->getName()]);
225244
$attribute->setRequired($isRequired)
226-
->setDescription($property->getAttr('description', ''))
245+
->setDescription($resolvedProperty ? $resolvedProperty->getAttr('description', '') : $property->getAttr('description', ''))
227246
->setReadOnly($property->isReadonly())
228247
->setDefault($property->guessDefault())
229248
->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE))
@@ -262,7 +281,7 @@ protected function resolveProperty(
262281
$attribute->setPhpType($fkProperty->guessPhpType())
263282
->setDbType($fkProperty->guessDbType(true))
264283
->setSize($fkProperty->getMaxLength())
265-
->setDescription($property->getRefSchema()->getDescription())
284+
->setDescription($resolvedProperty ? $resolvedProperty->getAttr('description', '') : $property->getRefSchema()->getDescription())
266285
->setDefault($fkProperty->guessDefault())
267286
->setLimits($min, $max, $fkProperty->getMinLength());
268287

src/lib/Config.php

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ class Config extends BaseObject
164164
*/
165165
private $openApi;
166166

167+
public $resolvedOpenApi;
168+
167169
/**
168170
* @return \cebe\openapi\spec\OpenApi
169171
* @throws \cebe\openapi\exceptions\IOException

src/lib/SchemaToDatabase.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function prepareModels():array
8181
$junctions = $this->findJunctionSchemas();
8282
foreach ($openApi->components->schemas as $schemaName => $openApiSchema) {
8383
$schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]);
84+
$resolvedComponentSchema = Yii::createObject(ComponentSchema::class, [$this->config->resolvedOpenApi->components->schemas[$schemaName], $schemaName]);
8485

8586
if (!$this->canGenerateModel($schemaName, $schema)) {
8687
continue;
@@ -89,7 +90,7 @@ public function prepareModels():array
8990
$schemaName = $junctions->trimPrefix($schemaName);
9091
}
9192
/**@var \cebe\yii2openapi\lib\AttributeResolver $resolver */
92-
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]);
93+
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config, $resolvedComponentSchema]);
9394
$models[$schemaName] = $resolver->resolve();
9495
}
9596
foreach ($models as $model) {

0 commit comments

Comments
 (0)