Comments (6)
Hi, Thank you for your reporting.
IMO this issue is caused if the entity is a doctrine proxy.
So I've added some patch.
Could you try again using new release v0.4.1?
from hshnserializerextrabundle.
@hshn
Hi Shota,
Thanks for your reply. I tried with 0.4.1 version and unfortunately I still can't see exported VichUploader based field. I remember to clear a cache.
Once again I'm putting a complete config and complete json output, maybe I'm doing something wrong. Anyway thanks for your work! It's a great and very helpful bundle.
# app/config/config.yml
hshn_serializer_extra:
vich_uploader:
classes:
"App\GeneralBundle\Entity\ProductImage": # works fine
files:
- { property: product, export_to: image }
- { property: product, export_to: thumbnail, filter: thumb_square }
"App\GeneralBundle\Entity\User": # doesn't work with or without "export_to" nor "thumbnail"
files:
- { property: avatar }
"App\GeneralBundle\Entity\Badge": # works fine
files:
- { property: badge, export_to: image }
- { property: badge, export_to: thumbnail, filter: thumb_square }
jms_serializer:
metadata:
directories:
FOSUserBundle:
namespace_prefix: "FOS\\UserBundle"
path: "%kernel.root_dir%/Resources/FOSUserBundle/serializer"
# app/Resources/FOSUserBundle/serializer/Model.User.yml
FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
id:
expose: true
username:
expose: true
slug:
expose: true
# I tried to expose here an avatar field as well, but it didn't help
// src/GeneralBundle/Entity/User.php
use FOS\UserBundle\Model\User as BaseUser;
// [...]
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="App\GeneralBundle\Repository\UserRepository")
* @Vich\Uploadable()
* @JMS\ExclusionPolicy("all")
*/
class User extends BaseUser
{
// [...]
/**
* @var string
*
* @ORM\Column(name="location", type="string", length=255, nullable=true)
* @Assert\Length(max="255")
* @JMS\Expose()
*/
protected $location;
/**
* @var string
*
* @ORM\Column(name="bio", type="text", nullable=true)
* @Assert\Length(max="10000")
* @JMS\Expose()
*/
protected $bio;
/**
* @var string
*
* @ORM\Column(name="avatar_filename", type="string", length=255, nullable=true)
* @Assert\Length(max="255")
*/
protected $avatarFilename;
/**
* @var File
*
* @Assert\File(
* maxSize="2M",
* mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
* )
* @Assert\NotBlank(groups={"Avatar"})
* @Vich\UploadableField(mapping="avatar", fileNameProperty="avatarFilename")
*/
protected $avatar;
// [...]
}
Serialized user without @JMS\Expose()
annotation in $avatar
field.
{
id: 5
username: "Alotro"
slug: "alotro"
location: "Poznań"
bio: "<p>...</p> "
blog: null
}
User with @JMS\Expose()
annotation in $avatar
field.
{
id: 5
username: "Alotro"
slug: "alotro"
location: "Poznań"
bio: "<p>...</p> "
blog: null
avatar: {}
}
from hshnserializerextrabundle.
Hi @hshn
Today I find out, why I don't get an avatar fields in serialized User entity. The reason is very simple, I just didn't upload any picture as an avatar into an entity! Entities that have uploaded avatar file works fine.
It's because here:
// hshn/serializer-extra-bundle/src/VichUploader/EventSubscriber.php
public function onPostSerialize(ObjectEvent $event)
// [...]
foreach ($files as $file) {
try {
$visitor->addData($file->getExportTo(), $this->uriResolver->resolve($object, $file, $configuration->getClass()));
} catch (UriResolverException $e) {
}
}
}
there is an exception caught and nothing happens with that. No new key is added to serialized object.
Actually developer doesn't know what happened. This can cause an unexpected behavior in your frontend, if you try to access a key that doesn't exist.
So what I suggest, is to add the defined property to serialized entity with null value.
foreach ($files as $file) {
try {
$visitor->addData($file->getExportTo(), $this->uriResolver->resolve($object, $file, $configuration->getClass()));
} catch (UriResolverException $e) {
$visitor->addData($file->getExportTo(), null);
}
}
What do you think about this solution? I can prepare a PR if you want, but it's just a one line and test to fix.
from hshnserializerextrabundle.
Hi @kwn
Thank you for your descriptions. I'd been trying to reproduce this issue but that was unexpected.
Actually developer doesn't know what happened. This can cause an unexpected behavior in your frontend, if you try to access a key that doesn't exist.
I agreed. However I think the default serialization behavior of JMSSerializerBundle does not expose null
value including its attribute. It depends on SerializationContext. (Also max_depth
should be effected by the context too.)
So I think we should improve exposing behavior to be same as jms/serializer
.
Thank you for your suggestion!
I'll implement it in the near feature.
from hshnserializerextrabundle.
Hello @hshn
Thank you for answering me.
Actually you are right, JSMSerializer doesn't expose null
but you can set an option in FOSRestBundle configuration to do that:
fos_rest:
[...]
serializer:
serialize_null: true
I'm pretty sure, that your bundle will be related to FOSRestBundle in most cases, so you can consider introducing a similar option as well.
Anyway, thank you for this bundle again. You saved a lot of my time :)
from hshnserializerextrabundle.
It's my pleasure :)
from hshnserializerextrabundle.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hshnserializerextrabundle.