Skip to content

Commit 4f6d728

Browse files
committed
Fix issues + add support for all refs only in oneOf
1 parent 960cc43 commit 4f6d728

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ Provide custom database table column name in case of relationship column. This w
309309
- x-fk-column-name: redelivery_of # this will create `redelivery_of` column instead of `redelivery_of_id`
310310
```
311311
312+
### `x-no-relation`
313+
312314
## Many-to-Many relation definition
313315

314316
There are two ways for define many-to-many relations:

src/lib/FakerStubResolver.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function resolve(): ?string
8787
$mn = $config->modelNamespace;
8888
return '$faker->randomElement(\\' . $mn
8989
. ($mn ? '\\' : '')
90-
. ucfirst((string) $this->attribute->reference) . '::find()->select("id")->column())';
90+
. ucfirst((string)$this->attribute->reference) . '::find()->select("id")->column())'; // TODO PK "id" can be also something else
9191
}
9292

9393
$limits = $this->attribute->limits;
@@ -285,19 +285,18 @@ private function fakeForArray(SpecObjectInterface $property, int $count = 4): st
285285
}
286286

287287
if ($items instanceof Reference) {
288-
$class = str_replace('#/components/schemas/', '', $items->getReference());
289-
$class .= 'Faker';
290-
return $this->wrapInArray('(new ' . $class . ')->generateModel()->attributes', false, $count);
291-
} elseif (!empty($items->oneOf)) {
288+
$aFakerForRef = $this->aElementFaker($items);
289+
return $this->wrapInArray($aFakerForRef, $uniqueItems, $count);
290+
}
291+
if (!empty($items->oneOf)) {
292292
return $this->handleOneOf($items, $count);
293293
}
294294

295295
$type = $items->type;
296296
if ($type === null) {
297297
return $this->arbitraryArray();
298298
}
299-
$aFaker = $this->aElementFaker($this->property->getProperty()->getSerializableData());
300-
299+
$aFaker = $this->aElementFaker($this->property->getProperty());
301300
if (in_array($type, ['string', 'number', 'integer', 'boolean', 'array'])) {
302301
return $this->wrapInArray($aFaker, $uniqueItems, $count);
303302
}
@@ -350,9 +349,16 @@ public function fakeForObject(SpecObjectInterface $items): string
350349
}
351350

352351
/**
353-
* @param $items
354-
* @param $count
352+
* This method must be only used incase of array
353+
* @param SpecObjectInterface $items
354+
* @param int $count
355355
* @return string
356+
* @throws ExceptionInterface
357+
* @throws IOException
358+
* @throws InvalidConfigException
359+
* @throws InvalidDefinitionException
360+
* @throws TypeErrorException
361+
* @throws UnresolvableReferenceException
356362
* @internal
357363
*/
358364
public function handleOneOf(SpecObjectInterface $items, int $count): string
@@ -361,8 +367,9 @@ public function handleOneOf(SpecObjectInterface $items, int $count): string
361367
foreach ($items->oneOf as $key => $aDataType) {
362368
/** @var Schema|Reference $aDataType */
363369

364-
$a1 = $this->aElementFaker(['items' => $aDataType->getSerializableData()]);
365-
$result .= '$dataType' . $key . ' = ' . $a1 . ';';
370+
$inp = $aDataType instanceof Reference ? $aDataType : ['items' => $aDataType->getSerializableData()];
371+
$aFaker = $this->aElementFaker($inp);
372+
$result .= '$dataType' . $key . ' = ' . $aFaker . ';';
366373
}
367374
$ct = count($items->oneOf) - 1;
368375
$result .= 'return ${"dataType".rand(0, ' . $ct . ')};';
@@ -385,7 +392,7 @@ public function arbitraryArray(): string
385392
/**
386393
* 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.
387394
* Also see OpenAPI extension `x-no-relation` in README.md
388-
* @param $data object|array
395+
* @param $data array|\stdClass|SpecObjectInterface
389396
* @return string|null
390397
* @throws ExceptionInterface
391398
* @throws InvalidConfigException
@@ -397,7 +404,14 @@ public function arbitraryArray(): string
397404
*/
398405
public function aElementFaker($data): ?string
399406
{
400-
$aElementData = Json::decode(Json::encode($data)); // element object of stdClass -> array
407+
if ($data instanceof Reference) {
408+
$class = str_replace('#/components/schemas/', '', $data->getReference());
409+
$class .= 'Faker';
410+
return '(new ' . $class . ')->generateModel()->attributes';
411+
}
412+
413+
$inp = $data instanceof SpecObjectInterface ? $data->getSerializableData() : $data;
414+
$aElementData = Json::decode(Json::encode($inp));
401415
$compoSchemaData = [
402416
'properties' => [
403417
'unnamedProp' => $aElementData['items']

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ components:
2424
type: integer
2525
name:
2626
type: string
27+
Fruit:
28+
properties:
29+
id:
30+
type: integer
31+
name:
32+
type: string
2733
Pet:
2834
required:
2935
- id
@@ -188,7 +194,16 @@ components:
188194
- type: array
189195
items:
190196
$ref: '#/components/schemas/User'
197+
- $ref: '#/components/schemas/Fruit'
198+
199+
one_of_from_multi_ref_arr:
200+
type: array
201+
x-no-relation: true
202+
items:
203+
oneOf:
204+
- $ref: '#/components/schemas/User'
205+
- $ref: '#/components/schemas/Fruit'
206+
191207

192208

193-
# oneOf
194-
# TODO count is not working in some cases
209+
# TODO count is not working in some cases

0 commit comments

Comments
 (0)