Skip to content

Commit

Permalink
Remove array setter when default param type to object
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Dec 2, 2022
1 parent 13c6f68 commit f0a3205
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Extracting/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public static function cleanParams(array $parameters): array
if (Str::contains($paramName, '.')) { // Object field (or array of objects)
self::setObject($cleanParameters, $paramName, $details->example, $parameters, $details->required);
} else {
$cleanParameters[$paramName] = $details->example instanceof \stdClass ? $details->example : $details->example;
$cleanParameters[$paramName] = $details->example;
}
}

Expand Down
17 changes: 7 additions & 10 deletions src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,9 @@ public function convertGenericArrayType(array $parameters): array
if ($childKey = Arr::first($allKeys, fn($key) => Str::startsWith($key, "$name.*"))) {
$childType = ($converted[$childKey] ?? $parameters[$childKey])['type'];
$details['type'] = "{$childType}[]";
} elseif (Arr::first($allKeys, fn($key) => Str::startsWith($key, "$name."))) {
$details['type'] = 'object';
} else {
} else { // `array` types default to `object` if no subtype is specified
$details['type'] = 'object';
unset($details['setter']);
}
}

Expand Down Expand Up @@ -620,14 +619,12 @@ public function setExamples(array $parameters): array
// and we automatically set this as the example for `data.title`
// Note that this approach assumes parent fields are listed before the children; meh.
$examples[$details['name']] = $details['example'];
} else {
} elseif (preg_match('/.+\.[^*]+$/', $details['name'])) {
// For object fields (eg 'data.details.title'), set examples from their parents if present as described above.
if (preg_match('/.+\.[^*]+$/', $details['name'])) {
[$parentName, $fieldName] = preg_split('/\.(?=[\w-]+$)/', $details['name']);
if (array_key_exists($parentName, $examples) && is_array($examples[$parentName])
&& array_key_exists($fieldName, $examples[$parentName])) {
$examples[$details['name']] = $details['example'] = $examples[$parentName][$fieldName];
}
[$parentName, $fieldName] = preg_split('/\.(?=[\w-]+$)/', $details['name']);
if (array_key_exists($parentName, $examples) && is_array($examples[$parentName])
&& array_key_exists($fieldName, $examples[$parentName])) {
$examples[$details['name']] = $details['example'] = $examples[$parentName][$fieldName];
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Strategies/GetFromFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,28 @@ public function sets_examples_from_parent_if_set()
$this->assertEquals($dataExample['meta']['tags'], $parsed['data.meta.tags']['example']);
}

/** @test */
public function generates_proper_examples_if_not_set()
{
$strategy = new BodyParameters\GetFromFormRequest(new DocumentationConfig([]));
$parametersFromFormRequest = $strategy->getParametersFromValidationRules(
[
'data' => 'array|required',
'data.title' => 'string|required',
'data.meta' => 'array',
'data.meta.tags' => 'array',
'data.meta.tags.*' => 'string',
],
[]
);

$parsed = $strategy->normaliseArrayAndObjectParameters($parametersFromFormRequest);
$this->assertEquals([], $parsed['data']['example']);
$this->assertTrue(is_string($parsed['data.title']['example']));
$this->assertNull($parsed['data.meta']['example']); // null because not required
$this->assertTrue(is_array($parsed['data.meta.tags']['example']));
$this->assertTrue(is_string($parsed['data.meta.tags']['example'][0]));
}

/** @test */
public function creates_missing_parent_fields()
Expand Down

0 comments on commit f0a3205

Please sign in to comment.