Skip to content

Commit

Permalink
Merge #56 remote-tracking branch 'php-openapi/52-bug-dependenton-allo…
Browse files Browse the repository at this point in the history
…f-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
  • Loading branch information
cebe committed Nov 12, 2024
2 parents f097660 + ff08c71 commit c52db9c
Show file tree
Hide file tree
Showing 22 changed files with 654 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/generator/default/dbmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
*/
abstract class <?= $model->getClassName() ?> extends \yii\db\ActiveRecord
{
<?php if($scenarios = $model->getScenarios()):
foreach($scenarios as $scenario): ?>
<?php if ($scenarios = $model->getScenarios()):
foreach ($scenarios as $scenario): ?>
/**
*<?= $scenario['description'] ?>

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

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

return [
<?php foreach($scenarios as $scenario): ?>
<?php foreach ($scenarios as $scenario): ?>
self::<?= $scenario['const'] ?> => $default,
<?php endforeach; ?>
/**
Expand Down
8 changes: 1 addition & 7 deletions src/lib/generators/ModelsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ public function generate():CodeFiles
)
));
if ($this->config->generateModelFaker) {
$deps = []; # list of all models that this model is dependent on
foreach ($model->hasOneRelations as $key => $hasOneRelation) {
$deps[] = $model->hasOneRelations[$key]->getClassName();
}
$deps = array_unique($deps);

$this->files->add(new CodeFile(
Yii::getAlias("$fakerPath/{$className}Faker.php"),
$this->config->render(
Expand All @@ -86,7 +80,7 @@ public function generate():CodeFiles
'model' => $model,
'modelNamespace' => $this->config->modelNamespace,
'namespace' => $this->config->fakerNamespace,
'deps' => $deps,
'deps' => $model->fakerDependentModels(),
]
)
));
Expand Down
18 changes: 17 additions & 1 deletion src/lib/items/DbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use yii\base\BaseObject;
use yii\db\ColumnSchema;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
use yii\helpers\VarDumper;
use function array_filter;
use function array_map;
Expand Down Expand Up @@ -317,4 +316,21 @@ public function getModelClassDescription(): string
}
return FormatHelper::getFormattedDescription($this->description);
}

/**
* Return array of models that this models depends on exclusively used in faker.
* Models with `x-faker: false` or with self-reference are excluded
*/
public function fakerDependentModels(): array
{
$result = [];
foreach ($this->attributes as $attribute) {
if ($attribute->reference && $attribute->fakerStub) {
if ($this->name !== $attribute->reference) { # exclude self-referenced models
$result[] = $attribute->reference;
}
}
}
return array_unique($result);
}
}
18 changes: 8 additions & 10 deletions src/lib/openapi/PropertySchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@

namespace cebe\yii2openapi\lib\openapi;

use yii\db\ColumnSchema;
use cebe\yii2openapi\generator\ApiGenerator;
use yii\db\mysql\Schema as MySqlSchema;
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
use yii\db\pgsql\Schema as PgSqlSchema;
use cebe\yii2openapi\lib\items\Attribute;
use yii\base\NotSupportedException;
use BadMethodCallException;
use cebe\openapi\ReferenceContext;
use cebe\openapi\spec\Reference;
use cebe\openapi\SpecObjectInterface;
use cebe\yii2openapi\generator\ApiGenerator;
use cebe\yii2openapi\lib\CustomSpecAttr;
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
use cebe\yii2openapi\lib\traits\ForeignKeyConstraints;
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
use Throwable;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ColumnSchema;
use yii\db\mysql\Schema as MySqlSchema;
use yii\db\pgsql\Schema as PgSqlSchema;
use yii\db\Schema as YiiDbSchema;
use yii\helpers\Inflector;
use yii\helpers\Json;
use yii\helpers\StringHelper;
use yii\helpers\VarDumper;
use function is_int;
use function strpos;
use cebe\yii2openapi\lib\traits\ForeignKeyConstraints;

class PropertySchema
{
Expand All @@ -49,7 +47,7 @@ class PropertySchema
/**
* @var null|bool|string
* If `false`, no faker will be generated in faker model
* See more about usage in README.md file present in root directory of the project
* See more about usage in README.md file present in root directory of this library
*/
public $xFaker;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static function dependentOn()
{
return [
// just model class names
'Invoice',

];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.yml',
'generateUrls' => false,
'generateModels' => true,
'excludeModels' => [
'Error',
],
'generateControllers' => false,
'generateMigrations' => false,
'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
];

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
openapi: 3.0.3

info:
title: 'Bug: dependentOn: allOf with "x-faker: false" #52'
version: 1.0.0

components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
Fruit:
type: object
properties:
id:
type: integer
name:
type: string
Animal:
type: object
properties:
id:
type: integer
name:
type: string
Invoice:
title: Invoice
x-table: invoices
type: object
properties:
id:
type: integer
reference_invoice:
allOf:
- $ref: '#/components/schemas/Invoice'
- x-faker: false
- description: This field is only set on invoices of type "cancellation_invoice"
reference_invoice_2:
allOf:
- $ref: '#/components/schemas/Invoice'
- x-faker: true
user:
$ref: '#/components/schemas/User'
user_2:
allOf:
- $ref: '#/components/schemas/User'
- x-faker: false
fruit:
$ref: '#/components/schemas/Fruit'
animal:
allOf:
- $ref: '#/components/schemas/Animal'
- x-faker: false

paths:
'/':
get:
responses:
'200':
description: OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Animal extends \app\models\base\Animal
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace app\models;

use Faker\UniqueGenerator;

/**
* Fake data generator for Animal
* @method static Animal makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Animal saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Animal[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
* @method static Animal[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
*/
class AnimalFaker extends BaseModelFaker
{

/**
* @param array|callable $attributes
* @return Animal|\yii\db\ActiveRecord
* @example
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
* $model->scenario = 'create';
* $model->author_id = 1;
* return $model;
* });
**/
public function generateModel($attributes = [])
{
$faker = $this->faker;
$uniqueFaker = $this->uniqueFaker;
$model = new Animal();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->name = $faker->sentence;
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
} else {
$model = $attributes($model, $faker, $uniqueFaker);
}
return $model;
}
}
Loading

0 comments on commit c52db9c

Please sign in to comment.