Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve: Bug: rules() "required" is generated before "*_default" #22 #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/lib/ValidationRulesBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use cebe\yii2openapi\lib\items\Attribute;
use cebe\yii2openapi\lib\items\DbModel;
use cebe\yii2openapi\lib\items\ValidationRule;
use yii\helpers\VarDumper;
use yii\validators\DateValidator;
use function count;
use function implode;
use function in_array;
Expand Down Expand Up @@ -53,9 +51,6 @@ public function build():array
$this->rules['trim'] = new ValidationRule($this->typeScope['trim'], 'trim');
}

if (!empty($this->typeScope['required'])) {
$this->rules['required'] = new ValidationRule($this->typeScope['required'], 'required');
}
if (!empty($this->typeScope['ref'])) {
$this->addExistRules($this->typeScope['ref']);
}
Expand All @@ -77,6 +72,9 @@ public function build():array
if (!empty($this->typeScope['safe'])) {
$this->rules['safe'] = new ValidationRule($this->typeScope['safe'], 'safe');
}
if (!empty($this->typeScope['required'])) {
$this->rules['required'] = new ValidationRule($this->typeScope['required'], 'required');
}
return $this->rules;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace tests;

use cebe\yii2openapi\generator\ApiGenerator;
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
use Yii;
use yii\di\Container;
use yii\db\mysql\Schema as MySqlSchema;
use yii\db\pgsql\Schema as PgSqlSchema;
use \SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
use yii\helpers\{ArrayHelper, VarDumper, StringHelper, Console};
use yii\di\Container;
use yii\helpers\{ArrayHelper, StringHelper};
use yii\helpers\FileHelper;

class DbTestCase extends \PHPUnit\Framework\TestCase
Expand Down Expand Up @@ -101,7 +101,6 @@ protected function compareFiles(array $actual, string $testFile)
foreach ($actual as $file) {
$expectedFile = str_replace('@app', substr($testFile, 0, -4), $file);
$actualFile = str_replace('@app', Yii::getAlias('@app'), $file);
// exec('cp '.$actualFile.' '.$expectedFile);
$this->checkFiles([$actualFile], [$expectedFile]);
}
}
Expand All @@ -127,6 +126,7 @@ protected function checkFiles(array $actual, array $expected)
);
}

