From c9e22c64c996cda05ea3ff12dd0676f1ae69a13d Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Thu, 16 Jan 2014 16:49:32 +0000 Subject: [PATCH] allow jms serializer to be configured by Guzzle's Parameter Data property --- Resources/doc/serialization.md | 25 ++++++++ .../Request/JMSSerializerBodyVisitor.php | 18 +++++- .../Request/JMSSerializerBodyVisitorTest.php | 59 +++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 Tests/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitorTest.php diff --git a/Resources/doc/serialization.md b/Resources/doc/serialization.md index 3ba833e..acd8efd 100644 --- a/Resources/doc/serialization.md +++ b/Resources/doc/serialization.md @@ -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 ----------------- diff --git a/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitor.php b/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitor.php index f60ee54..125a24b 100644 --- a/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitor.php +++ b/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitor.php @@ -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; /** @@ -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); diff --git a/Tests/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitorTest.php b/Tests/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitorTest.php new file mode 100644 index 0000000..725fae3 --- /dev/null +++ b/Tests/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitorTest.php @@ -0,0 +1,59 @@ +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'); + } +}