Skip to content

Commit

Permalink
feature #53898 [Serializer] Add context for `CamelCaseToSnakeCaseName…
Browse files Browse the repository at this point in the history
…Converter` (AurelienPillevesse)

This PR was merged into the 7.1 branch.

Discussion
----------

[Serializer] Add context for `CamelCaseToSnakeCaseNameConverter`

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        |
| License       | MIT

Currently, when we use a Symfony 7.0 project (or 6.4 for example) with the `symfony/serializer` component installed.

With the `#[MapRequestPayload]` feature and this configuration :
```yaml
framework:
    serializer:
        name_converter: 'serializer.name_converter.camel_case_to_snake_case'
```

This configuration forces Symfony to return to the snake case format.
But when sending these two `POST` requests :
```json
{
    "lastName": "MyLastName"
}
```

```json
{
    "last_name": "MyLastName"
}
```
They will be handled exactly the same.

The idea is to add a context `CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES` which can be used in `#[MapRequestPayload]` attribute to only accept the `POST` request with `last_name` attribute body.

Example :
```php
class MyRouteInput
{
    public string $lastName;
}

#[MapRequestPayload(serializationContext: [CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES => true])]
MyRouteInput $input
```

**[Note]**
This is a first draft to expose my idea. The exception thrown and where the new condition is added can probably be improved.
A possible improvement can be to use the existing logic with `PartialDenormalizationException`.
If for you it can added in 6.4 and 7.0 instead of 7.1, i'm OK.

Commits
-------

04e22ec add context to force snake_case
  • Loading branch information
fabpot committed Mar 17, 2024
2 parents 8574147 + 04e22ec commit a5c0b0c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
use Symfony\Component\Serializer\Exception\UnexpectedPropertyException;
use Symfony\Component\Serializer\Exception\UnsupportedFormatException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -197,6 +198,8 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
throw new UnsupportedMediaTypeHttpException(sprintf('Unsupported format: "%s".', $format), $e);
} catch (NotEncodableValueException $e) {
throw new BadRequestHttpException(sprintf('Request payload contains invalid "%s" data.', $format), $e);
} catch (UnexpectedPropertyException $e) {
throw new BadRequestHttpException(sprintf('Request payload contains invalid "%s" property.', $e->property), $e);
}
}
}
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Exception;

/**
* UnexpectedPropertyException.
*
* @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>
*/
class UnexpectedPropertyException extends \UnexpectedValueException implements ExceptionInterface
{
public function __construct(
public readonly string $property,
?\Throwable $previous = null,
) {
$msg = sprintf('Property is not allowed ("%s" is unknown).', $this->property);

parent::__construct($msg, 0, $previous);
}
}
Expand Up @@ -11,13 +11,21 @@

namespace Symfony\Component\Serializer\NameConverter;

use Symfony\Component\Serializer\Exception\UnexpectedPropertyException;

/**
* CamelCase to Underscore name converter.
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>
*/
class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
class CamelCaseToSnakeCaseNameConverter implements AdvancedNameConverterInterface
{
/**
* Require all properties to be written in snake_case.
*/
public const REQUIRE_SNAKE_CASE_PROPERTIES = 'require_snake_case_properties';

/**
* @param array|null $attributes The list of attributes to rename or null for all attributes
* @param bool $lowerCamelCase Use lowerCamelCase style
Expand All @@ -28,7 +36,7 @@ public function __construct(
) {
}

public function normalize(string $propertyName): string
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
{
if (null === $this->attributes || \in_array($propertyName, $this->attributes, true)) {
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
Expand All @@ -37,8 +45,12 @@ public function normalize(string $propertyName): string
return $propertyName;
}

public function denormalize(string $propertyName): string
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
{
if (($context[self::REQUIRE_SNAKE_CASE_PROPERTIES] ?? false) && $propertyName !== $this->normalize($propertyName, $class, $format, $context)) {
throw new UnexpectedPropertyException($propertyName);
}

$camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', fn ($match) => ('.' === $match[1] ? '_' : '').strtoupper($match[2]), $propertyName);

if ($this->lowerCamelCase) {
Expand Down
Expand Up @@ -12,11 +12,13 @@
namespace Symfony\Component\Serializer\Tests\NameConverter;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\UnexpectedPropertyException;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>
*/
class CamelCaseToSnakeCaseNameConverterTest extends TestCase
{
Expand Down Expand Up @@ -55,4 +57,20 @@ public static function attributeProvider()
['this_is_a_test', 'ThisIsATest', false],
];
}

public function testDenormalizeWithContext()
{
$nameConverter = new CamelCaseToSnakeCaseNameConverter(null, true);
$denormalizedValue = $nameConverter->denormalize('last_name', context: [CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES]);

$this->assertSame($denormalizedValue, 'lastName');
}

public function testErrorDenormalizeWithContext()
{
$nameConverter = new CamelCaseToSnakeCaseNameConverter(null, true);

$this->expectException(UnexpectedPropertyException::class);
$nameConverter->denormalize('lastName', context: [CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES => true]);
}
}

0 comments on commit a5c0b0c

Please sign in to comment.