Skip to content

[Serializer] Add SnakeCaseToCamelCaseNameConverter #20160

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

Closed
wants to merge 1 commit into from

Conversation

thibaut22200
Copy link
Contributor

Document symfony/symfony#58060
and closes #20151

$normalizer->normalize($kevin);
// ['first_name' => 'Kévin'];

$anne = $normalizer->denormalize(['first_name' => 'Anne'], 'Person');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example here seems to be the same as the one for the CamelCaseToSnakeCaseNameConverter. That doesn't look right to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh Fixed the example, thanks you.

@OskarStark OskarStark changed the title [Serializer] Add SnakeCaseToCamelCaseNameConverter [Serializer] Add SnakeCaseToCamelCaseNameConverter Aug 25, 2024
@xabbuh
Copy link
Member

xabbuh commented Aug 28, 2024

I think there is still some misunderstanding. When the name converter is being used the name of the property is converted but not the value. So I would expect a class with a property like $full_name and a key like fullName in the normalised data.

Comment on lines +645 to +663
class Person
{
public function __construct(
private string $fullName,
) {
}

public function getFullName(): string
{
return $this->fullName;
}
}

$john = new Person('john_doe');
$normalizer->normalize($john);
// ['full_name' => 'johnDoe'];

$john = $normalizer->denormalize(['full_name' => 'johnDoe'], 'Person');
// Person object with fullName: 'jonh_doe'
Copy link
Contributor

@OskarStark OskarStark Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class Person
{
public function __construct(
private string $fullName,
) {
}
public function getFullName(): string
{
return $this->fullName;
}
}
$john = new Person('john_doe');
$normalizer->normalize($john);
// ['full_name' => 'johnDoe'];
$john = $normalizer->denormalize(['full_name' => 'johnDoe'], 'Person');
// Person object with fullName: 'jonh_doe'
class Person
{
public function __construct(
private string $full_name,
) {
}
public function getFullName(): string
{
return $this->full_name;
}
}
$john = new Person('john');
$normalizer->normalize($john);
// ['fullName' => 'john'];
$john = $normalizer->denormalize(['fullName' => 'john'], 'Person');
// $john->getFullName() -> 'john'

cc @xabbuh

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it works this way when the property is not public and the getter has a camel case name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it's for camel_cased public properties and getters/setters (Eloquent-like).

@wouterj wouterj added this to the 7.2 milestone Dec 7, 2024
@wouterj
Copy link
Member

wouterj commented Dec 7, 2024

Hi!

We've rewritten the Serializer documentation. In this process, we've merged the contents of the component and framework documentation. Do you have time to update this PR to instead document this new feature in https://symfony.com/doc/current/serializer.html#camelcase-to-snake-case ? No worries if you can't make the change, we can always take over this PR and make this change.

@alamirault
Copy link
Contributor

Hi, I opened a new PR based on new merged content: #20774

@OskarStark OskarStark closed this Mar 18, 2025
javiereguiluz added a commit that referenced this pull request Mar 19, 2025
…irault)

This PR was merged into the 7.2 branch.

Discussion
----------

[Serializer] Add SnakeCaseToCamelCaseNameConverter

Fix #20151

Continue work started in #20160

Commits
-------

4213bb5 [Serializer] Add SnakeCaseToCamelCaseNameConverter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Serializer] Add SnakeCaseToCamelCaseNameConverter
7 participants