Skip to content

Commit c52db9c

Browse files
committed
Merge #56 remote-tracking branch 'php-openapi/52-bug-dependenton-allof-with-x-faker-false'
* php-openapi/52-bug-dependenton-allof-with-x-faker-false: Fix test Fix failing tests Complete the implementation Complete the test Add test - WIP Implementation Create PR
2 parents f097660 + ff08c71 commit c52db9c

File tree

22 files changed

+654
-24
lines changed

22 files changed

+654
-24
lines changed

src/generator/default/dbmodel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
*/
5050
abstract class <?= $model->getClassName() ?> extends \yii\db\ActiveRecord
5151
{
52-
<?php if($scenarios = $model->getScenarios()):
53-
foreach($scenarios as $scenario): ?>
52+
<?php if ($scenarios = $model->getScenarios()):
53+
foreach ($scenarios as $scenario): ?>
5454
/**
5555
*<?= $scenario['description'] ?>
5656

@@ -76,7 +76,7 @@ public static function tableName()
7676
{
7777
return <?= var_export($model->getTableAlias()) ?>;
7878
}
79-
<?php if($scenarios): ?>
79+
<?php if ($scenarios): ?>
8080

8181
/**
8282
* Automatically generated scenarios from the model 'x-scenarios'.
@@ -92,7 +92,7 @@ public function scenarios()
9292
$default = parent::scenarios()[self::SCENARIO_DEFAULT];
9393

9494
return [
95-
<?php foreach($scenarios as $scenario): ?>
95+
<?php foreach ($scenarios as $scenario): ?>
9696
self::<?= $scenario['const'] ?> => $default,
9797
<?php endforeach; ?>
9898
/**

src/lib/generators/ModelsGenerator.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ public function generate():CodeFiles
7272
)
7373
));
7474
if ($this->config->generateModelFaker) {
75-
$deps = []; # list of all models that this model is dependent on
76-
foreach ($model->hasOneRelations as $key => $hasOneRelation) {
77-
$deps[] = $model->hasOneRelations[$key]->getClassName();
78-
}
79-
$deps = array_unique($deps);
80-
8175
$this->files->add(new CodeFile(
8276
Yii::getAlias("$fakerPath/{$className}Faker.php"),
8377
$this->config->render(
@@ -86,7 +80,7 @@ public function generate():CodeFiles
8680
'model' => $model,
8781
'modelNamespace' => $this->config->modelNamespace,
8882
'namespace' => $this->config->fakerNamespace,
89-
'deps' => $deps,
83+
'deps' => $model->fakerDependentModels(),
9084
]
9185
)
9286
));

src/lib/items/DbModel.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use yii\base\BaseObject;
1414
use yii\db\ColumnSchema;
1515
use yii\helpers\Inflector;
16-
use yii\helpers\StringHelper;
1716
use yii\helpers\VarDumper;
1817
use function array_filter;
1918
use function array_map;
@@ -317,4 +316,21 @@ public function getModelClassDescription(): string
317316
}
318317
return FormatHelper::getFormattedDescription($this->description);
319318
}
319+
320+
/**
321+
* Return array of models that this models depends on exclusively used in faker.
322+
* Models with `x-faker: false` or with self-reference are excluded
323+
*/
324+
public function fakerDependentModels(): array
325+
{
326+
$result = [];
327+
foreach ($this->attributes as $attribute) {
328+
if ($attribute->reference && $attribute->fakerStub) {
329+
if ($this->name !== $attribute->reference) { # exclude self-referenced models
330+
$result[] = $attribute->reference;
331+
}
332+
}
333+
}
334+
return array_unique($result);
335+
}
320336
}

src/lib/openapi/PropertySchema.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,27 @@
77

88
namespace cebe\yii2openapi\lib\openapi;
99

