Skip to content

Commit

Permalink
allow jms deserializer to be configured by Guzzle's Operation Data pr…
Browse files Browse the repository at this point in the history
…operty
  • Loading branch information
bendavies committed Jan 20, 2014
1 parent 9451d52 commit da2d1f4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
24 changes: 24 additions & 0 deletions Resources/doc/serialization.md
Expand Up @@ -32,6 +32,30 @@ Executing the `GetPerson` command will now return an instance of `Vendor\MyBundl
$command = $client->getCommand('GetPerson', array('id' => $id));
$person = $client->execute($command);

If you wish to customize the deserialization context of the serializer, you can do so by usage of the `data` property of the Operation. Groups, version, and max depth checks are configurable for deserialization:

For example:

"GetPerson":{
"httpMethod":"GET",
"uri":"person/{id}",
"summary":"Gets a person",
"responseClass":"Vendor\\MyBundle\\Entity\\Person",
"data": {
"jms_serializer.groups": "person-details",
"jms_serializer.version": 1,
"jms_serializer.max_depth_checks": true,
}
"parameters":{
"id":{
"location":"uri",
"type":"integer",
"description":"Person to retrieve by ID",
"required":"true"
}
}
}

### Arrays

The `responseClass` value can actually be any of the JMS Serializer's `@Type` annotation value forms which contain a classname.
Expand Down
19 changes: 17 additions & 2 deletions Service/Command/JMSSerializerResponseParser.php
Expand Up @@ -12,9 +12,10 @@
namespace Misd\GuzzleBundle\Service\Command;

use Guzzle\Http\Message\Response;
use Guzzle\Service\Command\CommandInterface;
use Guzzle\Service\Command\ResponseParserInterface;
use Guzzle\Service\Description\OperationInterface;
use Guzzle\Service\Command\CommandInterface;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\SerializerInterface;

/**
Expand Down Expand Up @@ -104,10 +105,24 @@ protected function deserialize(CommandInterface $command, Response $response, $c
if (null !== $serializerContentType &&
OperationInterface::TYPE_CLASS === $command->getOperation()->getResponseType()
) {
$context = DeserializationContext::create();
$operation = $command->getOperation();

if (null !== $groups = $operation->getData('jms_serializer.groups')) {
$context->setGroups($groups);
}
if (null !== $version = $operation->getData('jms_serializer.version')) {
$context->setVersion($version);
}
if (true === $operation->getData('jms_serializer.max_depth_checks')) {
$context->enableMaxDepthChecks();
}

return $this->serializer->deserialize(
$response->getBody(),
$command->getOperation()->getResponseClass(),
$serializerContentType
$serializerContentType,
$context
);
}
}
Expand Down
59 changes: 59 additions & 0 deletions Tests/Service/Command/JMSSerializerResponseParserTest.php
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the MisdGuzzleBundle for Symfony2.
*
* (c) University of Cambridge
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Misd\GuzzleBundle\Tests\Service\Command;

use Guzzle\Http\Message\Response;
use Guzzle\Service\Description\OperationInterface;
use JMS\Serializer\DeserializationContext;
use Misd\GuzzleBundle\Service\Command\JMSSerializerResponseParser;

class JMSSerializerResponseParserTest extends \PHPUnit_Framework_TestCase
{
public function testDeserializeContextConfiguration()
{
$expectedContext = DeserializationContext::create();
$expectedContext->setGroups('group');
$expectedContext->setVersion(1);
$expectedContext->enableMaxDepthChecks();

$operation = $this->getMock('Guzzle\Service\Description\OperationInterface');
$operation->expects($this->any())->method('getResponseType')->will($this->returnValue(OperationInterface::TYPE_CLASS));
$operation->expects($this->any())->method('getResponseClass')->will($this->returnValue('ResponseClass'));

$dataMap = array(
array('jms_serializer.groups', 'group'),
array('jms_serializer.version', 1),
array('jms_serializer.max_depth_checks', true)
);

$operation->expects($this->any())
->method('getData')
->will($this->returnValueMap($dataMap));

$command = $this->getMock('Guzzle\Service\Command\CommandInterface');
$command->expects($this->any())->method('getOperation')->will($this->returnValue($operation));

$response = new Response(200);;
$response->setBody('body');

$serializer = $this->getMock('JMS\Serializer\SerializerInterface');
$serializer->expects($this->once())->method('deserialize')
->with('body', 'ResponseClass', 'json', $this->equalTo($expectedContext));

$parser = new JMSSerializerResponseParser($serializer, $this->getMock('Guzzle\Service\Command\ResponseParserInterface'));

$ref = new \ReflectionMethod($parser, 'deserialize');
$ref->setAccessible(true);

return $ref->invoke($parser, $command, $response, 'json');
}
}

0 comments on commit da2d1f4

Please sign in to comment.