Skip to content

Commit

Permalink
allow jms serializer to be configured by Guzzle's Parameter Data prop…
Browse files Browse the repository at this point in the history
…erty
  • Loading branch information
bendavies committed Jan 20, 2014
1 parent da2d1f4 commit c9e22c6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Resources/doc/serialization.md
Expand Up @@ -103,6 +103,31 @@ Executing the `CreatePerson` command will now send an instance of `Vendor\MyBund
$command = $client->getCommand('CreatePerson', array('person' => $person));
$client->execute($command);

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

For example:

"CreatePerson":{
"httpMethod":"POST",
"uri":"person",
"summary":"Create a person",
"parameters":{
"person":{
"location":"body",
"type":"object",
"instanceOf":"Vendor\\MyBundle\\Entity\\Person",
"sentAs":"json",
"required":"true",
"data": {
"jms_serializer.groups": "person-details",
"jms_serializer.version": 1,
"jms_serializer.serialize_nulls": true,
"jms_serializer.max_depth_checks": true,
}
}
}
}

Concrete commands
-----------------

Expand Down
Expand Up @@ -15,6 +15,7 @@
use Guzzle\Service\Command\CommandInterface;
use Guzzle\Service\Command\LocationVisitor\Request\BodyVisitor;
use Guzzle\Service\Description\Parameter;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;

/**
Expand Down Expand Up @@ -64,7 +65,22 @@ public function visit(CommandInterface $command, RequestInterface $request, Para
$contentType = 'xml';
break;
}
$value = $this->serializer->serialize($filteredValue, $contentType);
$context = SerializationContext::create();

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

$value = $this->serializer->serialize($filteredValue, $contentType, $context);
}

parent::visit($command, $request, $param, $value);
Expand Down
@@ -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 JMS\Serializer\SerializationContext;
use Misd\GuzzleBundle\Service\Command\LocationVisitor\Request\JMSSerializerBodyVisitor;

class JMSSerializerBodyVisitorTest extends \PHPUnit_Framework_TestCase
{
public function testSerializeContextConfiguration()
{
$expectedContext = SerializationContext::create();
$expectedContext->setGroups('group');
$expectedContext->setVersion(1);
$expectedContext->setSerializeNull(true);
$expectedContext->enableMaxDepthChecks();

$parameter = $this->getMock('Guzzle\Service\Description\Parameter');
$parameter->expects($this->once())->method('getSentAs')->will($this->returnValue('json'));
$parameter->expects($this->any())->method('filter')->will($this->returnValue(array()));

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

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

$command = $this->getMock('Guzzle\Service\Command\CommandInterface');
$request = $this->getMockBuilder('Guzzle\Http\Message\EntityEnclosingRequest')
->disableOriginalConstructor()
->getMock();

$serializer = $this->getMock('JMS\Serializer\SerializerInterface');
$serializer->expects($this->once())->method('serialize')
->with(array(), 'json', $this->equalTo($expectedContext))
->will($this->returnValue('serialized'));

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

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

return $ref->invoke($parser, $command, $request, $parameter, 'value');
}
}

0 comments on commit c9e22c6

Please sign in to comment.