10-
use yii\db\ColumnSchema;
11-
use cebe\yii2openapi\generator\ApiGenerator;
12-
use yii\db\mysql\Schema as MySqlSchema;
13-
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
14-
use yii\db\pgsql\Schema as PgSqlSchema;
15-
use cebe\yii2openapi\lib\items\Attribute;
16-
use yii\base\NotSupportedException;
1710
use BadMethodCallException;
1811
use cebe\openapi\ReferenceContext;
1912
use cebe\openapi\spec\Reference;
2013
use cebe\openapi\SpecObjectInterface;
14+
use cebe\yii2openapi\generator\ApiGenerator;
2115
use cebe\yii2openapi\lib\CustomSpecAttr;
2216
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
17+
use cebe\yii2openapi\lib\traits\ForeignKeyConstraints;
18+
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
2319
use Throwable;
2420
use Yii;
21+
use yii\base\NotSupportedException;
22+
use yii\db\ColumnSchema;
23+
use yii\db\mysql\Schema as MySqlSchema;
24+
use yii\db\pgsql\Schema as PgSqlSchema;
2525
use yii\db\Schema as YiiDbSchema;
2626
use yii\helpers\Inflector;
2727
use yii\helpers\Json;
2828
use yii\helpers\StringHelper;
29-
use yii\helpers\VarDumper;
3029
use function is_int;
3130
use function strpos;
32-
use cebe\yii2openapi\lib\traits\ForeignKeyConstraints;
3331

3432
class PropertySchema
3533
{
@@ -49,7 +47,7 @@ class PropertySchema
4947
/**
5048
* @var null|bool|string
5149
* If `false`, no faker will be generated in faker model
52-
* See more about usage in README.md file present in root directory of the project
50+
* See more about usage in README.md file present in root directory of this library
5351
*/
5452
public $xFaker;
5553

tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/OrderFaker.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public static function dependentOn()
4343
{
4444
return [
4545
// just model class names
46-
'Invoice',
4746

4847
];
4948
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.yml',
5+
'generateUrls' => false,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
13+
];
14+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
openapi: 3.0.3
2+
3+
info:
4+
title: 'Bug: dependentOn: allOf with "x-faker: false" #52'
5+
version: 1.0.0
6+
7+
components:
8+
schemas:
9+
User:
10+
type: object
11+
properties:
12+
id:
13+
type: integer
14+
name:
15+
type: string
16+
Fruit:
17+
type: object
18+
properties:
19+
id:
20+
type: integer
21+
name:
22+
type: string
23+
Animal:
24+
type: object
25+
properties:
26+
id:
27+
type: integer
28+
name:
29+
type: string
30+
Invoice:
31+
title: Invoice
32+
x-table: invoices
33+
type: object
34+
properties:
35+
id:
36+
type: integer
37+
reference_invoice:
38+
allOf:
39+
- $ref: '#/components/schemas/Invoice'
40+
- x-faker: false
41+
- description: This field is only set on invoices of type "cancellation_invoice"
42+
reference_invoice_2:
43+
allOf:
44+
- $ref: '#/components/schemas/Invoice'
45+
- x-faker: true
46+
user:
47+
$ref: '#/components/schemas/User'
48+
user_2:
49+
allOf:
50+
- $ref: '#/components/schemas/User'
51+
- x-faker: false
52+
fruit:
53+
$ref: '#/components/schemas/Fruit'
54+
animal:
55+
allOf:
56+
- $ref: '#/components/schemas/Animal'
57+
- x-faker: false
58+
59+
paths:
60+
'/':
61+
get:
62+
responses:
63+
'200':
64+
description: OK
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Animal extends \app\models\base\Animal
6+
{
7+
8+
9+
}
10+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace app\models;
3+
4+
use Faker\UniqueGenerator;
5+
6+
/**
7+
* Fake data generator for Animal
8+
* @method static Animal makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
9+
* @method static Animal saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
10+
* @method static Animal[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
11+
* @method static Animal[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
12+
*/
13+
class AnimalFaker extends BaseModelFaker
14+
{
15+
16+
/**
17+
* @param array|callable $attributes
18+
* @return Animal|\yii\db\ActiveRecord
19+
* @example
20+
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
21+
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
22+
* $model->scenario = 'create';
23+
* $model->author_id = 1;
24+
* return $model;
25+
* });
26+
**/
27+
public function generateModel($attributes = [])
28+
{
29+
$faker = $this->faker;
30+
$uniqueFaker = $this->uniqueFaker;
31+
$model = new Animal();
32+
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
33+
$model->name = $faker->sentence;
34+
if (!is_callable($attributes)) {
35+
$model->setAttributes($attributes, false);
36+
} else {
37+
$model = $attributes($model, $faker, $uniqueFaker);
38+
}
39+
return $model;
40+
}
41+
}

0 commit comments

Comments
 (0)