Skip to content

Commit 6cfadcf

Browse files
committed
Handle example
1 parent ee6f7ec commit 6cfadcf

File tree

5 files changed

+28
-30
lines changed

5 files changed

+28
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ Provide custom database table column name in case of relationship column. This w
314314
### `x-no-relation`
315315

316316
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.
317+
related objects, `x-no-relation` (type: boolean, default: false) is used.
318318

319319
```yaml
320320
comments:

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(CustomSpecAttr::NO_RELATION))) { // TODO custom attr
221+
if (!empty($property->getAttr(CustomSpecAttr::NO_RELATION))) {
222222
$this->attributes[$property->getName()] = $attribute->setFakerStub($this->guessFakerStub($attribute, $property));
223223
}
224224

src/lib/FakerStubResolver.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,21 @@ public function resolve(): ?string
105105
} elseif ($this->attribute->phpType === 'array' ||
106106
substr($this->attribute->phpType, -2) === '[]') {
107107
$result = $this->fakeForArray($this->property->getProperty());
108+
if ($result !== '$faker->words()') { # example for array will only work with a list/`$faker->words()`
109+
return $result;
110+
}
108111
} elseif ($this->attribute->phpType === 'object') {
109112
$result = $this->fakeForObject($this->property->getProperty());
110113
} else {
111114
return null;
112115
}
113116

114-
if (!$this->property->hasAttr('example')) {
115-
return $result;
116-
}
117-
if (stripos($result, 'uniqueFaker') !== false) {
117+
if (!$this->property->hasAttr('example') ||
118+
$this->property->getAttr('uniqueItems')
119+
) {
118120
return $result;
119121
}
122+
120123
$example = $this->property->getAttr('example');
121124
$example = VarExporter::export($example);
122125
return str_replace('$faker->', '$faker->optional(0.92, ' . $example . ')->', $result);
@@ -277,8 +280,6 @@ private function fakeForArray(SpecObjectInterface $property, int $count = 4): st
277280
$uniqueItems = $property->uniqueItems;
278281
}
279282

280-
// TODO consider example of OpenAPI spec
281-
282283
/** @var Schema|Reference|null $items */
283284
$items = $property->items;
284285

@@ -308,13 +309,6 @@ private function fakeForArray(SpecObjectInterface $property, int $count = 4): st
308309
return $this->wrapInArray($result, $uniqueItems, $count);
309310
}
310311

311-
// TODO more complex type array/object; also consider $ref; may be recursively; may use `oneOf`
312-
313-
// return '$faker->words()'; // TODO implement faker for array; also consider min, max, unique
314-
315-
// if ($this->attribute->required) {
316-
// return '["a" => "b"]'; // TODO this is incorrect, array schema should be checked first
317-
// }
318312
return '[]';
319313
}
320314

@@ -383,7 +377,8 @@ public function wrapInArray(string $aFaker, bool $uniqueItems, int $count, bool
383377

384378
public function arbitraryArray(): string
385379
{
386-
return '$faker->words()';
380+
$theFaker = $this->property->getAttr('uniqueItems') ? '$uniqueFaker' : '$faker';
381+
return $theFaker . '->words()';
387382
}
388383

389384
/**

src/lib/items/Attribute.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@
77

88
namespace cebe\yii2openapi\lib\items;
99

10-
use yii\helpers\VarDumper;
11-
use \Yii;
12-
use cebe\yii2openapi\lib\openapi\PropertySchema;
13-
use cebe\yii2openapi\generator\ApiGenerator;
10+
use cebe\yii2openapi\db\ColumnSchema;
1411
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
12+
use cebe\yii2openapi\lib\openapi\PropertySchema;
1513
use yii\base\BaseObject;
16-
use cebe\yii2openapi\db\ColumnSchema;
17-
use yii\helpers\Inflector;
18-
use yii\helpers\StringHelper;
19-
use yii\db\mysql\Schema as MySqlSchema;
20-
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
21-
use yii\db\pgsql\Schema as PgSqlSchema;
14+
use yii\base\InvalidConfigException;
2215
use yii\base\NotSupportedException;
16+
use yii\helpers\Inflector;
2317
use function is_array;
2418
use function strtolower;
2519

@@ -302,7 +296,7 @@ public function getFormattedDescription():string
302296
return $type.' $'.str_replace("\n", "\n * ", rtrim($comment));
303297
}
304298

305-
public function toColumnSchema():ColumnSchema
299+
public function toColumnSchema(): ColumnSchema
306300
{
307301
$column = new ColumnSchema([
308302
'name' => $this->columnName,
@@ -333,7 +327,11 @@ public function toColumnSchema():ColumnSchema
333327
}
334328

335329
/**
336-
* @throws \yii\base\InvalidConfigException
330+
* @param string $dbType
331+
* @return string
332+
* @throws InvalidDefinitionException
333+
* @throws NotSupportedException
334+
* @throws InvalidConfigException
337335
*/
338336
private function yiiAbstractTypeForDbSpecificType(string $dbType): string
339337
{

tests/specs/issue_fix/20_consider_openapi_spec_examples_in_faker_code_generation/index.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ components:
5252
items: { } # array of arbitrary types e.g. [ "hello", -2, true, [5.7], {"id": 5} ]
5353
minItems: 6
5454
maxItems: 10
55-
uniqueItems: true
56-
# example: ['long-tail', 'short-tail', 'black', 'white']
55+
# uniqueItems: true
56+
example: [ 'long-tail', 'short-tail', 'black', 'white' ]
5757
number_arr:
5858
type: array
5959
items:
@@ -68,6 +68,8 @@ components:
6868

6969
int_arr:
7070
type: array
71+
# uniqueItems: true
72+
example: [ 4, 5 ]
7173
items:
7274
type: integer
7375

@@ -158,6 +160,9 @@ components:
158160
properties:
159161
id:
160162
type: integer
163+
title:
164+
type: string
165+
maxLength: 4
161166

162167
user_ref_obj_arr_normal: # faker for this won't be generated
163168
type: array

0 commit comments

Comments
 (0)