webparking/laravel-openapi-validator
Composer 安装命令:
composer require webparking/laravel-openapi-validator
包简介
An OpenAPI documentation validator using your existing Laravel tests
README 文档
README
An OpenAPI documentation validator using your existing Laravel tests.
TL;DR
composer require --dev webparking/laravel-openapi-validator
<?php namespace Tests; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Webparking\OpenAPIValidator\ValidatesHttpMessagesAgainstDocs; abstract class TestCase extends BaseTestCase { use CreatesApplication; use ValidatesHttpMessagesAgainstDocs; }
Laravel Version Compatibility
The Laravel versions listed below are all currently supported:
- Laravel >= 11.x.x on PHP >= 8.3 is supported starting from 3.0.0
- Laravel >= 10.x.x on PHP >= 8.2 is supported starting from 2.2.0
- Laravel >= 9.x.x on PHP >= 8.1 is supported starting from 2.1.0
- Laravel >= 8.2.x on PHP >= 8.0 is supported starting from 2.0.0
The problem
When creating and maintaining an HTTP REST API, you want to make sure it works well and is well documented. We consider it essential to be able to check both automatically.
What we tried before
- We made a system that would generate tests from the API documentation. For the majority of tests, it worked. But having a simple way to include edge cases in your tests is next to impossible. For example: there's an endpoint that returns a 500 status code when called with certain input. You've fixed the bug and want to add a test to make sure it actually is and stays fixed. To do that, you'd have to include an error-case in your docs. You don't want to live in a hacky world like that.
- We made a system that would generate documentation based on the application's code. First of all, it would be very difficult to explain nuances and abstract concepts in your documentation, as they don't have a formal place in the application. We'd have to split the documentation in two: the technical spec generated from code and the complimentary explanations in a wiki of some kind. And yes, they'd get out of sync over time. Second, your application code needs to be very very structured in order to accomplish this. Maintaining quality by consistency is a big thing for us, but in practice there are always one or two edge cases where it's actually better to deviate.
Our solution
We combined Laravel HTTP testing with ThePHPLeague's OpenAPI PSR7 validator by overriding MakesHttpRequests#call. Before executing the request, we have it validated against the OpenAPI documentation. And before returning the response, we have that validated too. Exceptions are thrown for any problems found.
This solution has enabled us to use both OpenAPI and HTTP tests to the fullest; no compromises needed.
Usage
Simply use the ValidatesHttpMessagesAgainstDocs in your test class. We generally do this on the project level TestCase.
The default index file for the docs is docs/index.yaml, but you're free to override that by setting the yamlPath property.
You may want to exclude certain URI's from validation. Set the ignoredUris property to do this.
webparking/laravel-openapi-validator 适用场景与选型建议
webparking/laravel-openapi-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.81k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「documentation」 「http」 「rest」 「api」 「validator」 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webparking/laravel-openapi-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webparking/laravel-openapi-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webparking/laravel-openapi-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
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
Api bundle
Laravel API documentation generating tool
统计信息
- 总下载量: 19.81k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-01