ybelenko/openapi-data-mocker
Composer 安装命令:
composer require ybelenko/openapi-data-mocker
包简介
Library that generates fake data from Swagger 2.0|Openapi 3.0 spec
README 文档
README
Openapi Data Mocker helps to generate fake data from OpenAPI 3.0 documents. Most of the methods may work with 2.0 version(fka Swagger 2.0), but it's not tested. This package was an enhancement of PHP Slim4 server in OpenAPI Generator project, but it easier to maintain it in separated repo.
Requirements
- PHP ^7.3
Important notice! While PHP 8.0 declared in composer.json this package hasn't been tested against it.
Installation via Composer
Run in terminal:
composer require ybelenko/openapi-data-mocker
Usage example
Imagine we have OpenAPI Specification 3.0.3 - Schema Object like this:
description: Real world example schema type: object properties: id: type: integer format: int32 minimum: 1 purchased_items: type: array items: type: object properties: SKU: type: string format: uuid maxLength: 20 quantity: type: integer format: int32 minimum: 1 maximum: 5 price: type: object properties: currency: type: string minLength: 3 maxLength: 3 enum: - USD - EUR - RUB value: type: number format: float minimum: 0.01 maximum: 99.99 manufacturer: type: object properties: name: type: string maxLength: 30 country: type: string enum: - CHN - USA - RUS buyer: type: object properties: first_name: type: string minLength: 3 maxLength: 15 last_name: type: string minLength: 3 maxLength: 15 credit_card: type: integer minimum: 1000000000000000 maximum: 10000000000000000 phone: type: integer minimum: 10000000000000 maximum: 99999999999999 email: type: string format: email status: type: string enum: - registered - paid - shipped - delivered default: registered created_at: type: string format: date-time
Notice! While schema object presented in YAML format this library doesn't support YAML or JSON parsing right now. It means that
mockSchemaObjectmethod expects already decoded JSON value as argument.
When we mock mentioned schema with mockSchemaObject method:
require __DIR__ . '/vendor/autoload.php'; use OpenAPIServer\Mock\OpenApiDataMocker as Mocker; $mocker = new Mocker(); // set model classes namespace for $ref handling // current example doesn't use $refs in schemas, however $mocker->setModelsNamespace('JohnDoesPackage\\Model\\'); // class InvoiceTest contains schema mentioned previously // it returns that schema with getOpenApiSchema() method declared in OpenAPIServer\Mock\BaseModel parent class $schema = \OpenAPIServer\Mock\Model\InvoiceTest::getOpenApiSchema(); $data = $mocker->mockSchemaObject($schema); echo json_encode($data, \JSON_PRETTY_PRINT);
the output looks like:
{
"id": 1912777939,
"purchased_items": [
{
"SKU": "5ee78cfde9f05",
"quantity": 4,
"price": {
"currency": "EUR",
"value": 57.635
},
"manufacturer": {
"name": "Lorem i",
"country": "USA"
}
}
],
"buyer": {
"first_name": "Lorem ipsum do",
"last_name": "Lorem ipsum ",
"credit_card": 2455087473915908,
"phone": 65526260517693,
"email": "jfkennedy@example.com"
},
"status": "delivered",
"created_at": "1978-08-08T04:03:09+00:00"
}
Of course that output will be slightly different on every call. That's what mocker package has been developed for.
You can check extended example at examples/extended_example.php.
Supported features
All data types supported except specific string formats: email, uuid, password which are poorly implemented.
Data Types Support
| Data Type | Data Format | Supported |
|---|---|---|
integer |
int32 |
✅ |
integer |
int64 |
✅ |
number |
float |
✅ |
number |
double |
|
string |
byte |
✅ |
string |
binary |
✅ |
boolean |
✅ | |
string |
date |
✅ |
string |
date-time |
✅ |
string |
password |
✅ |
string |
email |
✅ |
string |
uuid |
✅ |
Data Options Support
| Data Type | Option | Supported |
|---|---|---|
string |
minLength |
✅ |
string |
maxLength |
✅ |
string |
enum |
✅ |
string |
pattern |
|
integer |
minimum |
✅ |
integer |
maximum |
✅ |
integer |
exclusiveMinimum |
✅ |
integer |
exclusiveMaximum |
✅ |
number |
minimum |
✅ |
number |
maximum |
✅ |
number |
exclusiveMinimum |
✅ |
number |
exclusiveMaximum |
✅ |
array |
items |
✅ |
array |
additionalItems |
|
array |
minItems |
✅ |
array |
maxItems |
✅ |
array |
uniqueItems |
|
object |
properties |
✅ |
object |
maxProperties |
|
object |
minProperties |
|
object |
patternProperties |
|
object |
additionalProperties |
|
object |
required |
|
* |
$ref |
✅ |
* |
allOf |
|
* |
anyOf |
|
* |
oneOf |
|
* |
not |
Known Limitations
Avoid circular refs in your schema. Schema below can cause infinite loop and Out of Memory PHP error:
# ModelA has reference to ModelB while ModelB has reference to ModelA. # Mock server will produce huge nested JSON example and ended with `Out of Memory` error. definitions: ModelA: type: object properties: model_b: $ref: '#/definitions/ModelB' ModelB: type: array items: $ref: '#/definitions/ModelA'
Don't ref scalar types, because generator will not produce models which mock server can find. So schema below will cause error:
# generated build contains only `OuterComposite` model class which referenced to not existed `OuterNumber`, `OuterString`, `OuterBoolean` classes # mock server cannot mock `OuterComposite` model and throws exception definitions: OuterComposite: type: object properties: my_number: $ref: '#/definitions/OuterNumber' my_string: $ref: '#/definitions/OuterString' my_boolean: $ref: '#/definitions/OuterBoolean' OuterNumber: type: number OuterString: type: string OuterBoolean: type: boolean
Links to mentioned technologies
ybelenko/openapi-data-mocker 适用场景与选型建议
ybelenko/openapi-data-mocker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.42k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2020 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「faker」 「data」 「mock」 「fake」 「swagger」 「openapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ybelenko/openapi-data-mocker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ybelenko/openapi-data-mocker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ybelenko/openapi-data-mocker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
Symfony bundle to manage fixtures with Alice and Faker.
Additional plugin for fakerphp/faker that allows you to generate a random animal
统计信息
- 总下载量: 35.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 29
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-08