-
-
Notifications
You must be signed in to change notification settings - Fork 585
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
fix discriminator for classes in the middle of hierarchy #648
Conversation
Something similar should be already on master. What is this PR for? |
In my application I have class hierarchy like this: class A {}
abstract class B extends A {}
class BA extends B {}
class BB extends B {} When I serialize an instance of class BA or BB there is a column 'type' in the json, describing which sub class is serialized. |
Isn't related to #382 ? |
Today I updated serializer to tag 1.3.1 but the errors still occurs. |
Should I try dev-master? |
Yes please, was merged Friday |
Ok, tested with 5ebae63 (dev-master), but problem still exists. Can you please consider a merge? |
Can you provide tests? |
No test, no merge, right? |
Yep |
But for me is sospicous that master dev does not work. .. |
Ok, I'll double check my setup |
You can check the PR, I think is exactly the feature you need |
(Remember to clear caches) |
I'm with you, I check everything again |
I saw, that I requested the same here: #309 :) And found that my deserialization call is like the following: $jms_serializer->deserialize($json_content,'WebCollection','json','AbstractMiddleClassName'); So the serializer has to find the right sub class from the 'type'' column in the json content. |
Ah, I see. Your pr is for the deserialization case. Unfortunately without tests can't be merged |
aight, will push the test here |
Update: made a fresh clone of masteer and composer install, then run phpunit and got lots of errors, especially for my wanted feature (testNestedPolymorphicInterfaces). Win10 / PHP 5.6.25 nts Is there something I should investigate? |
what it means? can you share? |
here is my console output: MINGW64 /c/Program Files (x86)/wamp/www/serializer (master)
$ phpunit.phar
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.
WWWWWWWW............................WWWWWWWWWWW............... 62 / 399 ( 15%)
...................................F..E..........WW......EEEWW 124 / 399 ( 31%)
WWWWWWWWWWW....WWWWWWWW....................................... 186 / 399 ( 46%)
..........WW............................................FF...F 248 / 399 ( 62%)
FFFFFF.SFFFFFFFFFFFFFFFFFFFFFFFSFFFFFFFFFFFFFFFFFFFFFFFFWFFFFF 310 / 399 ( 77%)
FFFF.FF.FFFFFF.F.FSSSSSFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 372 / 399 ( 93%)
FFFFFFFFFFFF.FF.FFFFFF.F.FW 399 / 399 (100%)
|
no idea... travis says master it is green https://travis-ci.org/schmittjoh/serializer/builds/159323741 |
Should I attach the whole output log? |
ok |
there are a bunch of errors in your environment... as example:
date time not configured well
sqlite missing? im not use your PHP is well configured |
right, have fixed these errors already. the others could be line ending issues. |
windows :( |
So, after correcting the line endings, tests running well. And thank you for pointing to coding a test, because my problem becomes clearly now: My input is a collection of classes. class Vehicle {
private $km;
}
class Car extends Vehicle {}
class Moped extends Vehicle {}
$json = '[{"km":3,"type":"car"},{"km":1,"type":"moped"}]';
$context = new \Own\Serializer\Context\DeserializationContext();
$context->setInnerObject('\Vehicle');
$ret = $this->jms_serializer->deserialize($json,'array','json',$context); Questions: |
here my use case classes, i try if 'array' will work class DeserializationContext extends \JMS\Serializer\DeserializationContext
{
/**
* @var string
*/
protected $inner_object;
/**
* @param string $inner_object
*/
public function setInnerObject ($inner_object) {
$this->inner_object = $inner_object;
}
/**
* @return string
*/
public function getInnerObject () {
return $this->inner_object;
}
}
namespace Prima\REST\ClientBundle\Serializer\Handler;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\VisitorInterface;
use Prima\REST\ClientBundle\Serializer\Context\DeserializationContext as Context;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\ClassMetadata;
class WebCollectionHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods()
{
return array(
array(
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => 'WebCollection',
'method' => 'deserializeWebCollection',
),
);
}
public function deserializeWebCollection(VisitorInterface $visitor, $data, array $type, Context $context)
{
$ret = new ArrayCollection();
$type['name'] = $context->getInnerObject();
$key = key($data);
if (is_numeric($key)) {
foreach ($data as $object) {
$ret->add(
$visitor->getNavigator()->accept($object, $type, $context)
);
}
}
else {
$ret->add(
$visitor->getNavigator()->accept($data, $type, $context)
);
}
$refl = new \ReflectionObject($visitor);
$property = $refl->getParentClass()->getProperty('result');
$property->setAccessible(TRUE);
$property->setValue($visitor, $ret);
return $ret;
}
} |
doing this you are moving the "deserialization" logic into something else... Do you use |
That is my doctrine config file for the classes: Prima\Partner\AccountBundle\Entity\User:
type: entity
table: partner_account
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: type
type: string
discriminatorMap:
partner: UserChief
sub: UserSub
|
looking at doctrine documentation.
This means that for hydration, doctrine does some magic ("lowercase short name"), that you have to do on your own later... Doing it, also JMS will work better. |
My input has a type column, because of correct serialization with |
But I use a descriminator column :) |
|
missed
this means that you can copy & paste
into
|
Ok, expect another abstract class 'FOS\UserBundle\Model\User' as parent of abstract 'Prima\Partner\AccountBundle\Entity\User' . Then deserialization is definitely broken. |
Pushed test case, would you please review? |
it looks you have used tabs instead of spaces |
You're right, I run cs fixer: $ php-cs-fixer.phar fix ./ --fixers=linefeed,short_tag,indentation
1) src\JMS\Serializer\Metadata\ClassMetadata.php
2) tests\JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Base.php
3) tests\JMS\Serializer\Tests\Serializer\BaseSerializationTest.php
Fixed all files in 88.984 seconds, 7.750 MB memory used |
Can I do something else to get merged? |
+1. The error was not fixed in #382 |
@goetas Is something left unclear, why this patch is useful? |
@goetas we really need this fix. I'm tired to live in forks =) |
UP |
=( |
@goetas some news? |
Replaced by #659 (the test classes were already in the repo, no need to add 120 lines of test classes) |
thank you! |
No description provided.