Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit a400e0c

Browse files
nofirgmartin-georgiev
authored andcommitted
Fix object validation error when input is not an array (#158)
1 parent 0e431da commit a400e0c

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/Types/ObjectType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static function createFromArray($name, array $data = [])
111111
*/
112112
public function discriminate($value)
113113
{
114-
if (isset($value[$this->getDiscriminator()])) {
114+
if (\is_array($value) && isset($value[$this->getDiscriminator()])) {
115115
if ($this->getDiscriminatorValue() !== null) {
116116
return $this->getDiscriminatorValue() === $value[$this->getDiscriminator()];
117117
}

tests/Types/ObjectTypeTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Raml\Tests\Types;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Raml\ApiDefinition;
7+
use Raml\Parser;
8+
use Raml\Types\TypeValidationError;
9+
10+
class ObjectTypeTest extends TestCase
11+
{
12+
/**
13+
* @var ApiDefinition
14+
*/
15+
private $apiDefinition;
16+
17+
protected function setUp()
18+
{
19+
parent::setUp();
20+
$this->apiDefinition = (new Parser())->parse(__DIR__ . '/../fixture/object_types.raml');
21+
}
22+
23+
/**
24+
* @test
25+
*/
26+
public function shouldCorrectlyValidateCorrectType()
27+
{
28+
$resource = $this->apiDefinition->getResourceByUri('/actors/1');
29+
$method = $resource->getMethod('get');
30+
$response = $method->getResponse(200);
31+
$body = $response->getBodyByType('application/json');
32+
$type = $body->getType();
33+
34+
$type->validate([
35+
'fistName' => 'Jackie',
36+
'lastName' => 'Сhan',
37+
]);
38+
39+
$this->assertTrue($type->isValid());
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function shouldNotCorrectlyValidateObjectType()
46+
{
47+
$resource = $this->apiDefinition->getResourceByUri('/actors/1');
48+
$method = $resource->getMethod('get');
49+
$response = $method->getResponse(200);
50+
$body = $response->getBodyByType('application/json');
51+
$type = $body->getType();
52+
53+
$type->validate('Jackie Сhan');
54+
55+
$this->assertFalse($type->isValid());
56+
$this->assertCount(1, $type->getErrors());
57+
$this->assertInstanceOf(TypeValidationError::class, $type->getErrors()[0]);
58+
}
59+
}

tests/fixture/object_types.raml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#%RAML 1.0
2+
3+
title: List of actors API
4+
baseUri: http://example.api.com/{version}
5+
version: v1
6+
mediaType: application/json
7+
8+
/actors/{id}:
9+
get:
10+
responses:
11+
200:
12+
body:
13+
application/json:
14+
type: Actor
15+
types:
16+
Actor:
17+
properties:
18+
fistName:
19+
type: string
20+
required: true
21+
lastName:
22+
type: string
23+
required: true

0 commit comments

Comments
 (0)