Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: data.meta ignored in relationships #89

Merged
merged 4 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public function hasAttributes(): bool
return !empty($this->toArray());
}

/**
* @return Meta
*/
public function getDataMeta(): Meta
BurningDog marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->meta;
}

/**
* @return array
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Parsers/ItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ function ($identifier) {
throw new ValidationException(sprintf('ResourceIdentifier property "id" MUST be a string, "%s" given.', gettype($data->id)));
}

return $this->getItemInstance($data->type)->setId($data->id);
$item = $this->getItemInstance($data->type)->setId($data->id);
if (property_exists($data, 'meta')) {
$item->setMeta($this->metaParser->parse($data->meta));
}

return $item;
}
}
43 changes: 43 additions & 0 deletions tests/Parsers/ItemParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,25 @@ public function itParsesMeta()
static::assertEquals(new Meta(['foo' => 'bar']), $item->getMeta());
}

/**
* @test
*/
public function itParsesMetaInData()
{
$typeMapper = new TypeMapper();
$typeMapper->setMapping('child', ChildItem::class);
$typeMapper->setMapping('master', MasterItem::class);
$parser = $this->getItemParser($typeMapper);

$item = $parser->parse($this->getJsonApiItemMock('master', '1'));

$dataMeta = $item->getRelation('childwithdatameta')->getIncluded()->getDataMeta();
static::assertInstanceOf(Meta::class, $dataMeta);

$image = $dataMeta->imageDerivatives->links->header->href;
$this->assertEquals('https://example.com/image/header/about-us.jpeg', $image);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so we can check we're getting specific information back, and not accidentally returning the other Meta object.

}

/**
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper
*
Expand Down Expand Up @@ -766,6 +785,30 @@ private function getJsonApiItemMock($type, $id)
'foo' => 'bar',
],
],
'childwithdatameta' => [
'data' => [
'type' => 'child',
'id' => '9',
'meta' => [
'alt' => '',
'width' => 1920,
'height' => 1280,
'imageDerivatives' => [
'links' => [
'header' => [
'href' => 'https://example.com/image/header/about-us.jpeg',
'meta' => [
'rel' => 'drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative',
],
],
],
],
],
],
'links' => [
'self' => 'http://example.com/'.$type.'/'.$id.'/relationships/childwithdatameta',
],
],
'empty' => [
'data' => null,
],
Expand Down