Skip to content

Commit 6ab30cc

Browse files
committed
Fix count issue for nested array
1 parent de75ca7 commit 6ab30cc

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/lib/AttributeResolver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ protected function resolveProperty(
347347
return;
348348
}
349349
$attribute->setPhpType($relatedClassName . '[]');
350-
351350
$this->relations[$property->getName()] =
352351
Yii::createObject(
353352
AttributeRelation::class,

src/lib/FakerStubResolver.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace cebe\yii2openapi\lib;
1212

13+
use cebe\openapi\exceptions\IOException;
1314
use cebe\openapi\exceptions\TypeErrorException;
1415
use cebe\openapi\exceptions\UnresolvableReferenceException;
1516
use cebe\openapi\ReferenceContext;
@@ -256,6 +257,7 @@ private function fakeForFloat(?int $min, ?int $max): ?string
256257
* @throws TypeErrorException
257258
* @throws UnresolvableReferenceException
258259
* @throws InvalidDefinitionException|ExceptionInterface
260+
* @throws IOException
259261
*/
260262
private function fakeForArray(SpecObjectInterface $property, int $count = 4): string
261263
{
@@ -294,22 +296,17 @@ private function fakeForArray(SpecObjectInterface $property, int $count = 4): st
294296
if ($type === null) {
295297
return $this->arbitraryArray();
296298
}
297-
$aElementFaker = $this->aElementFaker($this->property->getProperty()->getSerializableData());
299+
$aFaker = $this->aElementFaker($this->property->getProperty()->getSerializableData());
298300

299-
if (in_array($type, ['string', 'number', 'integer', 'boolean'])) {
300-
return $this->wrapInArray($aElementFaker, $uniqueItems, $count);
301-
}
302-
303-
if ($type === 'array') { # array or nested arrays
304-
return $this->{__FUNCTION__}($items);
301+
if (in_array($type, ['string', 'number', 'integer', 'boolean', 'array'])) {
302+
return $this->wrapInArray($aFaker, $uniqueItems, $count);
305303
}
306304

307305
if ($type === 'object') {
308-
$result = $this->fakeForObject($items, $count);
306+
$result = $this->fakeForObject($items);
309307
return $this->wrapInArray($result, $uniqueItems, $count);
310308
}
311309

312-
313310
// TODO more complex type array/object; also consider $ref; may be recursively; may use `oneOf`
314311

315312
// return '$faker->words()'; // TODO implement faker for array; also consider min, max, unique
@@ -373,10 +370,10 @@ public function handleOneOf($items, $count): string
373370
return $result;
374371
}
375372

376-
public function wrapInArray($aElementFaker, $uniqueItems, $count): string
373+
public function wrapInArray($aFaker, $uniqueItems, $count): string
377374
{
378375
return 'array_map(function () use ($faker, $uniqueFaker) {
379-
return ' . ($uniqueItems ? str_replace('$faker->', '$uniqueFaker->', $aElementFaker) : $aElementFaker) . ';
376+
return ' . ($uniqueItems ? str_replace('$faker->', '$uniqueFaker->', $aFaker) : $aFaker) . ';
380377
}, range(1, ' . $count . '))';
381378
}
382379

@@ -385,6 +382,19 @@ public function arbitraryArray(): string
385382
return '$faker->words()';
386383
}
387384

385+
/**
386+
* This method is only for `fakeForArray()` or methods only used inside `fakeForArray()`. If needed to use outside `fakeForArray()` context then some changes might be required.
387+
* Also see OpenAPI extension `x-no-relation` in README.md
388+
* @param $data
389+
* @return string|null
390+
* @throws ExceptionInterface
391+
* @throws InvalidConfigException
392+
* @throws InvalidDefinitionException
393+
* @throws TypeErrorException
394+
* @throws UnresolvableReferenceException
395+
* @throws IOException
396+
* @internal
397+
*/
388398
public function aElementFaker($data): ?string
389399
{
390400
$aElementData = Json::decode(Json::encode($data)); // object of stdClass -> array
@@ -393,7 +403,11 @@ public function aElementFaker($data): ?string
393403
'unnamedProp' => $aElementData['items']
394404
]
395405
];
396-
if (!empty($compoSchemaData['properties']['unnamedProp']['items']['$ref'])) { // TODO
406+
407+
// This condition is only for properties with type = array
408+
// If you intend to use this method from out of `fakeForArray()` context then below condition should be changed depending on your use case
409+
// Also see OpenAPI extension `x-no-relation` in README.md
410+
if (!empty($compoSchemaData['properties']['unnamedProp']['items']['$ref'])) {
397411
$compoSchemaData['properties']['unnamedProp']['x-no-relation'] = true;
398412
}
399413

@@ -405,16 +419,6 @@ public function aElementFaker($data): ?string
405419
}
406420
$dbModels = (new AttributeResolver('UnnamedCompo', $cs, new JunctionSchemas([]), $this->config))->resolve();
407421

408-
// foreach ($schema->properties as $name => $prop) {
409-
// if($prop->items instanceof Reference) {
410-
// $dbModels->attributes[$name] = new Attribute($name, [
411-
// 'phpType' => 'array',
412-
// 'dbType' => 'array',
413-
// 'reference' => $prop->items->getReference(),
414-
// ]);
415-
// }
416-
// }
417-
418422
return (new static($dbModels->attributes['unnamedProp'], $cs->getProperty('unnamedProp'), $this->config))->resolve();
419423
}
420424
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ components:
9393

9494
arr_arr_arr_str:
9595
type: array
96+
minItems: 3
9697
items:
9798
type: array
99+
minItems: 4
98100
items:
99101
type: array
102+
minItems: 5
100103
items:
101104
type: string
102105

0 commit comments

Comments
 (0)