@@ -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 ' ]
0 commit comments