Skip to content

Commit

Permalink
feature #14987 [FrameworkBundle] Configurable Serializer name convert…
Browse files Browse the repository at this point in the history
…er (dunglas)

This PR was merged into the 2.8 branch.

Discussion
----------

[FrameworkBundle] Configurable Serializer name converter

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | symfony/symfony-docs#5483

- [x] Add tests

Commits
-------

e500a71 [FrameworkBundle] Configurable Serializer name converter
  • Loading branch information
fabpot committed Aug 1, 2015
2 parents 0de8905 + e500a71 commit f02a5dc
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 4 deletions.
Expand Up @@ -660,6 +660,7 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
->children()
->booleanNode('enable_annotations')->defaultFalse()->end()
->scalarNode('cache')->end()
->scalarNode('name_converter')->end()
->end()
->end()
->end()
Expand Down
Expand Up @@ -970,6 +970,10 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
1, new Reference($config['cache'])
);
}

if ($config['name_converter']) {
$container->getDefinition('serializer.normalizer.object')->replaceArgument(1, new Reference($config['name_converter']));
}
}

/**
Expand Down
Expand Up @@ -217,5 +217,6 @@
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="cache" type="xsd:string" />
<xsd:attribute name="enable-annotations" type="xsd:boolean" />
<xsd:attribute name="name-converter" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
Expand Up @@ -22,7 +22,7 @@
<!-- Normalizer -->
<service id="serializer.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer" public="false">
<argument type="service" id="serializer.mapping.class_metadata_factory" />
<argument>null</argument>
<argument>null</argument> <!-- name converter -->
<argument type="service" id="serializer.property_accessor" />

<!-- Run after all custom serializers -->
Expand Down Expand Up @@ -55,5 +55,8 @@
<service id="serializer.encoder.json" class="%serializer.encoder.json.class%" public="false">
<tag name="serializer.encoder" />
</service>

<!-- Name converter -->
<service id="serializer.name_converter.camel_case_to_snake_case" class="Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter" public="false" />
</services>
</container>
Expand Up @@ -61,7 +61,12 @@
'debug' => true,
'file_cache_dir' => '%kernel.cache_dir%/annotations',
),
'serializer' => array('enabled' => true),
'serializer' => array(
'enabled' => true,
'enable_annotations' => true,
'cache' => 'serializer.mapping.cache.apc',
'name_converter' => 'serializer.name_converter.camel_case_to_snake_case',
),
'ide' => 'file%%link%%format',
'request' => array(
'formats' => array(
Expand Down
Expand Up @@ -39,6 +39,6 @@
</framework:translator>
<framework:validation enabled="true" cache="apc" />
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
<framework:serializer enabled="true" />
<framework:serializer enabled="true" enable-annotations="true" cache="serializer.mapping.cache.apc" name-converter="serializer.name_converter.camel_case_to_snake_case" />
</framework:config>
</container>
Expand Up @@ -47,7 +47,11 @@ framework:
cache: file
debug: true
file_cache_dir: %kernel.cache_dir%/annotations
serializer: { enabled: true }
serializer:
enabled: true
enable_annotations: true
cache: serializer.mapping.cache.apc
name_converter: serializer.name_converter.camel_case_to_snake_case
ide: file%%link%%format
request:
formats:
Expand Down
Expand Up @@ -446,6 +446,13 @@ public function testSerializerEnabled()
{
$container = $this->createContainerFromFile('full');
$this->assertTrue($container->has('serializer'));

$argument = $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0);

$this->assertCount(1, $argument);
$this->assertEquals('Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader', $argument[0]->getClass());
$this->assertEquals(new Reference('serializer.mapping.cache.apc'), $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
$this->assertEquals(new Reference('serializer.name_converter.camel_case_to_snake_case'), $container->getDefinition('serializer.normalizer.object')->getArgument(1));
}

public function testAssetHelperWhenAssetsAreEnabled()
Expand Down

0 comments on commit f02a5dc

Please sign in to comment.