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

Resolve multiple PRs #140

Merged
merged 55 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
cae3e10
Initial commit to create PR
SOHELAHMED7 Mar 27, 2023
cef2b0f
WIP
SOHELAHMED7 Mar 27, 2023
af52c38
Initial commit to create PR
SOHELAHMED7 Mar 27, 2023
243f841
Add implementation and tests (WIP)
SOHELAHMED7 Mar 27, 2023
c9e8b85
Add tests
SOHELAHMED7 Mar 28, 2023
ec80618
Merge pull request #21 from SOHELAHMED7/90-prevent-id-suffix-in-colum…
SOHELAHMED7 Mar 28, 2023
6ecba40
Merge branch 'cebe:master' into master
SOHELAHMED7 Mar 28, 2023
5757d55
Merge branches 'master' and '139-running-phpunit-second-time-fails-a-…
SOHELAHMED7 Mar 28, 2023
b5e1474
Attempt to fix failing test on Github CI
SOHELAHMED7 Mar 28, 2023
c79ef94
Attempt to fix failing test on Github CI - 2
SOHELAHMED7 Mar 28, 2023
93b99e8
Enhance docs
SOHELAHMED7 Mar 28, 2023
0cd601b
Remove unwanted comments
SOHELAHMED7 Mar 28, 2023
86321c8
Remove stale DB table
SOHELAHMED7 Mar 28, 2023
3eea6d1
WIP - drop DB approach
SOHELAHMED7 Mar 28, 2023
d8791d8
Fix issue
SOHELAHMED7 Mar 29, 2023
5a10635
Fix issue - 2
SOHELAHMED7 Mar 29, 2023
ef53fed
Remove unnecessary code
SOHELAHMED7 Mar 29, 2023
b4b659b
Detect the issue - WIP
SOHELAHMED7 Mar 29, 2023
3449b15
Make running of tests more stronger
SOHELAHMED7 Mar 29, 2023
21d400b
Change Mysql to Pgsql to fix the issue
SOHELAHMED7 Mar 30, 2023
1c47fa3
Polish PR
SOHELAHMED7 Mar 31, 2023
33d31b2
Merge pull request #20 from SOHELAHMED7/139-running-phpunit-second-ti…
SOHELAHMED7 Mar 31, 2023
005ce44
Fix merge conflict
SOHELAHMED7 Mar 31, 2023
71300ca
Merge branch 'master' of https://github.com/cebe/yii2-openapi into ce…
SOHELAHMED7 Apr 13, 2023
7e5c509
Merge branch 'cebe-master'
SOHELAHMED7 Apr 13, 2023
6c722a2
Initial commit
SOHELAHMED7 May 10, 2023
807502d
WIP
SOHELAHMED7 May 12, 2023
834ebdc
Fix typo
SOHELAHMED7 May 29, 2023
74f7022
WIP
SOHELAHMED7 May 29, 2023
2a5546d
Fix issue https://github.com/cebe/yii2-openapi/issues/143
SOHELAHMED7 May 30, 2023
2b63f8f
Enhance test
SOHELAHMED7 May 30, 2023
016c985
Add docs
SOHELAHMED7 May 31, 2023
c76feaa
Add docs
SOHELAHMED7 May 31, 2023
7aaca2a
Merge pull request #24 from SOHELAHMED7/143-if-data-type-is-not-chang…
SOHELAHMED7 May 31, 2023
a75f579
Commit to create PR
SOHELAHMED7 May 31, 2023
82f71c6
Fix mysql test issue
SOHELAHMED7 May 31, 2023
bbcf5c8
Revert
SOHELAHMED7 May 31, 2023
fd5c960
Revert
SOHELAHMED7 May 31, 2023
b515f7c
Fix issue https://github.com/cebe/yii2-openapi/issues/148
SOHELAHMED7 May 31, 2023
59373ad
Fix style
SOHELAHMED7 May 31, 2023
e866b3a
Merge pull request #25 from SOHELAHMED7/148-model-name-generated-more…
SOHELAHMED7 May 31, 2023
314d4a8
WIP
SOHELAHMED7 May 31, 2023
f564b16
Fix https://github.com/cebe/yii2-openapi/issues/149
SOHELAHMED7 Jun 1, 2023
c8e3b43
Merge pull request #26 from SOHELAHMED7/149-wrong-migration-for-pgsql…
SOHELAHMED7 Jun 1, 2023
a8a2ef6
Initial commit of this branch to create PR
SOHELAHMED7 Jun 2, 2023
8f214e9
Refactor, adds cache usage and more
SOHELAHMED7 Jun 7, 2023
227033d
Fix failing test
SOHELAHMED7 Jun 8, 2023
3feadaf
Fix issue for PHP 7.3
SOHELAHMED7 Jun 8, 2023
d50d863
Add tests
SOHELAHMED7 Jun 12, 2023
eeea676
Merge pull request #27 from SOHELAHMED7/145-path-auto-completion-caus…
SOHELAHMED7 Jun 12, 2023
36de802
Create PR
SOHELAHMED7 Jun 13, 2023
daa9a99
Add failing test
SOHELAHMED7 Jun 13, 2023
4404868
Fix this issue
SOHELAHMED7 Jun 14, 2023
1da94f0
Fix style and complete test
SOHELAHMED7 Jun 14, 2023
1d168f9
Merge pull request #28 from SOHELAHMED7/153-nullable-false-should-put…
SOHELAHMED7 Jun 14, 2023
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
64 changes: 64 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ To apply multiple migration with one command:
./yii migrate-pgsql/down --interactive=0 4
```

PHPUnit run only one test by regex
----------------------------------

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:

```bash
./vendor/bin/phpunit --filter '/XDbDefaultExpressionTest::testEdit$/'
```


Switching PHP versions
----------------------
Expand Down Expand Up @@ -84,5 +93,60 @@ with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies
with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans
```

