Skip to content

Commit

Permalink
Support specifying Example: null in annotations (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
liverocaisson authored Nov 19, 2023
1 parent 7b5d1b6 commit 6ecd6a8
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 17 deletions.
1 change: 1 addition & 0 deletions camel/Extraction/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Parameter extends BaseDTO
public mixed $example = null;
public string $type = 'string';
public array $enumValues = [];
public bool $exampleWasSpecified = false;

public function __construct(array $parameters = [])
{
Expand Down
9 changes: 6 additions & 3 deletions src/Extracting/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@ public static function cleanParams(array $parameters): array
* @var Parameter $details
*/
foreach ($parameters as $paramName => $details) {
// Remove params which have no examples and are optional.
if (is_null($details->example) && $details->required === false) {
continue;

// Remove params which have no intentional examples and are optional.
if (!$details->exampleWasSpecified) {
if (is_null($details->example) && $details->required === false) {
continue;
}
}

if ($details->type === 'file') {
Expand Down
13 changes: 10 additions & 3 deletions src/Extracting/ParamHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,21 @@ protected function shouldExcludeExample(string $description): bool
*/
protected function parseExampleFromParamDescription(string $description, string $type): array
{
$exampleWasSpecified = false;
$example = null;
$enumValues = [];

if (preg_match('/(.*)\bExample:\s*([\s\S]+)\s*/s', $description, $content)) {
$exampleWasSpecified = true;
$description = trim($content[1]);

// Examples are parsed as strings by default, we need to cast them properly
$example = $this->castToType($content[2], $type);
if ($content[2] == 'null') {
// If we intentionally put null as example we return null as example
$example = null;
} else {
// Examples are parsed as strings by default, we need to cast them properly
$example = $this->castToType($content[2], $type);
}
}

if (preg_match('/(.*)\bEnum:\s*([\s\S]+)\s*/s', $description, $content)) {
Expand All @@ -255,6 +262,6 @@ protected function parseExampleFromParamDescription(string $description, string
);
}

return [$description, $example, $enumValues];
return [$description, $example, $enumValues, $exampleWasSpecified];
}
}
4 changes: 2 additions & 2 deletions src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,14 +660,14 @@ public function convertArraySubfields(array $parameters): array
foreach ($parameters as $name => $details) {
if (Str::endsWith($name, '.*')) {
// The user might have set the example via bodyParameters()
$hasExample = $this->examplePresent($details);
$exampleWasSpecified = $this->examplePresent($details);

// Change cars.*.dogs.things.*.* with type X to cars.*.dogs.things with type X[][]
while (Str::endsWith($name, '.*')) {
$details['type'] .= '[]';
$name = substr($name, 0, -2);

if ($hasExample) {
if ($exampleWasSpecified) {
$details['example'] = [$details['example']];
} else if (isset($details['setter'])) {
$previousSetter = $details['setter'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function parseTag(string $tagContent): array
}

$type = static::normalizeTypeName($type);
[$description, $example, $enumValues] =
[$description, $example, $enumValues, $exampleWasSpecified] =
$this->getDescriptionAndExample($description, $type, $tagContent, $name);

return compact('name', 'type', 'description', 'required', 'example', 'enumValues');
return compact('name', 'type', 'description', 'required', 'example', 'enumValues', 'exampleWasSpecified');
}
}
12 changes: 9 additions & 3 deletions src/Extracting/Strategies/GetFieldsFromTagStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ protected function getDescriptionAndExample(
string $description, string $type, string $tagContent, string $fieldName
): array
{
[$description, $example, $enumValues] = $this->parseExampleFromParamDescription($description, $type);
$example = $this->setExampleIfNeeded($example, $type, $tagContent, $fieldName, $enumValues);
return [$description, $example, $enumValues];
[$description, $example, $enumValues, $exampleWasSpecified] = $this->parseExampleFromParamDescription($description, $type);

if($exampleWasSpecified && $example === null) {
$example = null;
} else {
$example = $this->setExampleIfNeeded($example, $type, $tagContent, $fieldName, $enumValues);
}

return [$description, $example, $enumValues, $exampleWasSpecified];
}

protected function setExampleIfNeeded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function parseTag(string $tagContent): array

}

[$description, $example, $enumValues] =
[$description, $example, $enumValues, $exampleWasSpecified] =
$this->getDescriptionAndExample($description, $type, $tagContent, $name);

return compact('name', 'description', 'required', 'example', 'type', 'enumValues');
return compact('name', 'description', 'required', 'example', 'type', 'enumValues', 'exampleWasSpecified');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ protected function parseTag(string $tagContent): array
: static::normalizeTypeName($type);
}

[$description, $example, $enumValues] =
[$description, $example, $enumValues, $exampleWasSpecified] =
$this->getDescriptionAndExample($description, $type, $tagContent, $name);

return compact('name', 'description', 'required', 'example', 'type', 'enumValues');
return compact('name', 'description', 'required', 'example', 'type', 'enumValues', 'exampleWasSpecified');
}
}
1 change: 1 addition & 0 deletions tests/GenerateDocumentation/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ public function will_not_overwrite_manually_modified_content_unless_force_flag_i
'type' => 'integer',
'enumValues' => [],
'custom' => [],
'exampleWasSpecified' => false,
];
$group['endpoints'][0]['urlParameters']['a_param'] = $extraParam;
file_put_contents($firstGroupFilePath, Yaml::dump(
Expand Down
25 changes: 25 additions & 0 deletions tests/Strategies/BodyParameters/GetFromBodyParamTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ public function can_fetch_from_bodyparam_tag()
], $results);
}

/** @test */
public function retains_null_as_example_if_specified()
{
$tags = [
new Tag('bodyParam', 'id int required The id to use. Leave null to autogenerate. Example: null'),
new Tag('bodyParam', 'key string A key. Example: null'),
];
$results = $this->strategy->getFromTags($tags);

$this->assertArraySubset([
'id' => [
'type' => 'integer',
'required' => true,
'description' => 'The id to use. Leave null to autogenerate.',
'example' => null,
],
'key' => [
'type' => 'string',
'required' => false,
'description' => 'A key.',
'example' => null,
],
], $results);
}

/** @test */
public function can_fetch_from_bodyparam_tag_for_array_body()
{
Expand Down

0 comments on commit 6ecd6a8

Please sign in to comment.