trying to get the sf4 serializer component to use snake_case as the default:
Symfony\Component\Serializer\Normalizer\ObjectNormalizer:
public: true
arguments: ['#serializer.mapping.class_metadata_factory', '#serializer.name_converter.camel_case_to_snake_case']
tags: [serializer.normalizer]
works.
but now DateTime is being normalized to empty arrays.
I don't get why, without the config changes, its normalized to a date string, as you would expect.
What am i doing wrong here?
turns out, you simply need to enable a name_converter the proper way like this instead:
# config/packages/framework.yaml
framework:
# ...
serializer:
name_converter: 'serializer.name_converter.camel_case_to_snake_case'
see
https://symfony.com/doc/current/serializer.html#enabling-a-name-converter
https://github.com/symfony/symfony/issues/40818
Related
I have an Rest API built on Symfony 2.7 Framework with FOSRestBundle and JMSSerializerBundle. I have a look to yml reference and annotations.
I have choosen to define how each entity of my model is serialized with yml.
I have seen that we can serialize Datetime object on a specific format :
#JMS\Type("DateTime<'d-m-Y'>")
But I don't know the correct syntax used with yml definition, I have tried :
my_field:
expose: true
type: datetime
format: 'd-m-Y'
And
my_field:
expose: true
type: datetime<'d-m-Y'>
I don't want to use Annotations because I have a lot of yaml files.
But the field is not serialized...
Anyone can help me ?
I put it as an answer in case it helps more people:
my_field:
expose: true
type: DateTime<'d-m-Y'>
Is it possible to set the default value for enableMaxDepthChecks to true?
Currently I have to add the following annotation to every route:
#View(serializerEnableMaxDepthChecks=true)
Is there a way to set this in the config or elsewhere to be the default?
Place this into your config.yml
jms_serializer:
default_context:
serialization:
enable_max_depth_checks: true
BUT! After making these changes, the parameter serializerEnableMaxDepthChecks becomes non-working, so you can't set it to false , and I don't know why...
I have a bundle with its custom *Extension class where I need to read configuration from another bundle (SecurityBundle in particular) where I have for example.
security:
...
firewalls:
main:
pattern: '^%url_prefix%'
I'm wondering how can I get value for security.firewalls.main.pattern with interpolated url_prefix parameter?
Retrieving configuration values (of any bundle different than the one into what your extension is located, because you are in an extension) is not supported, and it seems that'll not be in the future.
The only ways are:
Define a parameter representing the whole option's value (as pointed by this answer on a similar question):
# app/config/security.yml
parameters:
firewalls.main.pattern: '^%url_prefix%'
# ...
security:
# ...
firewalls:
main:
pattern: '^%url_prefix%'
Parse your config.yml using the Yaml component:
$yamlPath = $this->getParameter('kernel.root_dir').'/config/security.yml';
$config = Symfony\Component\Yaml\Yaml::parse(file_get_contents($yamlPath));
// The option value
$value = $config['security']['firewalls']['main']['pattern'];
I think that's really a pity to don't be able to retrieve a config option from any container-aware context without doing such hacks.
I'm trying to implement OAuth2 in my actual symfony2 project using the FOSOAuthServerBundle.
I've been following this Article to implement it.
Since i don't use FOS User bundle i had to create a UserProvider.
I'm also using A User Repository as he did in his Article.
I've been stuck with this error :
ContextErrorException: Catchable Fatal Error: Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, none given, called in /app/cache/dev/appDevDebugProjectContainer.php on line 3527 and defined in /app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php line 67
I've followed the given code for services.yml:
And converted it to Yaml, here my file (services.yml) :
imports:
- { resource: parameters.yml }
parameters:
# wca_transition.example.class: WCA\TransitionBundle\Example
collection.import.from_email: "%import_from_email%"
collection.import.to_email: "%import_to_email%"
platform.entity.user.class: WCA\ServerBundle\Entity\Profile
platform.user.provider.class: WCA\ServerBundle\Provider\ProfileProvider
services:
# wca_betosee_transition.example:
# class: %wca_transition.example.class%
# arguments: [#service_id, "plain_value", %parameter%]
collectionImportCommand:
class: WCA\ServerBundle\Command\CollectionImporterCommand
calls:
- [setContainer, ["#service_container"] ]
platform.user.manager:
class: Doctrine\ORM\EntityManager
factory-service: doctrine
factory-method: getManagerForClass
arguments: ["%platform.entity.user.class%"]
platform.user.repository:
class: WCA\ServerBundle\Model\ProfileRepository
factory-service: platform.user.manager
factory-method: getRepository
arguments: ["%platform.entity.user.class%"]
platform.user.provider:
class: "%platform.user.provider.class%"
# arguments: ["#platform.user.repository"]
platform.grant_type.api_key:
class: WCA\ServerBundle\OAuth\ApiKeyGrantExtension
arguments: ["#platform.user.repository"]
tags:
- { name: fos_oauth_server.grant_extension, uri: http://mywebsite.local/grants/api_key }
I suppose the given error is coming from my configuration but i don't see what could be wrong since i followed what was given. Either the services created don't get the given arguments ("none given") or i don't pass the correct ones.
Any ideas or hints?
PS : If you need any other files, don't hesitate to ask, i'll update the post.
I doubt if it will help but try changing factory-service,factory-method to factory_service and factory_method to match the Symfony documentation.
Sounds like it is the only article that describes very well how to implement FOS OAuth2 bundle, even better than their own documentation.
But for some reason, services are defined in xml which I find very weird and ugly.
Anyway, as #Cerad mentioned, if you translate the xml service to the yaml format, then you'll have to replace the - by _ and it will work fine.
Does the symfony2 can handle only flat parameters?
Say we have:
services:
manager:
class: blabla
arguments: [%app.vat%]
and in app.yml :
parameters:
app.vat: 24.5
it works, but
parameters:
app:
vat: 24.5
does not work. Is there some special syntax to access arrays or this is not possible?
This is indeed possible. You can access the values from your example in your code like this:
$config = $this->get('service_container')->getParameter('app.vat');
If this is still not working you should try to rename "app" into something else (e.g. "application"). Symfony preserves the name "app" on many places and handles it in a special way.