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 '22-bug-rules-required-is-generated-before-_default' of github.com:php-openapi/yii2-openapi into 22-bug-rules-required-is-generated-before-_default
Copy file name to clipboardExpand all lines: README.md
+217-6Lines changed: 217 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,215 @@ 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
+
468
+
### `x-description-is-comment`
469
+
470
+
boolean; default: false
471
+
472
+
When a new database table is created from new OpenAPI component schema, description of a property will be used as
473
+
comment of column (of database table).
474
+
475
+
This extension is used when a description is edited for existing property, and you want to generate migration for its
476
+
corresponding column comment changes.
477
+
478
+
This extension can be used at 3 place:
479
+
480
+
**1. root level (highest priority)**
481
+
482
+
```yaml
483
+
openapi: 3.0.3
484
+
x-description-is-comment: true
485
+
info:
486
+
title: Description
487
+
```
488
+
489
+
This will create migration of any changed description of component schema property present throughout the spec.
490
+
491
+
**2. component schema level**
492
+
493
+
```yaml
494
+
components:
495
+
schemas:
496
+
Fruit:
497
+
type: object
498
+
x-description-is-comment: true
499
+
```
500
+
501
+
This will create migration of changed description of only properties of component schema which have this extension.
502
+
503
+
**3. property level (lowest priority)**
504
+
505
+
```yaml
506
+
components:
507
+
schemas:
508
+
Fruit:
509
+
type: object
510
+
properties:
511
+
id:
512
+
type: integer
513
+
name:
514
+
type: string
515
+
nullable: false
516
+
x-description-is-comment: true
517
+
description: Hi there
518
+
```
519
+
520
+
Migrations will be only generated for changed description of properties having this extension.
521
+
312
522
## Many-to-Many relation definition
313
523
314
524
There are two ways for define many-to-many relations:
315
525
316
526
### Simple many-to-many without junction model
317
527
318
528
- 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
529
+
530
+
- referenced schema should contain mirrored reference to current schema
321
531
322
532
- migration for junction table can be generated automatically - table name should be [pluralized, lower-cased
323
533
schema_name1]2[pluralized, lower-cased schema name2], in alphabetical order;
@@ -390,7 +600,7 @@ User:
390
600
`NOT NULL` in DB migrations is determined by `nullable` and `required` properties of the OpenAPI schema.
391
601
e.g. attribute = 'my_property'.
392
602
393
-
- If you define attribute neither "required" nor via "nullable", then it is by default `NULL`:
603
+
- 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
604
395
605
```yaml
396
606
ExampleSchema:
@@ -508,12 +718,13 @@ created_at:
508
718
## Assumptions
509
719
510
720
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.
721
+
Thus, there are some assumptions and limitations that are currently applied to make this work.
512
722
Here is a (possibly incomplete) list:
513
723
514
724
- The current implementation works best with OpenAPI description that follows the [JSON:API](https://jsonapi.org/) guidelines.
515
725
- 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()`
726
+
- column/field/property with name `id` is considered as Primary Key by this library, and it is automatically handled by
727
+
DB/Yii; so remove it from validation `rules()`
517
728
- 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.
0 commit comments