Skip to content

Commit f2ff761

Browse files
authored
Merge pull request #37 from Art4/bugfix_optional_id
Bugfix with optional Resource ID
2 parents e41c7f8 + 1ead0e7 commit f2ff761

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/Document.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,12 @@ protected function parseData($data)
205205
throw new ValidationException('Data value has to be null or an object, "' . gettype($data) . '" given.');
206206
}
207207

208-
$object_vars = get_object_vars($data);
208+
$object_keys = array_keys(get_object_vars($data));
209+
sort($object_keys);
209210

210211
// the properties must be type and id or
211212
// the 3 properties must be type, id and meta
212-
if ( count($object_vars) === 2 or (count($object_vars) === 3 and property_exists($data, 'meta')) )
213+
if ( $object_keys === ['id', 'type'] or $object_keys === ['id', 'meta', 'type'] )
213214
{
214215
$resource = $this->manager->getFactory()->make(
215216
'ResourceIdentifier',

tests/Integration/ParsingTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,19 @@ public function testParseCreateResourceWithoutId()
612612
// Test full array
613613
$this->assertEquals(json_decode($string, true), $document->asArray(true));
614614
}
615+
616+
/**
617+
* @test
618+
*/
619+
public function testParseCreateShortResourceWithoutId()
620+
{
621+
$string = $this->getJsonString('15_create_resource_without_id.json');
622+
$document = Helper::parseRequestBody($string);
623+
624+
$this->assertInstanceOf('Art4\JsonApiClient\Document', $document);
625+
$this->assertSame(['data'], $document->getKeys());
626+
627+
// Test full array
628+
$this->assertEquals(json_decode($string, true), $document->asArray(true));
629+
}
615630
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"data": {
3+
"type": "photos",
4+
"attributes": {
5+
"title": "Ember Hamster",
6+
"src": "http://example.com/images/productivity.png"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)