cebe / yii2-openapi

REST API application generator for Yii2, openapi 3.0 YAML -> Yii2
MIT License
130 stars 23 forks source link

Crashes when required is not specified #92

Closed davidnewcomb closed 2 years ago

davidnewcomb commented 2 years ago
Running 'REST API Generator'...

PHP Warning 'yii\base\ErrorException' with message 'Invalid argument supplied for foreach()'

in /backend-php/vendor/cebe/yii2-openapi/src/generator/ApiGenerator.php:645

Stack trace:
#0 /backend-php/vendor/cebe/yii2-openapi/src/generator/ApiGenerator.php(645): yii\base\ErrorHandler->handleError(2, 'Invalid argumen...', '/Users/davidnew...', 645, Array)
#1 /backend-php/vendor/cebe/yii2-openapi/src/generator/ApiGenerator.php(890): cebe\yii2openapi\generator\ApiGenerator->generateModels()
#2 /backend-php/vendor/yiisoft/yii2-gii/src/console/GenerateAction.php(53): cebe\yii2openapi\generator\ApiGenerator->generate()
#3 /backend-php/vendor/yiisoft/yii2-gii/src/console/GenerateAction.php(36): yii\gii\console\GenerateAction->generateCode()
#4 [internal function]: yii\gii\console\GenerateAction->run()
#5 /backend-php/vendor/yiisoft/yii2/base/Action.php(94): call_user_func_array(Array, Array)
#6 /backend-php/vendor/yiisoft/yii2/base/Controller.php(181): yii\base\Action->runWithParams(Array)
#7 /backend-php/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('api', Array)
#8 /backend-php/vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction('api', Array)
#9 /backend-php/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('gii/api', Array)
#10 /backend-php/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction('gii/api', Array)
#11 /backend-php/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))
#12 /backend-php/yii(9): yii\base\Application->run()
#13 {main}
14:05:23 /backend-php 510 (master) $ ./yii gii/api --openApiPath=$PWD/openapi.yaml 
Running 'REST API Generator'...

PHP Warning 'yii\base\ErrorException' with message 'Invalid argument supplied for foreach()'

Fixed in ApiGenerator::generateModels with

            if (isset($schema->required)) { // <-----
                foreach ($schema->required as $property) {
                    if (!isset($attributes[$property])) {
                        continue;
                    }
                    $attributes[$property]['required'] = true;
                }
            } // <-----

If I don't specify a list of required fields they must all default to not required, but it shouldn't blow up!

cebe commented 2 years ago

Please try version 2.0-alpha instead of 1.0.x, we have changed and fixed a lot in 2.0.