Skip to content

Commit

Permalink
Fix sample body with array of object (#720)
Browse files Browse the repository at this point in the history
* Fix sample body with array of object

* Fix indent

* Rename methods
  • Loading branch information
stephane-monnot authored Sep 3, 2023
1 parent 8407835 commit 16a879d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Tools/WritingUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
76 changes: 76 additions & 0 deletions tests/Unit/WritingUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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));
Expand Down

0 comments on commit 16a879d

Please sign in to comment.