You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+9-8Lines changed: 9 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,8 @@ make cli
21
21
# create new branch from master and Happy contributing!
22
22
```
23
23
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` .
25
26
26
27
```bash
27
28
cd tests
@@ -49,17 +50,19 @@ To apply multiple migration with one command:
49
50
PHPUnit run only one test by regex
50
51
----------------------------------
51
52
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
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
Copy file name to clipboardExpand all lines: README.md
+77-54Lines changed: 77 additions & 54 deletions
Original file line number
Diff line number
Diff line change
@@ -14,16 +14,17 @@ Base on [Gii, the Yii Framework Code Generator](https://www.yiiframework.com/ext
14
14
15
15
A code generator for OpenAPI and Yii Framework based PHP API application.
16
16
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))
18
19
19
20
Output: Yii Framework Application with Controllers, Models, database schema
20
21
21
-
22
22
## Features
23
23
24
24
Currently available features:
25
25
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
27
28
- Generate **Models** and validation based on OpenAPI Schema
28
29
- Generate **Database Schema** from OpenAPI Schema
29
30
- Generates **Database Migrations** for schema changes
To use the web generator, open `index.php?r=gii` and select the `REST API Generator`.
78
78
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).
82
82
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`
84
85
86
+
See [Petstore example](https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.0/petstore.yaml) for example
87
+
OpenAPI spec.
85
88
86
89
## OpenAPI extensions
87
90
@@ -145,7 +148,7 @@ You can generate non-db model based on \yii\base\Model without migrations by set
145
148
146
149
### `x-pk`
147
150
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"
149
152
150
153
```yaml
151
154
Post:
@@ -160,29 +163,33 @@ Explicitly specify primary key name for table, if it is different from "id"
160
163
161
164
### `x-db-type`
162
165
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.)).
164
168
If `x-db-type` is set to `false`, property will be processed as virtual;
165
169
It will be added in model as public property, but skipped for migrations generation.
- `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`)
184
186
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`.
186
193
187
194
### `x-indexes`
188
195
@@ -283,11 +290,13 @@ Allow to set foreign key constraint in migrations for ON DELETE event of row in
283
290
284
291
### `x-fk-on-update`
285
292
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`.
287
295
288
296
### `x-fk-column-name`
289
297
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:
291
300
292
301
```yaml
293
302
components:
@@ -315,15 +324,15 @@ There are two ways for define many-to-many relations:
315
324
316
325
### Simple many-to-many without junction model
317
326
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
325
334
be attachments2posts
326
-
335
+
327
336
```
328
337
Post:
329
338
properties:
@@ -341,16 +350,16 @@ Tag:
341
350
items:
342
351
$ref: '#/components/schemas/Post'
343
352
```
344
-
353
+
345
354
### Many-to-many with junction model
346
355
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
348
357
349
358
- 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
+
354
363
```
355
364
# Model TeamMembers with table team_members will be generated with columns team_id, user_id and role
356
365
junction_TeamMembers:
@@ -361,8 +370,9 @@ junction_TeamMembers:
361
370
role:
362
371
type: string
363
372
```
373
+
364
374
- 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
366
376
367
377
```
368
378
Team:
@@ -381,9 +391,8 @@ User:
381
391
items:
382
392
$ref: '#/components/schemas/junction_TeamMembers'
383
393
```
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)
387
396
388
397
## Handling of `NOT NULL` constraints
389
398
@@ -434,6 +443,7 @@ e.g. attribute = 'my_property'.
434
443
```
435
444
436
445
## Handling of `enum` (#enum)
446
+
437
447
It works on all 3 DB: MySQL, MariaDb and PgSQL.
438
448
439
449
```yaml
@@ -446,7 +456,9 @@ It works on all 3 DB: MySQL, MariaDb and PgSQL.
446
456
- three
447
457
```
448
458
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.
450
462
451
463
It will be ignored for database column schema (migrations) if `x-db-type` is provided.
452
464
@@ -456,30 +468,36 @@ precision-default = 10
456
468
scale-default = 2
457
469
458
470
- You can define attribute like "numeric(precision,scale)":
471
+
459
472
```yaml
460
473
test_table:
461
474
properties:
462
475
my_property:
463
476
x-db-type: decimal(12,4)
464
477
```
478
+
465
479
DB-Result = decimal(12,4)
466
480
467
481
- You can define attribute like "numeric(precision)" with default scale-default = 2:
482
+
468
483
```yaml
469
484
test_table:
470
485
properties:
471
486
my_property:
472
487
x-db-type: decimal(12)
473
488
```
489
+
474
490
DB-Result = decimal(12,2)
475
491
476
492
- You can define attribute like "numeric" with precision-default = 10 and scale-default = 2:
493
+
477
494
```yaml
478
495
test_table:
479
496
properties:
480
497
my_property:
481
498
x-db-type: decimal
482
499
```
500
+
483
501
DB-Result = decimal(10,2)
484
502
485
503
## Handling of `timestamp` database column data type
@@ -494,7 +512,8 @@ created_at:
494
512
readOnly: true
495
513
```
496
514
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
498
517
499
518
```yaml
500
519
created_at:
@@ -511,10 +530,14 @@ When generating code from an OpenAPI description there are many possible ways to
511
530
Thus there are some assumptions and limitations that are currently applied to make this work.
512
531
Here is a (possibly incomplete) list:
513
532
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.
518
541
519
542
Other things to keep in mind:
520
543
@@ -524,7 +547,8 @@ When adding new fields in the API models, new migrations will be generated to ad
524
547
For a project that is already in production, it should be considered to adjust the generated migration to add default
525
548
values for existing data records.
526
549
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.
528
552
Such a migration will fail when the table is not empty:
0 commit comments