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
Merge branches 'master' and '45-setting-dbtype-to-enum-is-not-totally-correct' of github.com:php-openapi/yii2-openapi into 45-setting-dbtype-to-enum-is-not-totally-correct
Copy file name to clipboardExpand all lines: README.md
+163-6Lines changed: 163 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,7 +76,9 @@ return $config;
76
76
77
77
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).
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).
80
82
81
83
Run `./yii gii/api --help` for all options. Example: Disable generation of migrations files `./yii gii/api --generateMigrations=0`
82
84
@@ -212,6 +214,14 @@ Specify table indexes
212
214
default: '{}'
213
215
```
214
216
217
+
If raw DB expression is needed in index, then it must be for only one column. Example:
Ability to provide default value by database expression
@@ -309,15 +319,161 @@ Provide custom database table column name in case of relationship column. This w
309
319
- x-fk-column-name: redelivery_of # this will create `redelivery_of` column instead of `redelivery_of_id`
310
320
```
311
321
322
+
323
+
### `x-deleted-schemas`
324
+
325
+
This is root level key used to generate "drop table" migration for the deleted component schema. If a component schema (DB model) is removed from OpenAPI spec then its following entities should be also deleted from the code:
326
+
327
+
- DB table (migrations)
328
+
- model
329
+
- faker
330
+
331
+
So to generate appropriate migration for the removed schema, explicitly setting schema name or schema name + custom table name is required in this key. Only then the migrations will be generated. It should be set as:
332
+
333
+
```yaml
334
+
x-deleted-schemas:
335
+
- Fruit # Example: table name is evaluated to `itt_fruits`, if `itt_` is prefix set in DB config
336
+
- Mango: the_mango_table_name # custom table name; see `x-table` in README.md
337
+
```
338
+
339
+
### `x-no-relation`
340
+
341
+
To differentiate a component schema property from one-to-many or many-to-many relation in favour of array(json) of
342
+
related objects, `x-no-relation` (type: boolean, default: false) is used.
343
+
344
+
```yaml
345
+
comments:
346
+
type: array
347
+
items:
348
+
$ref: "#/components/schemas/Comment"
349
+
```
350
+
351
+
This will not generate 'comments' column in database migrations. But it will generate `getComments()` relation in Yii model file.
352
+
353
+
In order to make it real database column, extension `x-no-relation` can be used.
354
+
355
+
```yaml
356
+
comments:
357
+
type: array
358
+
x-no-relation: true
359
+
items:
360
+
$ref: "#/components/schemas/Comment"
361
+
```
362
+
363
+
Database column type can be `array`, `json` etc. to store such data.
364
+
365
+
Now if the Comment schema from the above example is
366
+
367
+
```yaml
368
+
Comment:
369
+
properties:
370
+
id:
371
+
type: integer
372
+
content:
373
+
type: string
374
+
```
375
+
376
+
then the value for `comments` can be
377
+
378
+
```json
379
+
[
380
+
{
381
+
"id": 1,
382
+
"content": "Hi there"
383
+
},
384
+
{
385
+
"id": 2,
386
+
"content": "Hi there 2"
387
+
}
388
+
]
389
+
```
390
+
391
+
`x-no-relation`can be only used with OpenAPI schema data type `array`.
392
+
393
+
### `x-route`
394
+
395
+
To customize route (controller ID/action ID) for a path, use custom key `x-route` with value `<controller ID>/<action ID>`. It can be used for non-crud paths. It must be used under HTTP method key but not
396
+
directly under the `paths` key of OpenAPI spec. Example:
397
+
398
+
```yaml
399
+
paths:
400
+
/payments/invoice/{invoice}:
401
+
parameters:
402
+
- name: invoice
403
+
in: path
404
+
description: lorem ipsum
405
+
required: true
406
+
schema:
407
+
type: integer
408
+
post:
409
+
x-route: 'payments/invoice'
410
+
summary: Pay Invoice
411
+
description: Pay for Invoice with given invoice number
412
+
requestBody:
413
+
description: Record new payment for an invoice
414
+
content:
415
+
application/json:
416
+
schema:
417
+
$ref: '#/components/schemas/Payments'
418
+
required: true
419
+
responses:
420
+
'200':
421
+
description: Successfully paid the invoice
422
+
content:
423
+
application/json:
424
+
schema:
425
+
$ref: '#/components/schemas/Success'
426
+
```
427
+
428
+
It won't generate `actionCreateInvoice` in `PaymentsController.php` file, but will generate `actionInvoice` instead in
429
+
same file.
430
+
431
+
Generated URL rules config for above is (in `urls.rest.php` or pertinent file):
Also, if same action is needed for HTTP GET and POST then use same value for `x-route`. Example:
438
+
439
+
```yaml
440
+
paths:
441
+
/a1/b1:
442
+
get:
443
+
x-route: 'abc/xyz'
444
+
operationId: opnid1
445
+
summary: List
446
+
description: Lists
447
+
responses:
448
+
'200':
449
+
description: The Response
450
+
post:
451
+
x-route: 'abc/xyz'
452
+
operationId: opnid2
453
+
summary: create
454
+
description: create
455
+
responses:
456
+
'200':
457
+
description: The Response
458
+
```
459
+
460
+
Generated URL rules config for above is (in `urls.rest.php` or pertinent file):
461
+
```php
462
+
'GET a1/b1' => 'abc/xyz',
463
+
'POST a1/b1' => 'abc/xyz',
464
+
'a1/b1' => 'abc/options',
465
+
```
466
+
`x-route`does not support [Yii Modules](https://www.yiiframework.com/doc/guide/2.0/en/structure-modules).
467
+
312
468
## Many-to-Many relation definition
313
469
314
470
There are two ways for define many-to-many relations:
315
471
316
472
### Simple many-to-many without junction model
317
473
318
474
- 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
475
+
476
+
- referenced schema should contain mirrored reference to current schema
321
477
322
478
- migration for junction table can be generated automatically - table name should be [pluralized, lower-cased
323
479
schema_name1]2[pluralized, lower-cased schema name2], in alphabetical order;
@@ -390,7 +546,7 @@ User:
390
546
`NOT NULL` in DB migrations is determined by `nullable` and `required` properties of the OpenAPI schema.
391
547
e.g. attribute = 'my_property'.
392
548
393
-
- If you define attribute neither "required" nor via "nullable", then it is by default `NULL`:
549
+
- If you define attribute neither "required" nor via "nullable", then it is by default `NULL` ([opposite of OpenAPI spec](https://swagger.io/specification/v3/?sbsearch=nullable)):
394
550
395
551
```yaml
396
552
ExampleSchema:
@@ -508,12 +664,13 @@ created_at:
508
664
## Assumptions
509
665
510
666
When generating code from an OpenAPI description there are many possible ways to achive a fitting result.
511
-
Thus there are some assumptions and limitations that are currently applied to make this work.
667
+
Thus, there are some assumptions and limitations that are currently applied to make this work.
512
668
Here is a (possibly incomplete) list:
513
669
514
670
- The current implementation works best with OpenAPI description that follows the [JSON:API](https://jsonapi.org/) guidelines.
515
671
- 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()`
672
+
- column/field/property with name `id` is considered as Primary Key by this library, and it is automatically handled by
673
+
DB/Yii; so remove it from validation `rules()`
517
674
- 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.
Copy file name to clipboardExpand all lines: src/db/ColumnSchema.php
+20Lines changed: 20 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -25,4 +25,24 @@ class ColumnSchema extends \yii\db\ColumnSchema
25
25
* ```
26
26
*/
27
27
public$xDbType;
28
+
29
+
/**
30
+
* Used only for MySQL/MariaDB
31
+
* @var array|null
32
+
* [
33
+
* index => int # position: starts from 1
34
+
* after => ?string # after column
35
+
* before => ?string # before column
36
+
* ]
37
+
* If `before` is null then column is last
38
+
* If `after` is null then column is first
39
+
* If both are null then table has only 1 column
40
+
*/
41
+
public ?array$fromPosition = null;
42
+
public ?array$toPosition = null;
43
+
44
+
/**
45
+
* From `$this->fromPosition` and `$this->toPosition` we can check if the position is changed or not. This is done in `BaseMigrationBuilder::setColumnsPositions()`
0 commit comments