Issues and solutions
--------------------

#### Issue when switching PHP version as mentioned above

```
root@f65bb59c3289:/app# ./vendor/bin/phpunit

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
```

#### Solution

```bash
sudo rm -rf vendor
composer update
```


## Use PR of your own fork of this library in your project to check new changes

Say you have a fork of this library at https://github.com/SOHELAHMED7/yii2-openapi

You implemented new changes or fixed bugs and created PR on GitHub. It is not yet merged in upstream master branch.

You wanted to check this new changes in your own project which is using this lib cebe/yii2-openapi.

You can accomplish it by:


Add below to composer.json of your project file


```json
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
},
{
"type": "vcs",
"url": "http://github.com/SOHELAHMED7/yii2-openapi"
}
]
```

Then lets say you have PR https://github.com/SOHELAHMED7/yii2-openapi/pull/24.

And branch name is `143-if-data-type-is-not-changed-then-still-migrations-are-generated-for-timestamp-in-mysql`.

Run below command:

```bash
composer require cebe/yii2-openapi:dev-143-if-data-type-is-not-changed-then-still-migrations-are-generated-for-timestamp-in-mysql
```

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-`
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ installdocker:
docker-compose run --user=$(UID) --rm php composer install && chmod +x tests/yii

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

efs: clean_all up migrate # Everything From Scratch

Expand Down
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ Allow to set foreign key constraint in migrations for ON DELETE event of row in

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`.

### `x-fk-column-name`

Provide custom column name in case of relationship column. Example:

```yaml
components:
schemas:
Webhook:
type: object
description: example for x-fk-column-name
properties:
id:
type: integer
name:
type: string
user:
$ref: '../openapi.yaml#/components/schemas/User' # this will automatically create `user_id` column
redelivery_of:
allOf:
- $ref: '../openapi.yaml#/components/schemas/Delivery'
# this will automatically create `redelivery_of_id` column, but to avoid that use below extension
- x-fk-column-name: redelivery_of # this will create `redelivery_of` column instead of `redelivery_of_id`
```

## Many-to-Many relation definition

There are two ways for define many-to-many relations:
Expand Down Expand Up @@ -386,7 +410,7 @@ It works on all 3 DB: MySQL, MariaDb and PgSQL.
- three
```

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.
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.

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

Expand Down Expand Up @@ -420,6 +444,28 @@ DB-Result = decimal(12,2)
```
DB-Result = decimal(10,2)

## Handling of `timestamp` database column data type

If field is defined as

```yaml
created_at:
type: string
format: date-time # or datetime
example: '2020-03-14T21:42:17Z'
readOnly: true
```

