10000 [FrameworkBundle] Configurable Serializer name converter by dunglas · Pull Request #14987 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Configurable Serializer name converter #14987

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

Merged
merged 1 commit into from
Aug 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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