Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 6d027c6

Browse files
authored
Resolve multiple PRs (#140)
Resolve - Prevent _id suffix in column name on reference properties Fix #90 PR in fork: SOHELAHMED7#21 --- Resolve: Running phpunit second time fails a tests Fix: #139 PR in fork: SOHELAHMED7#20 --- Resolve: If data type is not changed then still migrations are generated for timestamp in MySQL Fix #143 PR in fork: SOHELAHMED7#24 --- Resolve: Model name generated more than once in Faker Fix #148 PR in fork: SOHELAHMED7#25 --- Resolve: Wrong migration for Pgsql is generated for string/varchar datatype Fix #149 PR in fork: SOHELAHMED7#26 --- Resolve - PathAutoCompletion causes timeouts on large projects Fix #145 PR in fork: SOHELAHMED7#27 --- Resolve - nullable false should put attribute in required section in model validation rules Fix #153 PR in fork: SOHELAHMED7#28
2 parents c9fc11b + 1d168f9 commit 6d027c6

File tree

99 files changed

+1777
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1777
-167
lines changed

CONTRIBUTING.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ To apply multiple migration with one command:
4444
./yii migrate-pgsql/down --interactive=0 4
4545
```
4646

47+
PHPUnit run only one test by regex
48+
----------------------------------
49+
50+
If a PHPUnit test file have 2 test method with names like `testEdit()` and `testEditExpression()` then by running `./vendor/bin/phpunit --filter XDbDefaultExpressionTest::testEdit` both tests will run. In order to run only one test `testEdit()`, run below command:
51+
52+
```bash
53+
./vendor/bin/phpunit --filter '/XDbDefaultExpressionTest::testEdit$/'
54+
```
55+
4756

4857
Switching PHP versions
4958
----------------------
@@ -84,5 +93,60 @@ with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies
8493
with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans
8594
```
8695

96+
Issues and solutions
97+
--------------------
98+
99+
#### Issue when switching PHP version as mentioned above
100+
101+
```
102+
root@f65bb59c3289:/app# ./vendor/bin/phpunit
103+
104+
Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.1.0". You are running 7.4.33. in /app/vendor/composer/platform_check.php on line 24
105+
```
106+
107+
#### Solution
108+
109+
```bash
110+
sudo rm -rf vendor
111+
composer update
112+
```
113+
87114

115+
## Use PR of your own fork of this library in your project to check new changes
116+
117+
Say you have a fork of this library at https://github.com/SOHELAHMED7/yii2-openapi
118+
119+
You implemented new changes or fixed bugs and created PR on GitHub. It is not yet merged in upstream master branch.
120+
121+
You wanted to check this new changes in your own project which is using this lib cebe/yii2-openapi.
122+
123+
You can accomplish it by:
124+
125+
126+
Add below to composer.json of your project file
127+
128+
129+
```json
130+
"repositories": [
131+
{
132+
"type": "composer",
133+
"url": "https://asset-packagist.org"
134+
},
135+
{
136+
"type": "vcs",
137+
"url": "http://github.com/SOHELAHMED7/yii2-openapi"
138+
}
139+
]
140+
```
141+
142+
Then lets say you have PR https://github.com/SOHELAHMED7/yii2-openapi/pull/24.
143+
144+
And branch name is `143-if-data-type-is-not-changed-then-still-migrations-are-generated-for-timestamp-in-mysql`.
145+
146+
Run below command:
147+
148+
```bash
149+
composer require cebe/yii2-openapi:dev-143-if-data-type-is-not-changed-then-still-migrations-are-generated-for-timestamp-in-mysql
150+
```
88151

152+
Ensure to use upstream package name `cebe/yii2-openapi` instead of your fork (`sohelahmed7/yii2-openapi`) in composer command. And prefix branch name by `dev-`

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ installdocker:
5555
docker-compose run --user=$(UID) --rm php composer install && chmod +x tests/yii
5656

5757
testdocker:
58-
docker-compose run --user=$(UID) --rm php sh -c 'vendor/bin/phpunit'
58+
docker-compose run --user=$(UID) --rm php sh -c 'vendor/bin/phpunit --repeat 3'
5959

6060
efs: clean_all up migrate # Everything From Scratch
6161

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,30 @@ Allow to set foreign key constraint in migrations for ON DELETE event of row in
249249

250250
Allow to set foreign key constraint in migrations for ON UPDATE event of row in database table. For example, see above section for `x-fk-on-delete`.
251251

252+
### `x-fk-column-name`
253+
254+
Provide custom column name in case of relationship column. Example:
255+
256+
```yaml
257+
components:
258+
schemas:
259+
Webhook:
260+
type: object
261+
description: example for x-fk-column-name
262+
properties:
263+
id:
264+
type: integer
265+
name:
266+
type: string
267+
user:
268+
$ref: '../openapi.yaml#/components/schemas/User' # this will automatically create `user_id` column
269+
redelivery_of:
270+
allOf:
271+
- $ref: '../openapi.yaml#/components/schemas/Delivery'
272+
# this will automatically create `redelivery_of_id` column, but to avoid that use below extension
273+
- x-fk-column-name: redelivery_of # this will create `redelivery_of` column instead of `redelivery_of_id`
274+
```
275+
252276
## Many-to-Many relation definition
253277
254278
There are two ways for define many-to-many relations:
@@ -386,7 +410,7 @@ It works on all 3 DB: MySQL, MariaDb and PgSQL.
386410
- three
387411
```
388412

389-
Note: Change in enum values are not very simple. For Mysql and Mariadb, migrations will be generated but in many cases custom modification in it are required. For Pgsql migrations for change in enum values will not be generated. It should be handled manually.
413+
Note: Changes in enum values are not very simple. For Mysql and Mariadb, migrations will be generated but in many cases custom modification in it are required. For Pgsql migrations for change in enum values will not be generated. It should be handled manually.
390414

391415
## Handling of `numeric` (#numeric, #MariaDb)
392416

@@ -420,6 +444,28 @@ DB-Result = decimal(12,2)
420444
```
421445
DB-Result = decimal(10,2)
422446

447+
## Handling of `timestamp` database column data type
448+
449+
If field is defined as
450+
451+
```yaml
452+
created_at:
453+
type: string
454+
format: date-time # or datetime
455+
example: '2020-03-14T21:42:17Z'
456+
readOnly: true
457+
```
458+
459+
then database type selected will be `timestamp`. This is by design. If `datetime` data type is needed, use `x-db-type` as
460+
461+
```yaml
462+
created_at:
463+
type: string
464+
format: date-time # or datetime
465+
example: '2020-03-14T21:42:17Z'
466+
x-db-type: datetime
467+
readOnly: true
468+
```
423469

424470
## Assumptions
425471

src/generator/ApiGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ public function hints()
372372
*/
373373
public function autoCompleteData()
374374
{
375-
return (new PathAutoCompletion())->complete();
375+
$config = $this->makeConfig();
376+
return (new PathAutoCompletion($config))->complete();
376377
}
377378

378379
/**

src/generator/default/faker.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @var \cebe\yii2openapi\lib\items\DbModel $model
44
* @var string $namespace
55
* @var string $modelNamespace
6+
* @var array $deps list of all models that this model is dependent on
67
**/
78

89
$modelClass = ($modelNamespace !== $namespace ? '\\'.trim($modelNamespace, '\\').'\\' : '').$model->getClassName();
@@ -68,8 +69,8 @@ public static function dependentOn()
6869
{
6970
return [
7071
// just model class names
71-
<?php foreach ($model->hasOneRelations as $key => $hasOneRelation): ?>
72-
<?php echo \yii\helpers\VarDumper::export($model->hasOneRelations[$key]->getClassName()).','.PHP_EOL ?>
72+
<?php foreach ($deps as $dep): ?>
73+
<?php echo \yii\helpers\VarDumper::export($dep).','.PHP_EOL ?>
7374
<?php endforeach; ?>
7475

7576
];

src/lib/AttributeResolver.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,17 @@ public function resolve():DbModel
9898
/** @var $property \cebe\yii2openapi\lib\openapi\PropertySchema */
9999

100100
$isRequired = $this->schema->isRequiredProperty($property->getName());
101+
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
102+
if ($nullableValue === false) { // see docs in README regarding NOT NULL, required and nullable
103+
$isRequired = true;
104+
}
105+
101106
if ($this->isJunctionSchema) {
102107
$this->resolveJunctionTableProperty($property, $isRequired);
103108
} elseif ($this->hasMany2Many) {
104109
$this->resolveHasMany2ManyTableProperty($property, $isRequired);
105110
} else {
106-
$this->resolveProperty($property, $isRequired);
111+
$this->resolveProperty($property, $isRequired, $nullableValue);
107112
}
108113
}
109114
return Yii::createObject(DbModel::class, [
@@ -141,7 +146,8 @@ protected function resolveJunctionTableProperty(PropertySchema $property, bool $
141146
->setIsPrimary($property->isPrimaryKey())
142147
->asReference($junkAttribute['relatedClassName'])
143148
->setPhpType($junkAttribute['phpType'])
144-
->setDbType($junkAttribute['dbType']);
149+
->setDbType($junkAttribute['dbType'])
150+
->setForeignKeyColumnName($property->fkColName);
145151
$relation = Yii::createObject(AttributeRelation::class, [
146152
$property->getName(),
147153
$junkAttribute['relatedTableName'],
@@ -201,20 +207,28 @@ protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bo
201207
/**
202208
* @param \cebe\yii2openapi\lib\openapi\PropertySchema $property
203209
* @param bool $isRequired
210+
* @param bool|null|string $nullableValue if string then its value will be only constant `ARG_ABSENT`. Default `null` is avoided because it can be in passed value in method call
204211
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
205212
* @throws \yii\base\InvalidConfigException
206213
*/
207-
protected function resolveProperty(PropertySchema $property, bool $isRequired):void
208-
{
214+
protected function resolveProperty(
215+
PropertySchema $property,
216+
bool $isRequired,
217+
$nullableValue = 'ARG_ABSENT'
218+
):void {
219+
if ($nullableValue === 'ARG_ABSENT') {
220+
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
221+
}
209222
$attribute = Yii::createObject(Attribute::class, [$property->getName()]);
210223
$attribute->setRequired($isRequired)
211224
->setDescription($property->getAttr('description', ''))
212225
->setReadOnly($property->isReadonly())
213226
->setDefault($property->guessDefault())
214227
->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE))
215228
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
216-
->setNullable($property->getProperty()->getSerializableData()->nullable ?? null)
217-
->setIsPrimary($property->isPrimaryKey());
229+
->setNullable($nullableValue)
230+
->setIsPrimary($property->isPrimaryKey())
231+
->setForeignKeyColumnName($property->fkColName);
218232
if ($property->isReference()) {
219233
if ($property->isVirtual()) {
220234
throw new InvalidDefinitionException('References not supported for virtual attributes');

src/lib/ColumnToCode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class ColumnToCode
110110
* Its possible values are:
111111
* `'FIRST'`
112112
* `'AFTER <columnName>'` e.g. AFTER first_name
113-
* `null` means append new column at the end
113+
* `null` (default) means append new column at the end
114114
*/
115115
private $position;
116116

src/lib/CustomSpecAttr.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ class CustomSpecAttr
3535
*/
3636
public const FK_ON_DELETE = 'x-fk-on-delete';
3737
public const FK_ON_UPDATE = 'x-fk-on-update';
38+
39+
/**
40+
* Foreign key column name. See README for usage docs
41+
*/
42+
public const FK_COLUMN_NAME = 'x-fk-column-name';
3843
}

src/lib/FakerStubResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ private function fakeForFloat(?int $min, ?int $max):?string
212212

213213
private function fakeForArray():string
214214
{
215+
if ($this->attribute->required) {
216+
return '["a" => "b"]';
217+
}
215218
return '[]';
216219
}
217220
}

0 commit comments

Comments
 (0)