Swagger-PHP for generating JSON file for Swagger-UI

落花浮王杯 提交于 2019-12-04 15:55:37

You just put comments aka annotations in your code, model example:

/**
* @SWG\Model(
* id="vps",
* required="['type', 'hostname']",
*  @SWG\Property(name="hostname", type="string"),
*  @SWG\Property(name="label", type="string"),
*  @SWG\Property(name="type", type="string", enum="['vps', 'dedicated']")
* )
*/
class HostVps extends Host implements ResourceInterface
{
    // ...
}

Controller example:

/**
 * @SWG\Resource(
 *  basePath="http://skyapi.dev",
 *  resourcePath="/vps",
 *  @SWG\Api(
 *   path="/vps",
 *   @SWG\Operation(
 *    method="GET",
 *    type="array",
 *    summary="Fetch vps lists",
 *    nickname="vps/index",
 *    @SWG\Parameter(
 *     name="expand",
 *     description="Models to expand",
 *     paramType="query",
 *     type="string",
 *     defaultValue="vps,os_template"
 *    )
 *   )
 *  )
 * )
 */
 class VpsController extends Controller
 {
     // ...
 }

Then in console:

php swagger.phar ./your-code-source/ -o ./directory-for-output-files

Then link generated files in Swagger UI. Is this help?

BTW, this documentation: http://zircote.com/swagger-php/annotations.html is incomplete. It's better to rely on parser errors, example:

php swagger.phar ./skynode-api/api/ -o ./foo
Swagger-PHP 0.9.0
-----------------
[INFO] Skipping unsupported property: "foo" for @Swagger\Annotations\Property, expecting "name", "description", "type", "format", "items", "uniqueItems", "required", "minimum", "maximum", "enum", "defaultValue", "_partialId", "_partials" in HostVps in /home/kane/some-dir/some-file.php on line 3

EDIT: Swagger 2.0 has pretty good specification on GitHub

BTW, consider to use Swagger Editor to create api specification file (json/yaml) to use in Swagger UI. Cause inline SWG documentation in php files is just ugly and you don't have autocomplete support in IDE.

I've struggled on this one lately and see some differences and managed to get it working and wanted to perhaps shortcut someone else:

// In the controller

/**
 * (other swagger stuff like @SWG\Put, etc...)
 *   @SWG\Parameter(name="type",in="path",description="project|task",
 *                  required=true, type="array",             
 *                  @SWG\Items(type="string"), 
 *                  default="project",
 *                  enum={"project", "task"}),
 * (next @SWG\Parameter or closing paran, etc)
 */

I had previous gotten this working with enum in the model and a @SWG\Items reference to it, but didn't save that code (I was just messing around), and I can't get it back. I even see I previously upvoted the question and accepted answer! But the above is the only way I can get it to work right now and is better than nothing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!