// exec('cp '.$file.' '.$expectedFilePath);
$this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath \n\n cp $file $expectedFilePath \n\n ");
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/blog/models/base/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public function rules()
{
return [
'trim' => [['title'], 'trim'],
'required' => [['title', 'active'], 'required'],
'title_unique' => [['title'], 'unique'],
'title_string' => [['title'], 'string', 'max' => 255],
'active_boolean' => [['active'], 'boolean'],
'active_default' => [['active'], 'default', 'value' => false],
'required' => [['title', 'active'], 'required'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/blog/models/base/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function rules()
{
return [
'trim' => [['post_id'], 'trim'],
'required' => [['post_id', 'author_id', 'message', 'created_at'], 'required'],
'post_id_string' => [['post_id'], 'string', 'max' => 128],
'post_id_exist' => [['post_id'], 'exist', 'targetRelation' => 'Post'],
'author_id_integer' => [['author_id'], 'integer'],
Expand All @@ -35,6 +34,7 @@ public function rules()
'meta_data_default' => [['meta_data'], 'default', 'value' => []],
'created_at_integer' => [['created_at'], 'integer'],
'safe' => [['message', 'meta_data'], 'safe'],
'required' => [['post_id', 'author_id', 'message', 'created_at'], 'required'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/blog/models/base/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function rules()
{
return [
'trim' => [['title', 'slug', 'created_at'], 'trim'],
'required' => [['title', 'category_id', 'active'], 'required'],
'category_id_integer' => [['category_id'], 'integer'],
'category_id_exist' => [['category_id'], 'exist', 'targetRelation' => 'Category'],
'created_by_id_integer' => [['created_by_id'], 'integer'],
Expand All @@ -40,6 +39,7 @@ public function rules()
'active_boolean' => [['active'], 'boolean'],
'active_default' => [['active'], 'default', 'value' => false],
'created_at_date' => [['created_at'], 'date', 'format' => 'php:Y-m-d'],
'required' => [['title', 'category_id', 'active'], 'required'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/blog/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function rules()
{
return [
'trim' => [['username', 'email', 'password', 'role', 'created_at'], 'trim'],
'required' => [['username', 'email', 'password'], 'required'],
'username_unique' => [['username'], 'unique'],
'email_unique' => [['email'], 'unique'],
'username_string' => [['username'], 'string', 'max' => 200],
Expand All @@ -37,6 +36,7 @@ public function rules()
'flags_integer' => [['flags'], 'integer'],
'flags_default' => [['flags'], 'default', 'value' => 0],
'created_at_datetime' => [['created_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'required' => [['username', 'email', 'password'], 'required'],
];
}
}
2 changes: 1 addition & 1 deletion tests/specs/blog_v2/models/base/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function rules()
{
return [
'trim' => [['title', 'cover'], 'trim'],
'required' => [['title', 'cover', 'active'], 'required'],
'title_string' => [['title'], 'string', 'max' => 100],
'cover_string' => [['cover'], 'string'],
'active_boolean' => [['active'], 'boolean'],
'required' => [['title', 'cover', 'active'], 'required'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/blog_v2/models/base/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function rules()
{
return [
'trim' => [['message', 'meta_data', 'created_at'], 'trim'],
'required' => [['post_id', 'message', 'created_at'], 'required'],
'post_id_integer' => [['post_id'], 'integer'],
'post_id_exist' => [['post_id'], 'exist', 'targetRelation' => 'Post'],
'user_id_integer' => [['user_id'], 'integer'],
Expand All @@ -35,6 +34,7 @@ public function rules()
'meta_data_string' => [['meta_data'], 'string', 'min' => 1, 'max' => 300],
'meta_data_default' => [['meta_data'], 'default', 'value' => ''],
'created_at_datetime' => [['created_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'required' => [['post_id', 'message', 'created_at'], 'required'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/blog_v2/models/base/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function rules()
{
return [
'trim' => [['title', 'slug', 'created_at'], 'trim'],
'required' => [['title', 'category_id', 'active'], 'required'],
'category_id_integer' => [['category_id'], 'integer'],
'category_id_exist' => [['category_id'], 'exist', 'targetRelation' => 'Category'],
'created_by_id_integer' => [['created_by_id'], 'integer'],
Expand All @@ -46,6 +45,7 @@ public function rules()
'lang_default' => [['lang'], 'default', 'value' => 'ru'],
'active_boolean' => [['active'], 'boolean'],
'created_at_date' => [['created_at'], 'date', 'format' => 'php:Y-m-d'],
'required' => [['title', 'category_id', 'active'], 'required'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/blog_v2/models/base/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name', 'lang'], 'required'],
'name_unique' => [['name'], 'unique'],
'name_string' => [['name'], 'string', 'max' => 100],
'lang_string' => [['lang'], 'string'],
'lang_in' => [['lang'], 'in', 'range' => [
'ru',
'eng',
]],
'required' => [['name', 'lang'], 'required'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/blog_v2/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function rules()
{
return [
'trim' => [['login', 'email', 'password', 'created_at'], 'trim'],
'required' => [['login', 'email', 'password'], 'required'],
'login_unique' => [['login'], 'unique'],
'email_unique' => [['email'], 'unique'],
'login_string' => [['login'], 'string'],
Expand All @@ -41,6 +40,7 @@ public function rules()
'flags_integer' => [['flags'], 'integer'],
'flags_default' => [['flags'], 'default', 'value' => 0],
'created_at_datetime' => [['created_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
'required' => [['login', 'email', 'password'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string', 'max' => 255],
'required' => [['name'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string', 'max' => 255],
'required' => [['name'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string', 'max' => 255],
'required' => [['name'], 'required'],
];
}
}
2 changes: 1 addition & 1 deletion tests/specs/fk_col_name/app/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string'],
'required' => [['name'], 'required'],
];
}
}
2 changes: 1 addition & 1 deletion tests/specs/fk_col_name_index/app/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string'],
'required' => [['name'], 'required'],
];
}
}
2 changes: 1 addition & 1 deletion tests/specs/id_not_in_rules/app/models/base/Fruit.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string'],
'required' => [['name'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static function tableName()
public function rules()
{
return [
'required' => [['billing_factor'], 'required'],
'billing_factor_integer' => [['billing_factor'], 'integer'],
'billing_factor_default' => [['billing_factor'], 'default', 'value' => 100],
'required' => [['billing_factor'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string', 'max' => 128],
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
'paymentMethodName_in' => [['paymentMethodName'], 'in', 'range' => [
'card',
'cash',
'ewallet',
]],
'required' => [['name'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function rules()
{
return [
'trim' => [['nickname'], 'trim'],
'required' => [['mailing_id'], 'required'],
'mailing_id_integer' => [['mailing_id'], 'integer'],
'mailing_id_exist' => [['mailing_id'], 'exist', 'targetRelation' => 'Mailing'],
'active_boolean' => [['active'], 'boolean'],
'active_default' => [['active'], 'default', 'value' => false],
'nickname_string' => [['nickname'], 'string'],
'required' => [['mailing_id'], 'required'],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function rules()
{
return [
'trim' => [['name', 'paymentMethodName'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string', 'max' => 128],
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
'required' => [['name'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function rules()
{
return [
'trim' => [['name', 'paymentMethodName'], 'trim'],
'required' => [['name'], 'required'],
'name_string' => [['name'], 'string', 'max' => 128],
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
'required' => [['name'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function rules()
{
return [
'trim' => [['nickname'], 'trim'],
'required' => [['account_id'], 'required'],
'account_id_integer' => [['account_id'], 'integer'],
'account_id_exist' => [['account_id'], 'exist', 'targetRelation' => 'Account'],
'active_boolean' => [['active'], 'boolean'],
'active_default' => [['active'], 'default', 'value' => false],
'nickname_string' => [['nickname'], 'string'],
'required' => [['account_id'], 'required'],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function rules()
{
return [
'trim' => [['name'], 'trim'],
'required' => [['name'], 'required'],
'name_unique' => [['name'], 'unique'],
'name_string' => [['name'], 'string', 'max' => 150],
'required' => [['name'], 'required'],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.yaml',
'generateUrls' => false,
'generateModels' => true,
'excludeModels' => [
'Error',
],
'generateControllers' => false,
'generateMigrations' => true,
'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,35 @@
openapi: 3.0.3

info:
title: 'Bug: rules() "required" is generated before "*_default" #22'
version: 1.0.0

components:
schemas:
Account:
description: Account
type: object
required:
- id
- name
- verified
properties:
id:
type: integer
readOnly: true
name:
description: account name
type: string
maxLength: 128
paymentMethodName:
type: string
verified:
type: boolean
default: false

paths:
'/account':
get:
responses:
'200':
description: Account info
Loading
Loading