Skip to content

Fix multiple handler callbacks in YamlDriver #515

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

Merged
merged 2 commits into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/JMS/Serializer/Metadata/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
}

if (isset($config['handler_callbacks'])) {
foreach ($config['handler_callbacks'] as $direction => $formats) {
foreach ($config['handler_callbacks'] as $directionName => $formats) {
$direction = GraphNavigator::parseDirection($directionName);
foreach ($formats as $format => $methodName) {
$direction = GraphNavigator::parseDirection($direction);
$metadata->addHandlerCallback($direction, $format, $methodName);
}
}
Expand Down
46 changes: 46 additions & 0 deletions tests/JMS/Serializer/Tests/Fixtures/ObjectWithHandlerCallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* Copyright 2013 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation\HandlerCallback;
use JMS\Serializer\Annotation\Type;

class ObjectWithHandlerCallbacks
{
/**
* @Type("string")
*/
public $name;

/**
* @HandlerCallback(direction="serialization", format="json")
*/
public function toJson()
{
return $this->name;
}

/**
* @HandlerCallback(direction="serialization", format="xml")
*/
public function toXml()
{
return $this->name;
}
}
8 changes: 8 additions & 0 deletions tests/JMS/Serializer/Tests/Metadata/Driver/BaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace JMS\Serializer\Tests\Metadata\Driver;

use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
Expand Down Expand Up @@ -321,6 +322,13 @@ public function testXmlNamespaceInheritanceMetadata()
$this->assertEquals($p, $m->propertyMetadata['qux']);
}

public function testHandlerCallbacks()
{
$m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks'));

$this->assertEquals('toJson', $m->handlerCallbacks[GraphNavigator::DIRECTION_SERIALIZATION]['json']);
$this->assertEquals('toXml', $m->handlerCallbacks[GraphNavigator::DIRECTION_SERIALIZATION]['xml']);
}

/**
* @return DriverInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;

$metadata = new ClassMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks');

$pMetadata = new PropertyMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks', 'name');
$pMetadata->type = 'string';
$metadata->addPropertyMetadata($pMetadata);

$metadata->addHandlerCallback(GraphNavigator::DIRECTION_SERIALIZATION, 'json', 'toJson');
$metadata->addHandlerCallback(GraphNavigator::DIRECTION_SERIALIZATION, 'xml', 'toXml');

return $metadata;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<class name="JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks">
<property name="name" type="string"/>
<callback-method name="toJson" type="handler" direction="serialization" format="json" />
<callback-method name="toXml" type="handler" direction="serialization" format="xml" />
</class>
</serializer>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks:
properties:
name:
type: string
handler_callbacks:
serialization:
xml: toXml
json: toJson