Skip to content

Commit a79a3b1

Browse files
committed
Apply Ctrl + Alt + Shift + L of PHPStorm
1 parent 16862ad commit a79a3b1

File tree

91 files changed

+2080
-2001
lines changed

Some content is hidden

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

91 files changed

+2080
-2001
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ make cli
2121
# create new branch from master and Happy contributing!
2222
```
2323

24-
These commands are available to develop and check the tests. It is available inside the Docker container. To enter into bash shell of container, run `make cli` .
24+
These commands are available to develop and check the tests. It is available inside the Docker container. To enter into
25+
bash shell of container, run `make cli` .
2526

2627
```bash
2728
cd tests
@@ -49,17 +50,19 @@ To apply multiple migration with one command:
4950
PHPUnit run only one test by regex
5051
----------------------------------
5152

52-
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:
53+
If a PHPUnit test file have 2 test method with names like `testEdit()` and `testEditExpression()` then by
54+
running `./vendor/bin/phpunit --filter XDbDefaultExpressionTest::testEdit` both tests will run. In order to run only one
55+
test `testEdit()`, run below command:
5356

5457
```bash
5558
./vendor/bin/phpunit --filter '/XDbDefaultExpressionTest::testEdit$/'
5659
```
5760

58-
5961
Switching PHP versions
6062
----------------------
6163

62-
You can switch the PHP version of the docker runtime by changing the `PHP_VERSION` environment variable in the `.env` file.
64+
You can switch the PHP version of the docker runtime by changing the `PHP_VERSION` environment variable in the `.env`
65+
file.
6366

6467
If you have no `.env` file yet, create it by copying `.env.dist` to `.env`.
6568

