Skip to content

Commit 92e2b97

Browse files
committed
Merge #21 remote-tracking branch 'php-openapi/20-consider-openapi-spec-examples-in-faker-code-generation'
* php-openapi/20-consider-openapi-spec-examples-in-faker-code-generation: (28 commits) Enhance docs Add test spec file Handle example Add docs for `x-no-relation` and create its constant Fix bug Refactor Fix issues + add support for all refs only in oneOf Add typehint to fn args Fix count issue for nested array Fix failing test in PHP >= 8.1 Fix errors 2 + implement custom attribute `x-no-relation` Fix errors Fix failing test Fix failing test Fix bug Fix failing test for x_db_type Implement complex oneOf + fix error 'Creating default object from empty value in PHP' Complex oneOf - WIP Implement oneOf and refactor - WIP 3 Implement oneOf and refactor - WIP 2 ...
2 parents 1e837f3 + 775ff26 commit 92e2b97

Some content is hidden

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

43 files changed

+1199
-113
lines changed

Diff for: Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ up:
4545
cli:
4646
docker-compose exec --user=$(UID) php bash
4747

48+
cli_root:
49+
docker-compose exec --user="root" php bash
50+
4851
cli_mysql:
4952
docker-compose exec --user=$(UID) mysql bash
5053

Diff for: README.md

+62-5
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

@@ -317,6 +319,60 @@ Provide custom database table column name in case of relationship column. This w
317319
- x-fk-column-name: redelivery_of # this will create `redelivery_of` column instead of `redelivery_of_id`
318320
```
319321
322+
### `x-no-relation`
323+
324+
To differentiate a component schema property from one-to-many or many-to-many relation in favour of array(json) of
325+
related objects, `x-no-relation` (type: boolean, default: false) is used.
326+
327+
```yaml
328+
comments:
329+
type: array
330+
items:
331+
$ref: "#/components/schemas/Comment"
332+
```
333+
334+
This will not generate 'comments' column in database migrations. But it will generate `getComments()` relation in Yii model file.
335+
336+
In order to make it real database column, extension `x-no-relation` can be used.
337+
338+
```yaml
339+
comments:
340+
type: array
341+
x-no-relation: true
342+
items:
343+
$ref: "#/components/schemas/Comment"
344+
```
345+
346+
Database column type can be `array`, `json` etc. to store such data.
347+
348+
Now if the Comment schema from the above example is
349+
350+
```yaml
351+
Comment:
352+
properties:
353+
id:
354+
type: integer
355+
content:
356+
type: string
357+
```
358+
359+
then the value for `comments` can be
360+
361+
```json
362+
[
363+
{
364+
"id": 1,
365+
"content": "Hi there"
366+
},
367+
{
368+
"id": 2,
369+
"content": "Hi there 2"
370+
}
371+
]
372+
```
373+
374+
`x-no-relation` can be only used with OpenAPI schema data type `array`.
375+
320376
### `x-route`
321377

322378
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
@@ -399,8 +455,8 @@ There are two ways for define many-to-many relations:
399455
### Simple many-to-many without junction model
400456

401457
- property name for many-to-many relation should be equal lower-cased, pluralized related schema name
402-
403-
- referenced schema should contains mirrored reference to current schema
458+
459+
- referenced schema should contain mirrored reference to current schema
404460

405461
- migration for junction table can be generated automatically - table name should be [pluralized, lower-cased
406462
schema_name1]2[pluralized, lower-cased schema name2], in alphabetical order;
@@ -591,12 +647,13 @@ created_at:
591647
## Assumptions
592648

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

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

602659
Other things to keep in mind:

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
},
2020
"require": {
2121
"php": "^7.4 || ^8.0",
22-
"cebe/php-openapi": "^1.5.0",
22+
"cebe/php-openapi": "^1.7.0",
2323
"yiisoft/yii2": "~2.0.48",
2424
"yiisoft/yii2-gii": "~2.0.0 | ~2.1.0 | ~2.2.0| ~2.3.0",
2525
"laminas/laminas-code": ">=3.4 <=4.13",
2626
"php-openapi/yii2-fractal": "^1.0.0",
2727
"fakerphp/faker": "^1.9",
2828
"sam-it/yii2-mariadb": "^2.0",
29+
"symfony/var-exporter": "^5.4",
2930
"symfony/polyfill-php80": "^1.30"
3031
},
3132
"require-dev": {

Diff for: src/lib/AttributeResolver.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,22 @@ protected function resolveProperty(
219219
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
220220
}
221221
$attribute = Yii::createObject(Attribute::class, [$property->getName()]);
222+
223+
if (!empty($property->getAttr(CustomSpecAttr::NO_RELATION))) {
224+
$this->attributes[$property->getName()] = $attribute->setFakerStub($this->guessFakerStub($attribute, $property));
225+
}
226+
222227
$attribute->setRequired($isRequired)
228+
->setPhpType($property->guessPhpType())
223229
->setDescription($property->getAttr('description', ''))
224230
->setReadOnly($property->isReadonly())
225231
->setDefault($property->guessDefault())
226232
->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE))
227233
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
228234
->setNullable($nullableValue)
229235
->setIsPrimary($property->isPrimaryKey())
230-
->setForeignKeyColumnName($property->fkColName);
236+
->setForeignKeyColumnName($property->fkColName)
237+
->setFakerStub($this->guessFakerStub($attribute, $property));
231238
if ($property->isReference()) {
232239
if ($property->isVirtual()) {
233240
throw new InvalidDefinitionException('References not supported for virtual attributes');
@@ -261,7 +268,8 @@ protected function resolveProperty(
261268
->setSize($fkProperty->getMaxLength())
262269
->setDescription($property->getRefSchema()->getDescription())
263270
->setDefault($fkProperty->guessDefault())
264-
->setLimits($min, $max, $fkProperty->getMinLength());
271+
->setLimits($min, $max, $fkProperty->getMinLength())
272+
->setFakerStub($this->guessFakerStub($attribute, $property));
265273

266274
$relation = Yii::createObject(
267275
AttributeRelation::class,

Diff for: src/lib/CustomSpecAttr.php

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class CustomSpecAttr
4141
*/
4242
public const FK_COLUMN_NAME = 'x-fk-column-name';
4343

44+
/**
45+
* Foreign key column name. See README for usage docs
46+
*/
47+
public const NO_RELATION = 'x-no-relation';
48+
4449
/**
4550
* Custom route (controller ID/action ID) instead of auto-generated. See README for usage docs. https://github.com/cebe/yii2-openapi/issues/144
4651
*/

0 commit comments

Comments
 (0)