Skip to content

Commit 1e7cea6

Browse files
committed
Complex oneOf - WIP
1 parent b3c660b commit 1e7cea6

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/lib/FakerStubResolver.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ private function fakeForArray(SpecObjectInterface $property, int $count = 4): st
272272
// TODO consider example of OpenAPI spec
273273

274274
/** @var Schema|Reference|null $items */
275-
$items = $property->items; # later is used only in `oneOf`
275+
$items = $property->items;
276276

277277
if (!$items) {
278278
return $this->arbitraryArray();
@@ -290,7 +290,7 @@ private function fakeForArray(SpecObjectInterface $property, int $count = 4): st
290290
if ($type === null) {
291291
return $this->arbitraryArray();
292292
}
293-
$aElementFaker = $this->aElementFaker();
293+
$aElementFaker = $this->aElementFaker($this->property->getProperty()->getSerializableData());
294294

295295
if (in_array($type, ['string', 'number', 'integer', 'boolean'])) {
296296
return $this->wrapAsArray($aElementFaker, $uniqueItems, $count);
@@ -375,7 +375,7 @@ public function handleOneOf($items, $count): string
375375
/** @var Schema|Reference $aDataType */
376376

377377
// $a1 = $this->fakeForArray($aDataType, 1);
378-
$a1 = $this->aElementFaker();
378+
$a1 = $this->aElementFaker($aDataType->getSerializableData());
379379
$result .= '$dataType' . $key . ' = ' . $a1 . ';';
380380
}
381381
$ct = count($items->oneOf) - 1;
@@ -396,12 +396,12 @@ public function arbitraryArray(): string
396396
return '$faker->words()';
397397
}
398398

399-
public function aElementFaker(): ?string
399+
public function aElementFaker($data): ?string
400400
{
401-
$aElementData = Json::decode(Json::encode($this->property->getProperty()->getSerializableData()));
401+
$aElementData = Json::decode(Json::encode($data));
402402
$compoSchemaData = [
403403
'properties' => [
404-
'unnamedProp' => $aElementData['items']
404+
'unnamedProp' => $aElementData['items'] ?? $aElementData # later is used only in `oneOf`
405405
]
406406
];
407407
$cs = new ComponentSchema(new Schema($compoSchemaData), 'UnnamedCompo');

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ components:
3434
name:
3535
type: string
3636
example: cat
37-
# example: ['long-tail', 'short-tail', 'black', 'white']
37+
# example: ['long-tail', 'short-tail', 'black', 'white']
3838
age:
3939
type: integer
4040
example: 2
@@ -157,6 +157,15 @@ components:
157157
oneOf:
158158
- type: integer
159159
- type: string
160+
- type: boolean
161+
- type: array
162+
- type: array
163+
items:
164+
type: string
165+
- type: object
166+
properties:
167+
id:
168+
type: integer
160169

161170

162171
# oneOf

0 commit comments

Comments
 (0)