then database type selected will be `timestamp`. This is by design. If `datetime` data type is needed, use `x-db-type` as

```yaml
created_at:
type: string
format: date-time # or datetime
example: '2020-03-14T21:42:17Z'
x-db-type: datetime
readOnly: true
```

## Assumptions

Expand Down
3 changes: 2 additions & 1 deletion src/generator/ApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ public function hints()
*/
public function autoCompleteData()
{
return (new PathAutoCompletion())->complete();
$config = $this->makeConfig();
return (new PathAutoCompletion($config))->complete();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/generator/default/faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @var \cebe\yii2openapi\lib\items\DbModel $model
* @var string $namespace
* @var string $modelNamespace
* @var array $deps list of all models that this model is dependent on
**/

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

];
Expand Down
26 changes: 20 additions & 6 deletions src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ public function resolve():DbModel
/** @var $property \cebe\yii2openapi\lib\openapi\PropertySchema */

$isRequired = $this->schema->isRequiredProperty($property->getName());
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
if ($nullableValue === false) { // see docs in README regarding NOT NULL, required and nullable
$isRequired = true;
}

if ($this->isJunctionSchema) {
$this->resolveJunctionTableProperty($property, $isRequired);
} elseif ($this->hasMany2Many) {
$this->resolveHasMany2ManyTableProperty($property, $isRequired);
} else {
$this->resolveProperty($property, $isRequired);
$this->resolveProperty($property, $isRequired, $nullableValue);
}
}
return Yii::createObject(DbModel::class, [
Expand Down Expand Up @@ -141,7 +146,8 @@ protected function resolveJunctionTableProperty(PropertySchema $property, bool $
->setIsPrimary($property->isPrimaryKey())
->asReference($junkAttribute['relatedClassName'])
->setPhpType($junkAttribute['phpType'])
->setDbType($junkAttribute['dbType']);
->setDbType($junkAttribute['dbType'])
->setForeignKeyColumnName($property->fkColName);
$relation = Yii::createObject(AttributeRelation::class, [
$property->getName(),
$junkAttribute['relatedTableName'],
Expand Down Expand Up @@ -201,20 +207,28 @@ protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bo
/**
* @param \cebe\yii2openapi\lib\openapi\PropertySchema $property
* @param bool $isRequired
* @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
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
* @throws \yii\base\InvalidConfigException
*/
protected function resolveProperty(PropertySchema $property, bool $isRequired):void
{
protected function resolveProperty(
PropertySchema $property,
bool $isRequired,
$nullableValue = 'ARG_ABSENT'
):void {
if ($nullableValue === 'ARG_ABSENT') {
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
}
$attribute = Yii::createObject(Attribute::class, [$property->getName()]);
$attribute->setRequired($isRequired)
->setDescription($property->getAttr('description', ''))
->setReadOnly($property->isReadonly())
->setDefault($property->guessDefault())
->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE))
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
->setNullable($property->getProperty()->getSerializableData()->nullable ?? null)
->setIsPrimary($property->isPrimaryKey());
->setNullable($nullableValue)
->setIsPrimary($property->isPrimaryKey())
->setForeignKeyColumnName($property->fkColName);
if ($property->isReference()) {
if ($property->isVirtual()) {
throw new InvalidDefinitionException('References not supported for virtual attributes');
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ColumnToCode
* Its possible values are:
* `'FIRST'`
* `'AFTER <columnName>'` e.g. AFTER first_name
* `null` means append new column at the end
* `null` (default) means append new column at the end
*/
private $position;

Expand Down
5 changes: 5 additions & 0 deletions src/lib/CustomSpecAttr.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ class CustomSpecAttr
*/
public const FK_ON_DELETE = 'x-fk-on-delete';
public const FK_ON_UPDATE = 'x-fk-on-update';

/**
* Foreign key column name. See README for usage docs
*/
public const FK_COLUMN_NAME = 'x-fk-column-name';
}
3 changes: 3 additions & 0 deletions src/lib/FakerStubResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ private function fakeForFloat(?int $min, ?int $max):?string

private function fakeForArray():string
{
if ($this->attribute->required) {
return '["a" => "b"]';
}
return '[]';
}
}
Loading