Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sample body with array of object #720

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading