Skip to content

Commit

Permalink
Add support for example value for routes using @bodyParam (fix #365)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Oct 12, 2018
1 parent 562f71f commit 7cdc567
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions resources/views/partials/route.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
curl -X {{$parsedRoute['methods'][0]}} {{$parsedRoute['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($parsedRoute['uri'], '/') }}" \
-H "Accept: application/json"@if(count($parsedRoute['headers'])) \
@foreach($parsedRoute['headers'] as $header => $value)
-H "{{$header}}"="{{$value}}" @if(! ($loop->last))\
-H "{{$header}}: {{$value}}" @if(! ($loop->last))\
@endif
@endforeach
@endif
@if(count($parsedRoute['parameters'])) \
@foreach($parsedRoute['parameters'] as $attribute => $parameter)
-d "{{$attribute}}"="{{$parameter['value']}}" @if(! ($loop->last))\
-d "{{$attribute}}"={{$parameter['value']}} @if(! ($loop->last))\
@endif
@endforeach
@endif
Expand Down Expand Up @@ -71,7 +71,7 @@
Parameter | Type | Status | Description
--------- | ------- | ------- | ------- | -----------
@foreach($parsedRoute['parameters'] as $attribute => $parameter)
{{$attribute}} | {{$parameter['type']}} | @if($parameter['required']) required @else optional @endif | {!! implode(' ',$parameter['description']) !!}
{{$attribute}} | {{$parameter['type']}} | @if($parameter['required']) required @else optional @endif | {!! $parameter['description'] !!}
@endforeach
@endif

Expand Down
35 changes: 34 additions & 1 deletion src/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mpociot\ApiDoc\Generators;

use Faker\Factory;
use ReflectionClass;
use Illuminate\Support\Str;
use League\Fractal\Manager;
Expand Down Expand Up @@ -114,8 +115,9 @@ protected function getParametersFromDocBlock($tags)
list($_, $name, $type, $required, $description) = $content;
$required = trim($required) == 'required' ? true : false;
$type = $this->normalizeParameterType($type);
$value = $this->getDummyValue($type);

return [$name => compact('type', 'description', 'required')];
return [$name => compact('type', 'description', 'required', 'value')];
})->toArray();

return $parameters;
Expand Down Expand Up @@ -382,8 +384,39 @@ private function normalizeParameterType($type)
$typeMap = [
'int' => 'integer',
'bool' => 'boolean',
'double' => 'float',
];

return $type ? ($typeMap[$type] ?? $type) : 'string';
}

private function getDummyValue(string $type)
{
$faker = Factory::create();
$fakes = [
'integer' => function () {
return rand(1, 20);
},
'number' => function () use ($faker) {
return $faker->randomFloat();
},
'float' => function () use ($faker) {
return $faker->randomFloat();
},
'boolean' => function () use ($faker) {
return $faker->boolean();
},
'string' => function () use ($faker) {
return str_random();
},
'array' => function () {
return "[]";
},
'object' => function () {
return "{}";
},
];

return $fakes[$type]() ?? $fakes['string']();
}
}

0 comments on commit 7cdc567

Please sign in to comment.