Skip to content

[Serializer] Allow to pass a single value for the groups opt #27503

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
Jul 1, 2018

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Jun 5, 2018

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

Shortcut syntax similar to #20509 but for the context: allows to pass a string instead of an array of string in the serialization context when only one group if used.

Before:

$serializer->serialize($foo, 'json', ['groups' => ['myGroup']];

/** @ApiResource(normalizationContext={"groups"={"myGroup"}}) */

After:

$serializer->serialize($foo, 'json', ['groups' => 'myGroup'];

/** @ApiResource(normalizationContext={"groups"="myGroup") */

@lyrixx
Copy link
Member

lyrixx commented Jun 5, 2018

Huge 👍 because the previous && \is_array($context[static::GROUPS] is really a time killer

@dunglas
Copy link
Member Author

dunglas commented Jun 5, 2018

Travis failure not related.

@nicolas-grekas nicolas-grekas added this to the next milestone Jun 6, 2018
Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

Can you add a note in the changelog file?

Copy link

@20uf 20uf left a comment

Choose a reason for hiding this comment

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

Nice job !

if (isset($context[static::GROUPS]) && \is_array($context[static::GROUPS])) {
$groups = $context[static::GROUPS];
if (isset($context[static::GROUPS])) {
$groups = (array) $context[static::GROUPS];
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this check is_scalar||is_array instead of casting objects?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@dunglas dunglas force-pushed the serializer-1-group branch from b7e8a89 to 5b39203 Compare June 30, 2018 10:45
@nicolas-grekas
Copy link
Member

Thank you @dunglas.

@nicolas-grekas nicolas-grekas merged commit 5b39203 into symfony:master Jul 1, 2018
nicolas-grekas added a commit that referenced this pull request Jul 1, 2018
…ps opt (dunglas)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Serializer] Allow to pass a single value for the groups opt

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |  n/a
| License       | MIT
| Doc PR        | todo

Shortcut syntax similar to #20509 but for the context: allows to pass a string instead of an array of string in the serialization context when only one group if used.

Before:

```php
$serializer->serialize($foo, 'json', ['groups' => ['myGroup']];

/** @ApiResource(normalizationContext={"groups"={"myGroup"}}) */
```

After:

```php
$serializer->serialize($foo, 'json', ['groups' => 'myGroup'];

/** @ApiResource(normalizationContext={"groups"="myGroup") */
```

Commits
-------

5b39203 [Serializer] Allow to pass a single value for the groups opt
@dunglas dunglas deleted the serializer-1-group branch July 2, 2018 09:20
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Sep 11, 2018
… opt (dunglas, javiereguiluz)

This PR was merged into the master branch.

Discussion
----------

[Serializer] Allow to pass a single value for the groups opt

symfony/symfony#27503

Commits
-------

ae558f4 Added the missing versionadded directive
e4eae25 Replaced a "note" by a "tip"
5e0a91e [Serializer] Allow to pass a single value for the groups opt
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
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.

10 participants