Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #53898 [Serializer] Add context for `CamelCaseToSnakeCaseName…
…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
Showing
4 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Serializer/Exception/UnexpectedPropertyException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters