承接 wilkques/openapi 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wilkques/openapi

Composer 安装命令:

composer require wilkques/openapi

包简介

Auto generates the Swagger.io documentation for a laravel project

README 文档

README

Laravel OAS3 scans your Laravel project's endpoints and auto generates a OAS3 documentation for you.

TESTS Latest Stable Version License

About

This package is heavily inspired by the mtrajano/laravel-swagger

Usage is pretty similar to the mtrajano/laravel-swagger

Laravel OAS3 works based on recommended practices by Laravel. It will parse your routes and generate a path object for each one. If you inject Form Request classes in your controller's actions as request validation, it will also generate the parameters for each request that has them. For the parameters, it will take into account wether the request is a GET/HEAD/DELETE or a POST/PUT/PATCH request and make its best guess as to the type of parameter object it should generate. It will also generate the path parameters if your route contains them. Finally, this package will also scan any documentation you have in your action methods and add it as summary and description to that path, along with any appropriate annotations such as @deprecated.

One thing to note is this library leans on being explicit. It will choose to include keys even if they have a default. For example it chooses to say a route has a deprecated value of false rather than leaving it out. I believe this makes reading the documentation easier by not leaving important information out. The file can be easily cleaned up afterwards if the user chooses to leave out the defaults.

Installation

The package can easily be installed by running composer require wilkques/openapi in your project's root folder.

If you are running a version of Laravel < 5.5 also make sure you add Wilkques\OpenAPI\OpenAPIServiceProvider::class to the providers array in config/app.php.

This will register the artisan command that will be available to you.

You can also override the default config provided by the application by running php artisan vendor:publish --provider "Wilkques\OpenAPI\OpenAPIServiceProvider" in your projects root and change the configuration in the new config/openapi.php file created.

Usage

Generating the OpenAPI documentation is easy, simply run php artisan openapi:generate in your project root. Keep in mind the command will simply print out the output in your console. If you want the docs saved in a file you can reroute the output like so: php artisan openapi:generate > openapi.json

If you wish to generate docs for a subset of your routes, you can pass a filter using --filter or --except, for example: php artisan openapi:generate --filter="/api" --except="/api/v1"

By default, laravel-openapi prints out the documentation in json format, if you want it in YAML format you can override the format using the --format flag. Make sure to have the yaml extension installed if you choose to do so.

Format options are:
json
yaml

Example

Say you have a route /api/users/{id} that maps to UserController@show

Your sample controller might look like this:

@Response() @Request() @Servers() @Path() input json format or yaml format (need php YAML extension)

/**
 * @Servers([
 *      {
 *          "url": "{schema}://example.com",
 *          "description": "local server",
 *          "variables": {
 *              "schema": {
 *                  "enum": ["https", "http"],
 *                  "default": "http"
 *              }
 *          }
 *      }
 * ])
 */
class TestController extends Controller
{
    /**
     * test controller
     * 
     * @Path({
     *      "id": {
     *          "description": "id",
     *          "example": "123456789"
     *      }
     * })
     * @Request({
     *      "summary": "test get /api/test index",
     *      "description": "Test route description",
     *      "tags": ["User"],
     *      "security": [{"bearerAuth": []}]
     * })
     * @Response({
     *     "code": 302
     * })
     * @Response({
     *     "code": 400
     * })
     * @Response({
     *     "code": 500
     * })
     * @Response({
     *      "code": 200,
     *      "body": {
     *          "data": {
     *              "type": "array",
     *              "items": {
     *                  "type": "object",
     *                  "properties": {
     *                      "id": {
     *                          "type": "integer",
     *                          "description": "file id",
     *                          "example": 1
     *                      },
     *                      "name": {
     *                          "type": "string",
     *                          "description": "file name",
     *                          "example": "name"
     *                      }
     *                  }
     *              }
     *          }
     *      }
     * })
     */
    public function update(UserUpdateRequest $request)

    // or

