Skip to content

Commit 0910073

Browse files
committed
Fix errors
1 parent b037265 commit 0910073

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/lib/AttributeResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ protected function resolveProperty(
225225
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
226226
->setNullable($nullableValue)
227227
->setIsPrimary($property->isPrimaryKey())
228-
->setForeignKeyColumnName($property->fkColName);
228+
->setForeignKeyColumnName($property->fkColName)
229+
->setFakerStub($this->guessFakerStub($attribute, $property));
229230
if ($property->isReference()) {
230231
if ($property->isVirtual()) {
231232
throw new InvalidDefinitionException('References not supported for virtual attributes');

src/lib/FakerStubResolver.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public function fakeForObject(SpecObjectInterface $items): string
336336
foreach ($items->properties as $name => $prop) {
337337
/** @var SpecObjectInterface $prop */
338338

339-
if ($prop->properties) { // object
339+
if ($prop->properties) { // nested object
340340
$result = $this->{__FUNCTION__}($prop);
341341
} else {
342342
$ps = new PropertySchema($prop, $name, $cs);
@@ -387,21 +387,31 @@ public function arbitraryArray(): string
387387

388388
public function aElementFaker($data): ?string
389389
{
390-
$aElementData = Json::decode(Json::encode($data));
390+
$aElementData = Json::decode(Json::encode($data)); // object of stdClass -> array
391391
$compoSchemaData = [
392392
'properties' => [
393393
'unnamedProp' => $aElementData['items']
394394
]
395395
];
396396

397397
$schema = new Schema($compoSchemaData);
398+
$cs = new ComponentSchema($schema, 'UnnamedCompo');
398399
if ($this->config) {
399400
$rc = new ReferenceContext($this->config->getOpenApi(), Yii::getAlias($this->config->openApiPath));
400401
$schema->setReferenceContext($rc);
401402
}
402-
403-
$cs = new ComponentSchema($schema, 'UnnamedCompo');
404403
$dbModels = (new AttributeResolver('UnnamedCompo', $cs, new JunctionSchemas([]), $this->config))->resolve();
404+
405+
foreach ($schema->properties as $name => $prop) {
406+
if($prop->items instanceof Reference) {
407+
$dbModels->attributes[$name] = new Attribute($name, [
408+
'phpType' => 'array',
409+
'dbType' => 'array',
410+
'reference' => $prop->items->getReference(),
411+
]);
412+
}
413+
}
414+
405415
return (new static($dbModels->attributes['unnamedProp'], $cs->getProperty('unnamedProp'), $this->config))->resolve();
406416
}
407417
}

tests/specs/issue_fix/20_consider_openapi_spec_examples_in_faker_code_generation/index.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ components:
3434
name:
3535
type: string
3636
example: cat
37-
# example: ['long-tail', 'short-tail', 'black', 'white']
3837
age:
3938
type: integer
4039
example: 2
@@ -48,8 +47,7 @@ components:
4847
minItems: 6
4948
maxItems: 10
5049
uniqueItems: true
51-
# type: string
52-
# example: ['long-tail', 'short-tail', 'black', 'white']
50+
# example: ['long-tail', 'short-tail', 'black', 'white']
5351
number_arr:
5452
type: array
5553
items:

0 commit comments

Comments
 (0)