diff --git a/src/Types/ObjectType.php b/src/Types/ObjectType.php index 0b5703eb..1a1cd966 100644 --- a/src/Types/ObjectType.php +++ b/src/Types/ObjectType.php @@ -111,7 +111,7 @@ public static function createFromArray($name, array $data = []) */ public function discriminate($value) { - if (isset($value[$this->getDiscriminator()])) { + if (\is_array($value) && isset($value[$this->getDiscriminator()])) { if ($this->getDiscriminatorValue() !== null) { return $this->getDiscriminatorValue() === $value[$this->getDiscriminator()]; } diff --git a/tests/Types/ObjectTypeTest.php b/tests/Types/ObjectTypeTest.php new file mode 100644 index 00000000..64e51b3b --- /dev/null +++ b/tests/Types/ObjectTypeTest.php @@ -0,0 +1,59 @@ +apiDefinition = (new Parser())->parse(__DIR__ . '/../fixture/object_types.raml'); + } + + /** + * @test + */ + public function shouldCorrectlyValidateCorrectType() + { + $resource = $this->apiDefinition->getResourceByUri('/actors/1'); + $method = $resource->getMethod('get'); + $response = $method->getResponse(200); + $body = $response->getBodyByType('application/json'); + $type = $body->getType(); + + $type->validate([ + 'fistName' => 'Jackie', + 'lastName' => 'Сhan', + ]); + + $this->assertTrue($type->isValid()); + } + + /** + * @test + */ + public function shouldNotCorrectlyValidateObjectType() + { + $resource = $this->apiDefinition->getResourceByUri('/actors/1'); + $method = $resource->getMethod('get'); + $response = $method->getResponse(200); + $body = $response->getBodyByType('application/json'); + $type = $body->getType(); + + $type->validate('Jackie Сhan'); + + $this->assertFalse($type->isValid()); + $this->assertCount(1, $type->getErrors()); + $this->assertInstanceOf(TypeValidationError::class, $type->getErrors()[0]); + } +} diff --git a/tests/fixture/object_types.raml b/tests/fixture/object_types.raml new file mode 100644 index 00000000..9d318b7f --- /dev/null +++ b/tests/fixture/object_types.raml @@ -0,0 +1,23 @@ +#%RAML 1.0 + +title: List of actors API +baseUri: http://example.api.com/{version} +version: v1 +mediaType: application/json + +/actors/{id}: + get: + responses: + 200: + body: + application/json: + type: Actor +types: + Actor: + properties: + fistName: + type: string + required: true + lastName: + type: string + required: true