Skip to content

Commit f0c5cd3

Browse files
committed
Fix errors 2 + implement custom attribute x-no-relation
1 parent 0910073 commit f0c5cd3

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

src/lib/AttributeResolver.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,13 @@ protected function resolveProperty(
217217
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
218218
}
219219
$attribute = Yii::createObject(Attribute::class, [$property->getName()]);
220+
221+
if (!empty($property->getAttr('x-no-relation'))) { // TODO custom attr
222+
$this->attributes[$property->getName()] = $attribute->setFakerStub($this->guessFakerStub($attribute, $property));
223+
}
224+
220225
$attribute->setRequired($isRequired)
226+
->setPhpType($property->guessPhpType())
221227
->setDescription($property->getAttr('description', ''))
222228
->setReadOnly($property->isReadonly())
223229
->setDefault($property->guessDefault())

src/lib/FakerStubResolver.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ public function aElementFaker($data): ?string
393393
'unnamedProp' => $aElementData['items']
394394
]
395395
];
396+
if (!empty($compoSchemaData['properties']['unnamedProp']['items']['$ref'])) { // TODO
397+
$compoSchemaData['properties']['unnamedProp']['x-no-relation'] = true;
398+
}
396399

397400
$schema = new Schema($compoSchemaData);
398401
$cs = new ComponentSchema($schema, 'UnnamedCompo');
@@ -402,15 +405,15 @@ public function aElementFaker($data): ?string
402405
}
403406
$dbModels = (new AttributeResolver('UnnamedCompo', $cs, new JunctionSchemas([]), $this->config))->resolve();
404407

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-
}
408+
// foreach ($schema->properties as $name => $prop) {
409+
// if($prop->items instanceof Reference) {
410+
// $dbModels->attributes[$name] = new Attribute($name, [
411+
// 'phpType' => 'array',
412+
// 'dbType' => 'array',
413+
// 'reference' => $prop->items->getReference(),
414+
// ]);
415+
// }
416+
// }
414417

415418
return (new static($dbModels->attributes['unnamedProp'], $cs->getProperty('unnamedProp'), $this->config))->resolve();
416419
}

tests/specs/blog_v2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ components:
349349
$ref: "#/components/schemas/User"
350350
comments:
351351
type: array
352+
# x-no-relation: true
352353
items:
353354
$ref: "#/components/schemas/Comment"
354355
tags:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
// 'Error',
99
// ],
1010
'generateControllers' => false,
11-
'generateMigrations' => false,
11+
'generateMigrations' => true,
1212
'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
1313
];

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,17 @@ components:
143143
id:
144144
type: integer
145145

146-
user_ref_obj_arr:
146+
user_ref_obj_arr_normal: # faker for this won't be generated
147147
type: array
148+
maxItems: 3
149+
# x-no-relation: true
150+
items:
151+
$ref: '#/components/schemas/User'
152+
153+
user_ref_obj_arr: # special
154+
type: array
155+
maxItems: 3
156+
x-no-relation: true
148157
items:
149158
$ref: '#/components/schemas/User'
150159

0 commit comments

Comments
 (0)