diff --git a/src/Tools/WritingUtils.php b/src/Tools/WritingUtils.php index 4a37d56c..43efe255 100644 --- a/src/Tools/WritingUtils.php +++ b/src/Tools/WritingUtils.php @@ -218,6 +218,10 @@ public static function getSampleBody(array $nestedBodyParameters) return array_map(function ($param) { if (!empty($param['__fields'])) { + if ($param['type'] === 'object[]') { + return [self::getSampleBody($param['__fields'])]; + } + return self::getSampleBody($param['__fields']); } diff --git a/tests/Unit/WritingUtilsTest.php b/tests/Unit/WritingUtilsTest.php index 6cb139ef..91046f2a 100644 --- a/tests/Unit/WritingUtilsTest.php +++ b/tests/Unit/WritingUtilsTest.php @@ -78,6 +78,25 @@ public function print_query_params_as_string_bash() $this->assertEquals($expected, $queryParams); } + /** @test */ + public function get_sample_body_with_array_fields() + { + $sampleBody = WritingUtils::getSampleBody($this->bodyParamsWithArrayFields()); + + $expected = [ + 'name' => 'Experience Form', + 'fields' => [ + [ + 'name' => 'experience', + 'label' => 'Experience', + 'type' => 'textarea', + 'order' => 1, + ], + ], + ]; + $this->assertEquals($expected, $sampleBody); + } + private function queryParams(): array { return [ @@ -99,6 +118,63 @@ private function queryParams(): array ]; } + private function bodyParamsWithArrayFields(): array + { + return [ + 'name' => [ + 'name' => 'name', + 'description' => 'Form\'s name', + 'required' => true, + 'example' => 'Experience Form', + 'type' => 'string', + 'custom' => [], + '__fields' => [], + ], + 'fields' => [ + 'name' => 'fields', + 'description' => 'Form\'s fields', + 'required' => false, + 'example' => [[]], + 'type' => 'object[]', + 'custom' => [], + '__fields' => [ + 'name' => [ + 'name' => 'fields[].name', + 'description' => 'Field\'s name', + 'required' => true, + 'example' => 'experience', + 'type' => 'string', + 'custom' => [], + ], + 'label' => [ + 'name' => 'fields[].label', + 'description' => 'Field\'s label', + 'required' => true, + 'example' => 'Experience', + 'type' => 'string', + 'custom' => [], + ], + 'type' => [ + 'name' => 'fields[].type', + 'description' => 'Field\'s type', + 'required' => true, + 'example' => 'textarea', + 'type' => 'string', + 'custom' => [], + ], + 'order' => [ + 'name' => 'fields[].order', + 'description' => 'Field\'s order', + 'required' => true, + 'example' => 1, + 'type' => 'number', + 'custom' => [], + ], + ], + ], + ]; + } + protected function assertStringsEqualNormalizingNewlines(string $expected, string $actual) { $this->assertEquals(str_replace("\r", "", $expected), str_replace("\r", "", $actual));