-
Notifications
You must be signed in to change notification settings - Fork 1
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
Exporting file/image doesn't work with inherited entity #1
Comments
@hshn 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 {
id: 5
username: "Alotro"
slug: "alotro"
location: "Poznań"
bio: "<p>...</p> "
blog: null
} User with {
id: 5
username: "Alotro"
slug: "alotro"
location: "Poznań"
bio: "<p>...</p> "
blog: null
avatar: {}
} |
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. |
Hi @kwn Thank you for your descriptions. I'd been trying to reproduce this issue but that was unexpected.
I agreed. However I think the default serialization behavior of JMSSerializerBundle does not expose So I think we should improve exposing behavior to be same as Thank you for your suggestion! |
Hello @hshn Thank you for answering me. Actually you are right, JSMSerializer doesn't expose 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 :) |
It's my pleasure :) |
Hello,
I'm trying to export an user's avatar. My user entity extends FOSUserBundle:User. My configuration:
JMS\Expose()
annotation works (after using additional directory with metadata). What is more, if I setExpose()
to$avatar
field in User entity it exposes (as a empty object).The text was updated successfully, but these errors were encountered: