Skip to content

Commit d8d787a

Browse files
committed
Don't throw exception when type keyword is missing
1 parent ed8519b commit d8d787a

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/Mock/BaseModel.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public static function createFromData($data): OpenApiModelInterface
117117
public function setData($data): void
118118
{
119119
$schema = (array) static::getOpenApiSchema();
120-
switch ($schema['type']) {
120+
$modelType = (array_key_exists('type', $schema)) ? $schema['type'] : null;
121+
switch ($modelType) {
121122
case IMocker::DATA_TYPE_ARRAY:
122123
// data for array OAS type should be straight indexed array
123124
if (is_array($data)) {
@@ -163,7 +164,8 @@ public function getData()
163164
{
164165
$data = null;
165166
$schema = (array) static::getOpenApiSchema();
166-
switch ($schema['type']) {
167+
$modelType = (array_key_exists('type', $schema)) ? $schema['type'] : null;
168+
switch ($modelType) {
167169
case IMocker::DATA_TYPE_OBJECT:
168170
// need to convert data container to object
169171
$data = new StdClass();
@@ -289,7 +291,7 @@ public function jsonSerialize()
289291
protected function validateModelType(?string $type = null, bool $throwException = true): bool
290292
{
291293
$isValid = in_array($type, static::VALID_OAS_DATA_TYPES);
292-
if ($isValid === false && $throwException) {
294+
if ($type !== null && $isValid === false && $throwException) {
293295
throw new InvalidArgumentException(
294296
sprintf(
295297
'Invalid OAS schema of %s model, "type" must be one of %s',

test/Mock/BaseModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function provideClassesAndDefaultData()
5757
'string model' => [BasicStringTestClass::class, json_encode(null)],
5858
'array model' => [BasicArrayTestClass::class, json_encode([])],
5959
'object model' => [BasicObjectTestClass::class, json_encode(new StdClass())],
60+
'missing type model' => [MissingTypeTestClass::class, json_encode(null)],
6061
];
6162
}
6263

@@ -75,7 +76,6 @@ public function provideInvalidClasses()
7576
{
7677
return [
7778
'unknown type model' => [UnknownTypeTestClass::class],
78-
'missing type model' => [MissingTypeTestClass::class],
7979
];
8080
}
8181

0 commit comments

Comments
 (0)