php-solution/swagger-ui-gen
Composer 安装命令:
composer require php-solution/swagger-ui-gen
包简介
Extra functionality for generate Swagger specification
README 文档
README
At now this lib is under development.
SwaggerUIGenerator
This lib includes components and Symfony bundle for generate OpenApi specification.
Install
Via Composer
$ composer require php-solution/swagger-ui-gen
Integration to Project
-
Copy to web document root swagger-ui dist files
-
Copy to /web/assets/swagger
- index.html
- swagger-ui.css
- swagger-ui.js
- swagger-ui-bundle.js
- swagger-ui-standalone-preset.js
-
Change on index.html url to swagger openapi specification
const ui = SwaggerUIBundle({
url: "http://localhost/assets/swagger-spec.json",
})
Add Symfony Configuration
swagger_ui_gen:
options_provider:
defaults:
- '%kernel.root_dir%/Resources/swagger-doc/defaults.yml'
files:
- '%kernel.root_dir%/Resources/swagger-doc/general.yml'
- '%kernel.root_dir%/Resources/swagger-doc/tags.yml'
- '%kernel.root_dir%/Resources/swagger-doc/paths.yml'
- '%kernel.root_dir%/Resources/swagger-doc/security_def.yml'
- '%kernel.root_dir%/Resources/swagger-doc/definitions.yml'
folders:
- '@ProjectAdminBundle/Resources/config/swagger-doc'
handlers:
validator: false
form: false
form_validator: false
serializer: false
doctrine_orm: false
naming_strategy_service: 'PhpSolution\SwaggerUIGen\Bundle\ModelHandler\PropertyNaming\UnderscoreNamingStrategy'
Generate file with Swagger Specification
php bin\console swagger-gen:generate-spec --path=./web/assets/swagger-spec.json
Examples
Symfony project
See example of Symfony app files on /examples/project.
Debug mode for Symfony
- Add route to your general route.yml
_swagger_ui_gen: resource: '@SwaggerUIGenBundle/Resources/config/routing.yml'
- Add to swagger ui bootstrap html file(index.html):
<script>
window.onload = function() {
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
const debugDataUrl = getUrlParameter('url');
const ui = SwaggerUIBundle({
url: debugDataUrl ? debugDataUrl : "./data.json",
...
</script>
- Use on browser:
http://localhost/path-to-swagger-ui-bootstrap-html.html?url=/swagger-ui-gen/data.json
- See dumped data
http://localhost/swagger-ui-gen/dump
Example of symfony route specification
sf_route_paths: - route: 'get_list_of_entity' # symfony route name, required tags: ['admin'] # openapi tags, not required, default value: [] schemes: [] # openapi schemes, not required, default value: [] response: # use for openapi operation response, not required, default value: [] status_code: 200 # not required, default value: "default" type: 'object' # not required, default value: "object", complex values: ['array', 'object', 'collection'] mapping: type: ['doctrine', 'serializer'] # not required, use all if builders empty class: 'Project\AdminBundle\Lib\PaginatedCollection' # required serializer_groups: ['list'] # use only for serializer properties: items: # property name, require type: 'collection' mapping: {class: 'Project\AdminBundle\Entity\Admin'} openapi_params: # not required, default value: [] get: summary: 'Get admin list' responses: << : *defaultResponceErrors security: - api_key: [] - route: 'post_with_form_dto' tags: ['admin'] request: form_class: 'Project\AdminBundle\Form\Type\AdminCreateType' form_options: {validation_groups: ['create']} validation_class: 'Project\AdminBundle\Lib\AdminModel' validation_groups: ['create'] response: type: 'object' mapping: type: ['doctrine', 'serializer'] class: 'Project\AdminBundle\Entity\Admin' serializer_groups: ['get'] - route: 'post_with_form_multiple_dto' tags: ['admin'] request: form_class: 'Project\AdminBundle\Form\Type\MultipleAdminType' in: 'body' # use this option if you want to send data as json response: type: 'collection' openapi_params: {$ref: '#/definitions/Admin'}
Example of definition specification
sf_object_definitions: - name: 'Admin' mapping: type: ['doctrine', 'serializer'] class: 'Project\AdminBundle\Entity\Admin' - name: 'AdminCreate' type: 'object' mapping: type: ['validator'] validation_groups: ['create'] class: 'Project\AdminBundle\Lib\AdminListModel' # Some DTO properties: admins: type: 'collection' mapping: {class: 'Project\AdminBundle\Lib\AdminModel'} openapi_params: properties: email: {uniqueItems: true}
TODO:
- Use SF Configuration component as normalizer for DefinitionsBuilder, RoutePaths
- JMSSerializer Schema builder
- Tests
php-solution/swagger-ui-gen 适用场景与选型建议
php-solution/swagger-ui-gen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79.99k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 php-solution/swagger-ui-gen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-solution/swagger-ui-gen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 79.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-16