Closed knvpk closed 4 years ago
Are you using any custom strategies?
On Tue, Sep 22, 2020, 15:52 Pavan kumar notifications@github.com wrote:
- I confirm that I have read and attempted the tips in the Troubleshooting Guide https://scribe.readthedocs.io/en/latest/troubleshooting.html.
What happened?
- I set my configuration to... openapi.enabled = true openapi.overrides = []
- Then I ran php artisan scribe:generate ...
- But I saw... an error as below stack trace instead of... successfull openapi.yaml generation message
Screenshots and stack traces: 🔊 info Writing source Markdown files to: resources/docs 🔊 info Wrote source Markdown files to: resources/docs 🔊 info Generating API HTML code 👍 success Wrote HTML documentation to: public/docs 🔊 info Generating Postman collection 👍 success Wrote Postman collection to: public/docs/collection.json 🔊 info Generating OpenAPI specification
ErrorException : Undefined index: required
at /var/www/api.vpay/vendor/knuckleswtf/scribe/src/Writing/OpenAPISpecWriter.php:187 183| $hasRequiredParameter = false; 184| $hasFileParameter = false; 185| 186| foreach ($endpoint['bodyParameters'] as $name => $details) {
187| if ($details['required']) { 188| $hasRequiredParameter = true; 189| // Don't declare this earlier. 190| // Can't have an empty required array. Must have something there. 191| $schema['required'][] = $name;
Exception trace:
1 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined index: required", "/var/www/api.vpay/vendor/knuckleswtf/scribe/src/Writing/OpenAPISpecWriter.php")
/var/www/api.vpay/vendor/knuckleswtf/scribe/src/Writing/OpenAPISpecWriter.php:187
2 Knuckles\Scribe\Writing\OpenAPISpecWriter::generateEndpointRequestBodySpec()
/var/www/api.vpay/vendor/knuckleswtf/scribe/src/Writing/OpenAPISpecWriter.php:76
Please use the argument -v to see more details.
My environment:
- PHP version (from php -v): 7.2.33
- Framework (Laravel/Lumen): Laravel 5.8
- Laravel/Lumen version (from composer show laravel/framework or composer show lumen/framework): 5.8.38
- Scribe version (from composer show knuckleswtf/scribe): 1.8
My Scribe config (minus the comments):
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/knuckleswtf/scribe/issues/102, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADNSD4PDV3H7I5VHOVQ2SDTSHC22PANCNFSM4RV4BQSQ .
Yes I am using the custom strategry, we keep our validation rules (indirectly body params) in static function of thier respective models and in my custom strategy i parsing them as below
$parameters = collect($tags)
->filter(function ($tag) {
return $tag instanceof Tag && $tag->getName() === 'rules';
})
->mapWithKeys(function (Tag $tag) {
// Format:
// @bodyParam <name> <type> <"required" (optional)> <description>
// Examples:
// @bodyParam text string required The text.
// @bodyParam user_id integer The ID of the user.
$result = str_replace('@', '::', $tag->getContent())();
$data = [];
foreach ($result as $k=>$r) {
$str = $k . ' ' . str_replace('|', ' ', $r);
$tagContent = $str;
preg_match('/(.+?)\s+(.+?)\s+(required\s+)?([\s\S]*)/', $tagContent, $content);
$content = preg_replace('/\s?No-example.?/', '', $content);
if (empty($content)) {
// this means only name and type were supplied
[$name, $type] = preg_split('/\s+/', $tagContent);
$required = false;
$description = '';
} else {
[$_, $name, $type, $required, $description] = $content;
$description = trim($description);
if ($description == 'required' && empty(trim($required))) {
$required = $description;
$description = '';
}
$required = trim($required) == 'required' ? true : false;
}
$type = $this->normalizeParameterType($type);
[$description, $example] = $this->parseExampleFromParamDescription($description, $type);
$value = is_null($example) && ! $this->shouldExcludeExample($tagContent)
? $this->generateDummyValue($type)
: $example;
$data = array_merge($data,[
$name => compact('type', 'description', 'value')
]);
}
return $data;
})->toArray();
return $parameters;
I solved by checking the code in GetFromFormRequest
class, infact using that class method as below
$methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method'];
$tags = $methodDocBlock->getTags();
$rulesTag = collect($tags)
->filter(function ($tag) {
return $tag instanceof Tag && $tag->getName() === 'rules';
})->first();
$rules = [];
if (!is_null($rulesTag)) {
$rules = str_replace('@', '::', $rulesTag->getContent())();
}
$rulesMetaTag = collect($tags)
->filter(function ($tag) {
return $tag instanceof Tag && $tag->getName() === 'rulesMeta';
})->first();
$customParametersData = [];
if (!is_null($rulesMetaTag)) {
$customParametersData = str_replace('@', '::', $rulesMetaTag->getContent())();
}
return app(GetFromFormRequest::class)->getBodyParametersFromValidationRules($rules, $customParametersData);
Doing like this also able to generate the open api spec successfully
@shalvah , am i doing correct?
Well, I don't know if you need to copy the code from that other strategy, but it looks like the problem here is your custom strategy isn't returning the required
key as part of the parameter data. Adding that should fix that, I think.
What happened?
openapi.enabled = true
openapi.overrides = []
php artisan scribe:generate
...openapi.yaml
generation messageScreenshots and stack traces: 🔊 info Writing source Markdown files to: resources/docs 🔊 info Wrote source Markdown files to: resources/docs 🔊 info Generating API HTML code 👍 success Wrote HTML documentation to: public/docs 🔊 info Generating Postman collection 👍 success Wrote Postman collection to: public/docs/collection.json 🔊 info Generating OpenAPI specification
ErrorException : Undefined index: required
at /var/www/api.vpay/vendor/knuckleswtf/scribe/src/Writing/OpenAPISpecWriter.php:187 183| $hasRequiredParameter = false; 184| $hasFileParameter = false; 185| 186| foreach ($endpoint['bodyParameters'] as $name => $details) {
Exception trace:
1 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined index: required", "/var/www/api.vpay/vendor/knuckleswtf/scribe/src/Writing/OpenAPISpecWriter.php") /var/www/api.vpay/vendor/knuckleswtf/scribe/src/Writing/OpenAPISpecWriter.php:187
2 Knuckles\Scribe\Writing\OpenAPISpecWriter::generateEndpointRequestBodySpec() /var/www/api.vpay/vendor/knuckleswtf/scribe/src/Writing/OpenAPISpecWriter.php:76
Please use the argument -v to see more details.
My environment:
php -v
): 7.2.33composer show laravel/framework
orcomposer show lumen/framework
): 5.8.38composer show knuckleswtf/scribe
): 1.8My Scribe config (minus the comments):