@@ -113,7 +116,6 @@ sudo rm -rf vendor
113116
composer update
114117
```
115118

116-
117119
## Use PR of your own fork of this library in your project to check new changes
118120

119121
Say you have a fork of this library at https://github.com/SOHELAHMED7/yii2-openapi
@@ -124,10 +126,8 @@ You wanted to check this new changes in your own project which is using this lib
124126

125127
You can accomplish it by:
126128

127-
128129
Add below to composer.json of your project file
129130

130-
131131
```json
132132
"repositories": [
133133
{
@@ -151,4 +151,5 @@ Run below command:
151151
composer require cebe/yii2-openapi:dev-143-if-data-type-is-not-changed-then-still-migrations-are-generated-for-timestamp-in-mysql
152152
```
153153

154-
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-`
154+
Ensure to use upstream package name `cebe/yii2-openapi` instead of your fork (`sohelahmed7/yii2-openapi`) in composer
155+
command. And prefix branch name by `dev-`

README.md

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ Base on [Gii, the Yii Framework Code Generator](https://www.yiiframework.com/ext
1414

1515
A code generator for OpenAPI and Yii Framework based PHP API application.
1616

17-
Input: [OpenAPI 3.0 YAML or JSON](https://github.com/OAI/OpenAPI-Specification#the-openapi-specification) (via [cebe/php-openapi](https://github.com/php-openapi/php-openapi))
17+
Input: [OpenAPI 3.0 YAML or JSON](https://github.com/OAI/OpenAPI-Specification#the-openapi-specification) (
18+
via [cebe/php-openapi](https://github.com/php-openapi/php-openapi))
1819

1920
Output: Yii Framework Application with Controllers, Models, database schema
2021

21-
2222
## Features
2323

2424
Currently available features:
2525

26-
- Generate Path mappings, **Controllers** and Actions **for API Endpoints**. CRUD Endpoints are ready-to-use, other Endpoints are generated as abstract functions that need to be implemented
26+
- Generate Path mappings, **Controllers** and Actions **for API Endpoints**. CRUD Endpoints are ready-to-use, other
27+
Endpoints are generated as abstract functions that need to be implemented
2728
- Generate **Models** and validation based on OpenAPI Schema
2829
- Generate **Database Schema** from OpenAPI Schema
2930
- Generates **Database Migrations** for schema changes
@@ -33,7 +34,6 @@ Currently available features:
3334

3435
- PHP 7.1 or higher (works fine with PHP 8)
3536

36-
3737
## Install
3838

3939
composer require php-openapi/yii2-openapi:^2.0@beta
@@ -76,12 +76,15 @@ return $config;
7676

7777
To use the web generator, open `index.php?r=gii` and select the `REST API Generator`.
7878

79-
On console you can run the generator with `./yii gii/api --openApiPath=@app/openapi.yaml`. Where `@app/openapi.yaml` should be the absolute path to your OpenAPI spec file. This can be JSON as well as YAML (see also [php-openapi/php-openapi](https://github.com/php-openapi/php-openapi/) for supported formats).
80-
81-
Run `./yii gii/api --help` for all options. Example: Disable generation of migrations files `./yii gii/api --generateMigrations=0`
79+
On console you can run the generator with `./yii gii/api --openApiPath=@app/openapi.yaml`. Where `@app/openapi.yaml`
80+
should be the absolute path to your OpenAPI spec file. This can be JSON as well as YAML (see
81+
also [php-openapi/php-openapi](https://github.com/php-openapi/php-openapi/) for supported formats).
8282

83-
See [Petstore example](https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.0/petstore.yaml) for example OpenAPI spec.
83+
Run `./yii gii/api --help` for all options. Example: Disable generation of migrations
84+
files `./yii gii/api --generateMigrations=0`
8485

86+
See [Petstore example](https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.0/petstore.yaml) for example
87+
OpenAPI spec.
8588

8689
## OpenAPI extensions
8790

@@ -145,7 +148,7 @@ You can generate non-db model based on \yii\base\Model without migrations by set
145148

146149
### `x-pk`
147150

148-
Explicitly specify primary key name for table, if it is different from "id"
151+
Explicitly specify primary key name for table, if it is different from "id"
149152

150153
```yaml
151154
Post:
@@ -160,29 +163,33 @@ Explicitly specify primary key name for table, if it is different from "id"
160163

161164
### `x-db-type`
162165

163-
Explicitly specify the database type for a column. (MUST contain only real DB type! (`json`, `jsonb`, `uuid`, `varchar` etc.)).
166+
Explicitly specify the database type for a column. (MUST contain only real DB type! (`json`, `jsonb`, `uuid`, `varchar`
167+
etc.)).
164168
If `x-db-type` is set to `false`, property will be processed as virtual;
165169
It will be added in model as public property, but skipped for migrations generation.
166170

167171
Example values of `x-db-type` are:
168172

169-
- `false` (boolean false)
170-
- as string and its value can be like:
171-
- text
172-
- text[]
173-
- INTEGER PRIMARY KEY AUTO_INCREMENT
174-
- decimal(12,4)
175-
- json
176-
- varchar
177-
- VARCHAR
178-
- SMALLINT UNSIGNED ZEROFILL
179-
- MEDIUMINT(10) UNSIGNED ZEROFILL COMMENT "comment" (note the double quotes here)
173+
- `false` (boolean false)
174+
- as string and its value can be like:
175+
- text
176+
- text[]
177+
- INTEGER PRIMARY KEY AUTO_INCREMENT
178+
- decimal(12,4)
179+
- json
180+
- varchar
181+
- VARCHAR
182+
- SMALLINT UNSIGNED ZEROFILL
183+
- MEDIUMINT(10) UNSIGNED ZEROFILL COMMENT "comment" (note the double quotes here)
180184

181185
Such values are not allowed:
182-
- `int null default null after low_price` (null and default will be handled by `nullable` and `default` keys respectively)
183-
- MEDIUMINT(10) UNSIGNED ZEROFILL NULL DEFAULT '7' COMMENT 'comment' AFTER `seti`, ADD INDEX `t` (`w`)
184186

185-
If `enum` and `x-db-type` both are provided then for database column schema (migrations), only `x-db-type` will be considered ignoring `enum`.
187+
- `int null default null after low_price` (null and default will be handled by `nullable` and `default` keys
188+
respectively)
189+
- MEDIUMINT(10) UNSIGNED ZEROFILL NULL DEFAULT '7' COMMENT 'comment' AFTER `seti`, ADD INDEX `t` (`w`)
190+
191+
If `enum` and `x-db-type` both are provided then for database column schema (migrations), only `x-db-type` will be
192+
considered ignoring `enum`.
186193

187194
### `x-indexes`
188195

@@ -283,11 +290,13 @@ Allow to set foreign key constraint in migrations for ON DELETE event of row in
283290

284291
### `x-fk-on-update`
285292

286-
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`.
293+
Allow to set foreign key constraint in migrations for ON UPDATE event of row in database table. For example, see above
294+
section for `x-fk-on-delete`.
287295

288296
### `x-fk-column-name`
289297

290-
Provide custom database table column name in case of relationship column. This will not reflect in models relations, faker etc. Example:
298+
Provide custom database table column name in case of relationship column. This will not reflect in models relations,
299+
faker etc. Example:
291300

292301
```yaml
293302
components:
@@ -315,15 +324,15 @@ There are two ways for define many-to-many relations:
315324
316325
### Simple many-to-many without junction model
317326
318-
- property name for many-to-many relation should be equal lower-cased, pluralized related schema name
319-
320-
- referenced schema should contains mirrored reference to current schema
321-
322-
- migration for junction table can be generated automatically - table name should be [pluralized, lower-cased
323-
schema_name1]2[pluralized, lower-cased schema name2], in alphabetical order;
324-
For example, for schemas Post and Tag - table should be posts2tags, for schemas Post and Attachement - table should
327+
- property name for many-to-many relation should be equal lower-cased, pluralized related schema name
328+
329+
- referenced schema should contains mirrored reference to current schema
330+
331+
- migration for junction table can be generated automatically - table name should be [pluralized, lower-cased
332+
schema_name1]2[pluralized, lower-cased schema name2], in alphabetical order;
333+
For example, for schemas Post and Tag - table should be posts2tags, for schemas Post and Attachement - table should
325334
be attachments2posts
326-
335+
327336
```
328337
Post:
329338
properties:
@@ -341,16 +350,16 @@ Tag:
341350
items:
342351
$ref: '#/components/schemas/Post'
343352
```
344-
353+
345354
### Many-to-many with junction model
346355
347-
This way allowed creating multiple many-to-many relations between to models
356+
This way allowed creating multiple many-to-many relations between to models
348357
349358
- define junction schema with all necessary attributes. There are only one important requirement - the junction
350-
schema name
351-
must be started with prefix 'junction_' (This prefix will be used internally only and
352-
will be trimmed before table and model generation)
353-
359+
schema name
360+
must be started with prefix 'junction_' (This prefix will be used internally only and
361+
will be trimmed before table and model generation)
362+
354363
```
355364
# Model TeamMembers with table team_members will be generated with columns team_id, user_id and role
356365
junction_TeamMembers:
@@ -361,8 +370,9 @@ junction_TeamMembers:
361370
role:
362371
type: string
363372
```
373+
364374
- Both many-to-many related schemas must have properties with reference to "junction_*" schema. These properties will be
365-
used as relation names
375+
used as relation names
366376
367377
```
368378
Team:
@@ -381,9 +391,8 @@ User:
381391
items:
382392
$ref: '#/components/schemas/junction_TeamMembers'
383393
```
384-
385-
- see both examples here [tests/specs/many2many.yaml](tests/specs/many2many.yaml)
386-
394+
395+
- see both examples here [tests/specs/many2many.yaml](tests/specs/many2many.yaml)
387396
388397
## Handling of `NOT NULL` constraints
389398

@@ -434,6 +443,7 @@ e.g. attribute = 'my_property'.
434443
```
435444

436445
## Handling of `enum` (#enum)
446+
437447
It works on all 3 DB: MySQL, MariaDb and PgSQL.
438448

439449
```yaml
@@ -446,7 +456,9 @@ It works on all 3 DB: MySQL, MariaDb and PgSQL.
446456
- three
447457
```
448458

449-
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.
459+
Note: Changes in enum values are not very simple. For Mysql and Mariadb, migrations will be generated but in many cases
460+
custom modification in it are required. For Pgsql migrations for change in enum values will not be generated. It should
461+
be handled manually.
450462

451463
It will be ignored for database column schema (migrations) if `x-db-type` is provided.
452464

@@ -456,30 +468,36 @@ precision-default = 10
456468
scale-default = 2
457469

458470
- You can define attribute like "numeric(precision,scale)":
471+
459472
```yaml
460473
test_table:
461474
properties:
462475
my_property:
463476
x-db-type: decimal(12,4)
464477
```
478+
465479
DB-Result = decimal(12,4)
466480

467481
- You can define attribute like "numeric(precision)" with default scale-default = 2:
482+
468483
```yaml
469484
test_table:
470485
properties:
471486
my_property:
472487
x-db-type: decimal(12)
473488
```
489+
474490
DB-Result = decimal(12,2)
475491

476492
- You can define attribute like "numeric" with precision-default = 10 and scale-default = 2:
493+
477494
```yaml
478495
test_table:
479496
properties:
480497
my_property:
481498
x-db-type: decimal
482499
```
500+
483501
DB-Result = decimal(10,2)
484502

485503
## Handling of `timestamp` database column data type
@@ -494,7 +512,8 @@ created_at:
494512
readOnly: true
495513
```
496514

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

499518
```yaml
500519
created_at:
@@ -511,10 +530,14 @@ When generating code from an OpenAPI description there are many possible ways to
511530
Thus there are some assumptions and limitations that are currently applied to make this work.
512531
Here is a (possibly incomplete) list:
513532

514-
- The current implementation works best with OpenAPI description that follows the [JSON:API](https://jsonapi.org/) guidelines.
515-
- The request and response format/schema is currently not extracted from OpenAPI schema and may need to be adjusted manually if it does not follow JSON:API
516-
- column/field/property with name `id` is considered as Primary Key by this library and it is automatically handled by DB/Yii; so remove it from validation `rules()`
517-
- other fields can currently be used as primary keys using the `x-pk` OpenAPI extension (see below) but it may not be work correctly in all cases, please report bugs if you find them.
533+
- The current implementation works best with OpenAPI description that follows the [JSON:API](https://jsonapi.org/)
534+
guidelines.
535+
- The request and response format/schema is currently not extracted from OpenAPI schema and may need to be adjusted
536+
manually if it does not follow JSON:API
537+
- column/field/property with name `id` is considered as Primary Key by this library and it is automatically handled by
538+
DB/Yii; so remove it from validation `rules()`
539+
- other fields can currently be used as primary keys using the `x-pk` OpenAPI extension (see below) but it may not
540+
be work correctly in all cases, please report bugs if you find them.
518541

519542
Other things to keep in mind:
520543

@@ -524,7 +547,8 @@ When adding new fields in the API models, new migrations will be generated to ad
524547
For a project that is already in production, it should be considered to adjust the generated migration to add default
525548
values for existing data records.
526549

527-
One case where this is important is the addition of a new column with `NOT NULL` contraint, which does not provide a default value.
550+
One case where this is important is the addition of a new column with `NOT NULL` contraint, which does not provide a
551+
default value.
528552
Such a migration will fail when the table is not empty:
529553

530554
```php
@@ -533,7 +557,8 @@ $this->addColumn('{{%company}}', 'name', $this->string(128)->notNull());
533557

534558
Fails on a PostgreSQL database with
535559

536-
> add column name string(128) NOT NULL to table {{%company}} ...Exception: SQLSTATE[23502]: Not null violation: 7 ERROR: column "name" contains null values
560+
> add column name string(128) NOT NULL to table {{%company}} ...Exception: SQLSTATE[23502]: Not null violation: 7 ERROR:
561+
> column "name" contains null values
537562

538563
The solution would be to create the column, allowing NULL, set the value to a default and add the null constraint later.
539564

@@ -553,12 +578,10 @@ Generated files:
553578

554579
![Gii Generated Files](doc/screenshot-files.png)
555580

556-
557581
# Development
558582

559583
To contribute or play around, steps to set up this project up locally are in [CONTRIBUTING.md](./CONTRIBUTING.md).
560584

561-
562585
# Support
563586

564587
**Need help with your API project?**

0 commit comments

Comments
 (0)