adrenalinkin/swagger-resolver-bundle
Composer 安装命令:
composer require adrenalinkin/swagger-resolver-bundle
包简介
Provides possibility for validate data according to Swagger documentation
关键字:
README 文档
README
Feel free to connect with me by email or in Telegram.
Usage example on GitHub Gist
Introduction
Bundle provides possibility for validate data according to the Swagger 2 documentation. You describe your API documentation by Swagger and provides verification of data for compliance with the described requirements. When documentation has been updated then verification will be updated too, all in one place!
Documentation is cached through the standard Symfony Warmers mechanism.
In debug mode, the cache automatically warms up if you change the file containing the description of the documentation.
Note: as result bundle returns SwaggerResolver object - extension for the
OptionsResolver.
In this way you get full control over created resolver.
Attention: remember, when you change generated SwaggerResolver object you risk getting
divergence with actual documentation.
Integrations
Bundle provides integration with NelmioApiDocBundle,
supports configuration loading by swagger-php and also supports
loading directly from the json or yaml(yml) configuration file.
When used default bundle configuration then swagger documentation will be load in most optimal available way.
Loaders priority:
NelmioApiDocBundle- do not require any additional configuration.swagger-php- scansrc/directory by default. Usesswagger_php.scanandswagger_php.excludeparameters.json- looking forweb/swagger.jsonby default. Usesconfiguration_fileparameter.
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
composer require adrenalinkin/swagger-resolver-bundle
is command requires you to have Composer install globally.
Step 2: Enable the Bundle
Then, enable the bundle by updating your app/AppKernel.php file to enable the bundle:
<?php declare(strict_types=1); // app/AppKernel.php class AppKernel extends Kernel { // ... public function registerBundles() { $bundles = [ // ... new Linkin\Bundle\SwaggerResolverBundle\LinkinSwaggerResolverBundle(), ]; return $bundles; } // ... }
Configuration
To start using bundle you don't need to define some additional configuration. All parameters have values by default:
# app/config.yml linkin_swagger_resolver: # default parameter locations which can apply normalization enable_normalization: - 'query' - 'path' - 'header' # strategy for merge all request parameters. path_merge_strategy: Linkin\Bundle\SwaggerResolverBundle\Merger\Strategy\StrictMergeStrategy configuration_loader_service: ~ # the name of the configuration loader service configuration_file: ~ # full path to the configuration file swagger_php: # settings for the swagger-php scan: ~ # array of the full paths for the annotations scan exclude: ~ # array of the full paths which should be excluded
Usage
Step 1: Swagger documentation preparation
Swagger documentation preparation differ according to used tools of your project.
NelmioApiDocBundle
If your project has NelmioApiDocBundle connected, then no additional configuration is required.
swagger-php
In the absence of NelmioApiDocBundle, the bundle will degrade to the configuration
loading by swagger-php annotations. In this case, by default, will be used src/ directory to scan.
To optimize scanning process you can describe directories in the configuration:
# app/config.yml linkin_swagger_resolver: swagger_php: scan: - '%kernel.project_dir%/src/Acme/ApiBundle' exclude: - '%kernel.project_dir%/src/Acme/ApiBundle/Resources' - '%kernel.project_dir%/src/Acme/ApiBundle/Repository'
JSON
In the absence of NelmioApiDocBundle and swagger-php, the bundle will degrade to the configuration
loading by json file. In this case, by default, will be used web/swagger.json file.
Also, you can set custom path to the json configuration:
# app/config.yml linkin_swagger_resolver: configuration_file: '%kernel.project_dir%/web/swagger.json' # json extension is required
YAML or (yml)
In the absence of NelmioApiDocBundle and swagger-php, but available
configuration file in the yaml or yml format you need to define path to that file:
# app/config.yml linkin_swagger_resolver: configuration_file: '%kernel.project_dir%/web/swagger.yaml' # yaml or yml extension is required
Custom
If you need to use custom configuration loader you should implement custom loading process in the class, which implements SwaggerConfigurationLoaderInterface interface. After that you need to define name of that service in the configuration:
# app/config.yml linkin_swagger_resolver: configuration_loader_service: acme_app.custom_configuration_loader
Step 2: Data validation
Validation for model
<?php declare(strict_types=1); /** @var \Linkin\Bundle\SwaggerResolverBundle\Factory\SwaggerResolverFactory $factory */ $factory = $container->get('linkin_swagger_resolver.factory'); // loading by fully qualified class name $swaggerResolver = $factory->createForDefinition(AcmeApiModel::class); // loading by class name $swaggerResolver = $factory->createForDefinition('AcmeApiModel'); /** @var \Symfony\Component\HttpFoundation\Request $request */ $data = $swaggerResolver->resolve(json_decode($request->getContent(), true));
Validation for request
<?php declare(strict_types=1); /** @var \Linkin\Bundle\SwaggerResolverBundle\Factory\SwaggerResolverFactory $factory */ $factory = $container->get('linkin_swagger_resolver.factory'); $request = $container->get('request_stack')->getCurrentRequest(); // Loading by request $swaggerResolver = $factory->createForRequest($request); $data = $swaggerResolver->resolve(json_decode($request->getContent(), true));
Advanced
Custom validator
Bundle validates the data through the validator system.
List of all validators, you can find out by going to the Validator folder.
All validators registered as tagging services. To create your own validator it is enough to create class,
which implements SwaggerValidatorInterface and then
register it under the tag linkin_swagger_resolver.validator.
Custom normalizer
Bundle validates the data through the normalizer system.
List of all normalizers, you can find out by going to the Normalizer folder.
All normalizers registered as tagging services. To create your own normalizer it is enough to create class,
which implements SwaggerNormalizerInterface and then
register it under the tag linkin_swagger_resolver.normalizer.
Running the tests and static analysis tools
Download the project:
git clone git@github.com:adrenalinkin/swagger-resolver-bundle.git
cd swagger-resolver-bundle
Follow this instruction for set up simple docker container.
Install composer dependencies:
composer update
For run the test suite:
bin/simple-phpunit
For run the PHP-CS-Fixer:
php-cs-fixer fix --diff
For run the composer-normalize:
composer-normalize --dry-run
License
adrenalinkin/swagger-resolver-bundle 适用场景与选型建议
adrenalinkin/swagger-resolver-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.3k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2018 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「documentation」 「api」 「validation」 「resolver」 「swagger」 「Swagger-PHP」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adrenalinkin/swagger-resolver-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adrenalinkin/swagger-resolver-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adrenalinkin/swagger-resolver-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
Laravel package that generates RESTful API documentation in Markdown based on PHPDoc.
A PSR-7 compatible library for making CRUD API endpoints
Laravel API documentation generating tool
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 13.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-27