    /**
     * test yaml controller
     * 
     * @Path(
     *      id:
     *          description: "id"
     *          example: 123456789
     * 
     * )
     * @Request(
     *      summary: "test get /api/test index"
     *      description: "Test route descriptioncd"
     *      tags:
     *      - "User"
     *      security: 
     *      - bearerAuth: []
     * )
     * @Response(
     *     code: 302
     * )
     * @Response(
     *     code: 400
     * )
     * @Response(
     *     code: 500
     * )
     * @Response(
     *      code: 200
     *      body:
     *          data:
     *              type: "array"
     *              items:
     *                  type: "object"
     *                  properties:
     *                      id:
     *                          type: "integer"
     *                          description: "file id"
     *                          example: 1
     *                      name: 
     *                          type: "string"
     *                          description: "file name"
     *                          example: "name"
     * )
     */
    public function yaml()

Or custom Request

/**
 * @Request({
 *      "summary": "test get /api/test index",
 *      "description": "Test route description",
 *      "tags": ["Test"],
 *      "security": [{"bearerAuth": []}],
 *      "parameters": [
 *          {
 *              "in": "query",
 *              "name": "test",
 *              "required": "false",
 *              "description": "Test description",
 *              "schema": {
 *                  "type": "string",
 *                  "enum": [1, 2],
 *                  "example": "1"
 *              }
 *          }
 *      ],
 *      "requestBody": {
 *          "custom": "only", // If the value of the custom parameter is "only", then display only the custom parameters. If the value is "merge", then merge the custom parameters with the results of the FormRequest. The default value is "merge".
 *          "content": {
 *              "multipart/form-data": {
 *                  "schema": {
 *                      "type": "object",
 *                      "required": ["file"],
 *                      "properties": {
 *                          "file":{
 *                              "type": "string",
 *                              "format": "binary",   
 *                              "description": "file upload" 
 *                          },
 *                          "content":{
 *                              "type": "string"
 *                          }
 *                      }
 *                  }
 *              }
 *          }
 *      }
 * })
 */
public function store(StoreRequest $request)

If you don't want generate route. might look like this:

/**
 * @exclude
 */ 
public function destroy($id)

Or

Editor config/openapi.php

'only' => [
    'namespace' => [
        // 'App\Http\Controllers',
    ],
],

'exclude' => [
    'routes' => [
        'uri'   => [
            // route uri
        ],
        'name'  => [
            // route name or as
        ]
    ],
],

And the FormRequest class might look like this:

@Fields() input json string or yaml format (need php YAML extension)

class UserUpdateRequest extends FormRequest
{
    /**
     * @Fields({
     *      "test": {
     *          "description": "Test description",
     *          "example": 1
     *      }
     * })
     */
    public function rules()
    {
        return [
            'test'          => 'in:1,2',
            'file.*.name'   => 'required|string',
            'file.*.path'   => 'required|string',
        ];
    }
}

Running artisan openapi:generate --output=storage/api-docs/api-docs.json will generate the following file:

{
    "openapi": "3.0.3",
    "info": {
        "title": "Laravel",
        "description": "Test",
        "version": "1.0.1"
    },
    "servers": [
        {
            "url": "\/",
            "description": "server",
            "variables": {
                "schema": {
                    "enum": [
                        "https",
                        "http"
                    ],
                    "default": "http"
                }
            }
        }
    ],
    "components": {
        "securitySchemes": {
            "bearerAuth": {
                "type": "http",
                "scheme": "bearer",
                "bearerFormat": "JWT"
            }
        }
    },
    "paths": {
        "\/api\/user\/{id}": {
            "put": {
                "summary": "Return all the details of a user",
                "description": "Returns the user's first name, last name and address
 Please see the documentation [here](https://example.com/users) for more information",
                "deprecated": true,
                "security": [{
                    "bearerAuth": []
                }],
                "tags": ["User"],
                "parameters": [
                    {
                        "in": "path",
                        "name": "id",
                        "required": true,
                        "description": "",
                        "schema": {
                            "type": "integer"
                        }
                    },
                    {
                        "in": "query",
                        "name": "test",
                        "required": false,
                        "description": "Test description",
                        "schema": {
                            "type": "enum",
                            "enum": ["1","2"],
                            "example": 1
                        }
                    }
                ],
                "requestBody": {
                    "content": {
                        "application\/json": {
                            "schema": {
                                "type": "object",
                                "required": [
                                    "file.*.name",
                                    "file.*.path"
                                ],
                                "properties": {
                                    "file": {
                                        "type": "array",
                                        "items": {
                                            "type": "object",
                                            "properties": {
                                                "name": {
                                                    "type": "string",
                                                    "description": "Test file name",
                                                    "example": "file name"
                                                },
                                                "path": {
                                                    "type": "string",
                                                    "description": "Test file path",
                                                    "example": "file path"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "302": {
                        "description": "Found"
                    },
                    "400": {
                        "description": "Bad Request"
                    },
                    "500": {
                        "description": "Internal Server Error"
                    },
                    "200": {
                        "content": {
                            "application\/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "type": "object",
                                                "properties": {
                                                    "id": {
                                                        "type": "integer",           
                                                        "description": "file id",
                                                        "example": 1
                                                    },
                                                    "name": {
                                                        "type": "string",           
                                                        "description": "file name",
                                                        "example": "name"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        "description": "OK"
                    }
                }
            },
        }
    }
}

Reference

wilkques/openapi 适用场景与选型建议

wilkques/openapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 102 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 wilkques/openapi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wilkques/openapi 我们能提供哪些服务?
定制开发 / 二次开发

基于 wilkques/openapi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 102
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-15