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.
- PHP ^7.3
Important notice! While PHP 8.0 declared in composer.json this package hasn't been tested against it.
Run in terminal:
composer require ybelenko/openapi-data-mockerImagine we have OpenAPI Specification 3.0.3 - Schema Object like this:
description: Real world example schematype: objectproperties:
id:
type: integerformat: int32minimum: 1purchased_items:
type: arrayitems:
type: objectproperties:
SKU:
type: stringformat: uuidmaxLength: 20quantity:
type: integerformat: int32minimum: 1maximum: 5price:
type: objectproperties:
currency:
type: stringminLength: 3maxLength: 3enum:
- USD
- EUR
- RUBvalue:
type: numberformat: floatminimum: 0.01maximum: 99.99manufacturer:
type: objectproperties:
name:
type: stringmaxLength: 30country:
type: stringenum:
- CHN
- USA
- RUSbuyer:
type: objectproperties:
first_name:
type: stringminLength: 3maxLength: 15last_name:
type: stringminLength: 3maxLength: 15credit_card:
type: integerminimum: 1000000000000000maximum: 10000000000000000phone:
type: integerminimum: 10000000000000maximum: 99999999999999email:
type: stringformat: emailstatus:
type: stringenum:
- registered
- paid
- shipped
- delivereddefault: registeredcreated_at:
type: stringformat: date-timeNotice! 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';
useOpenAPIServer\Mock\OpenApiDataMockerasMocker;
$mocker = newMocker();
// 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);
echojson_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.
All data types supported except specific string formats: email, uuid, password which are poorly implemented.
| 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 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 |
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: objectproperties:
model_b:
$ref: '#/definitions/ModelB'ModelB:
type: arrayitems:
$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 exceptiondefinitions:
OuterComposite:
type: objectproperties:
my_number:
$ref: '#/definitions/OuterNumber'my_string:
$ref: '#/definitions/OuterString'my_boolean:
$ref: '#/definitions/OuterBoolean'OuterNumber:
type: numberOuterString:
type: stringOuterBoolean:
type: boolean