Skip to content

Commit ee6f7ec

Browse files
committed
Add docs for x-no-relation and create its constant
1 parent 77f6ff2 commit ee6f7ec

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

README.md

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ 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).
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).
8082

8183
Run `./yii gii/api --help` for all options. Example: Disable generation of migrations files `./yii gii/api --generateMigrations=0`
8284

@@ -311,15 +313,67 @@ Provide custom database table column name in case of relationship column. This w
311313
312314
### `x-no-relation`
313315

316+
To differentiate a component schema property from one-to-many or many-to-many relation in favour of array(json) of
317+
related objects, `x-no-relation` is used.
318+
319+
```yaml
320+
comments:
321+
type: array
322+
items:
323+
$ref: "#/components/schemas/Comment"
324+
```
325+
326+
This will not generate 'comments' column in database migrations. But it will generate `getComments()` relation in model.
327+
328+
In order to make it real database column, extension `x-no-relation` can be used.
329+
330+
```yaml
331+
comments:
332+
type: array
333+
x-no-relation: true
334+
items:
335+
$ref: "#/components/schemas/Comment"
336+
```
337+
338+
Database column type can be `array`, `json` etc. to store such data.
339+
340+
Now if the Comment schema from the above example is
341+
342+
```yaml
343+
Comment:
344+
properties:
345+
id:
346+
type: integer
347+
content:
348+
type: string
349+
```
350+
351+
then the value can be
352+
353+
```json
354+
[
355+
{
356+
"id": 1,
357+
"content": "Hi there"
358+
},
359+
{
360+
"id": 2,
361+
"content": "Hi there 2"
362+
}
363+
]
364+
```
365+
366+
At this moment, `x-no-relation` can be only used with OpenAPI schema data type `array`.
367+
314368
## Many-to-Many relation definition
315369

316370
There are two ways for define many-to-many relations:
317371

318372
### Simple many-to-many without junction model
319373

320374
- property name for many-to-many relation should be equal lower-cased, pluralized related schema name
321-
322-
- referenced schema should contains mirrored reference to current schema
375+
376+
- referenced schema should contain mirrored reference to current schema
323377

324378
- migration for junction table can be generated automatically - table name should be [pluralized, lower-cased
325379
schema_name1]2[pluralized, lower-cased schema name2], in alphabetical order;
@@ -510,12 +564,13 @@ created_at:
510564
## Assumptions
511565

512566
When generating code from an OpenAPI description there are many possible ways to achive a fitting result.
513-
Thus there are some assumptions and limitations that are currently applied to make this work.
567+
Thus, there are some assumptions and limitations that are currently applied to make this work.
514568
Here is a (possibly incomplete) list:
515569

516570
- The current implementation works best with OpenAPI description that follows the [JSON:API](https://jsonapi.org/) guidelines.
517571
- 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
518-
- 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()`
572+
- column/field/property with name `id` is considered as Primary Key by this library, and it is automatically handled by
573+
DB/Yii; so remove it from validation `rules()`
519574
- 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.
520575

521576
Other things to keep in mind:

src/lib/AttributeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected function resolveProperty(
218218
}
219219
$attribute = Yii::createObject(Attribute::class, [$property->getName()]);
220220

221-
if (!empty($property->getAttr('x-no-relation'))) { // TODO custom attr
221+
if (!empty($property->getAttr(CustomSpecAttr::NO_RELATION))) { // TODO custom attr
222222
$this->attributes[$property->getName()] = $attribute->setFakerStub($this->guessFakerStub($attribute, $property));
223223
}
224224

src/lib/CustomSpecAttr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ class CustomSpecAttr
4444
/**
4545
* Foreign key column name. See README for usage docs
4646
*/
47-
// public const FK_COLUMN_NAME = 'x-fk-column-name';
47+
public const NO_RELATION = 'x-no-relation';
4848
}

src/lib/FakerStubResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public function aElementFaker($data, ?string $columnName = null): ?string
421421
// If you intend to use this method from out of `fakeForArray()` context then below condition should be changed depending on your use case
422422
// Also see OpenAPI extension `x-no-relation` in README.md
423423
if (!empty($compoSchemaData['properties'][$columnName]['items']['$ref'])) {
424-
$compoSchemaData['properties'][$columnName]['x-no-relation'] = true;
424+
$compoSchemaData['properties'][$columnName][CustomSpecAttr::NO_RELATION] = true;
425425
}
426426

427427
$schema = new Schema($compoSchemaData);

0 commit comments

Comments
 (0)