Skip to content

Commit 065ddb6

Browse files
committed
change name converter configuration node
1 parent 6139498 commit 065ddb6

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

core/serialization.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ App\Entity\Book:
139139
```
140140

141141
In the previous example, the `name` property will be visible when reading (`GET`) the object, and it will also be available
142-
to write (`PUT/POST`). The `author` property will be write-only; it will not be visible when serialized responses are
142+
to write (`PUT/POST`). The `author` property will be write-only; it will not be visible when serialized responses are
143143
returned by the API.
144144

145145
Internally, API Platform passes the value of the `normalization_context` to the Symfony Serializer during the normalization
@@ -219,9 +219,9 @@ In the following JSON document, the relation from a book to an author is represe
219219
}
220220
```
221221

222-
However, for performance reasons, it is sometimes preferable to avoid forcing the client to issue extra HTTP requests.
223-
It is possible to embed related objects (in their entirety, or only some of their properties) directly in the parent
224-
response through the use of serialization groups. By using the following serialization groups annotations (`@Groups`),
222+
However, for performance reasons, it is sometimes preferable to avoid forcing the client to issue extra HTTP requests.
223+
It is possible to embed related objects (in their entirety, or only some of their properties) directly in the parent
224+
response through the use of serialization groups. By using the following serialization groups annotations (`@Groups`),
225225
a JSON representation of the author is embedded in the book response:
226226

227227
```php
@@ -416,7 +416,7 @@ final class BookContextBuilder implements SerializerContextBuilderInterface
416416
{
417417
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
418418
$resourceClass = $context['resource_class'] ?? null;
419-
419+
420420
if ($resourceClass === Book::class && isset($context['groups']) && $this->authorizationChecker->isGranted('ROLE_ADMIN') && false === $normalization) {
421421
$context['groups'][] = 'admin:input';
422422
}
@@ -426,17 +426,17 @@ final class BookContextBuilder implements SerializerContextBuilderInterface
426426
}
427427
```
428428

429-
If the user has the `ROLE_ADMIN` permission and the subject is an instance of Book, `admin_input` group will be dynamically added to the
430-
denormalization context. The `$normalization` variable lets you check whether the context is for normalization (if `TRUE`) or denormalization
429+
If the user has the `ROLE_ADMIN` permission and the subject is an instance of Book, `admin_input` group will be dynamically added to the
430+
denormalization context. The `$normalization` variable lets you check whether the context is for normalization (if `TRUE`) or denormalization
431431
(`FALSE`).
432432

433433
## Changing the Serialization Context on a Per-item Basis
434434

435-
The example above demonstrates how you can modify the normalization/denormalization context based on the current user
435+
The example above demonstrates how you can modify the normalization/denormalization context based on the current user
436436
permissions for all books. Sometimes, however, the permissions vary depending on what book is being processed.
437437

438-
Think of ACL's: User "A" may retrieve Book "A" but not Book "B". In this case, we need to leverage the power of the
439-
Symfony Serializer and register our own normalizer that adds the group on every single item (note: priority `64` is
438+
Think of ACL's: User "A" may retrieve Book "A" but not Book "B". In this case, we need to leverage the power of the
439+
Symfony Serializer and register our own normalizer that adds the group on every single item (note: priority `64` is
440440
an example; it is always important to make sure your normalizer gets loaded first, so set the priority to whatever value
441441
is appropriate for your application; higher values are loaded earlier):
442442

@@ -449,7 +449,7 @@ services:
449449
- { name: 'serializer.normalizer', priority: 64 }
450450
```
451451

452-
The Normalizer class is a bit harder to understand, because it must ensure that it is only called once and that there is no recursion.
452+
The Normalizer class is a bit harder to understand, because it must ensure that it is only called once and that there is no recursion.
453453
To accomplish this, it needs to be aware of the parent Normalizer instance itself.
454454

455455
Here is an example:
@@ -489,7 +489,7 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Normal
489489
490490
return $this->normalizer->normalize($object, $format, $context);
491491
}
492-
492+
493493
public function supportsNormalization($data, $format = null, array $context = [])
494494
{
495495
// Make sure we're not called twice
@@ -499,7 +499,7 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Normal
499499
500500
return $data instanceof Book;
501501
}
502-
502+
503503
private function userHasPermissionsForBook($object): bool
504504
{
505505
// Get permissions from user in $this->tokenStorage
@@ -509,17 +509,17 @@ class BookAttributeNormalizer implements ContextAwareNormalizerInterface, Normal
509509
}
510510
```
511511

512-
This will add the serialization group `can_retrieve_book` only if the currently logged-in user has access to the given book
512+
This will add the serialization group `can_retrieve_book` only if the currently logged-in user has access to the given book
513513
instance.
514514

515-
Note: In this example, we use the `TokenStorageInterface` to verify access to the book instance. However, Symfony
515+
Note: In this example, we use the `TokenStorageInterface` to verify access to the book instance. However, Symfony
516516
provides many useful other services that might be better suited to your use case. For example, the [`AuthorizationChecker`](https://symfony.com/doc/current/components/security/authorization.html#authorization-checker).
517517

518518
## Name Conversion
519519

520520
The Serializer Component provides a handy way to map PHP field names to serialized names. See the related [Symfony documentation](http://symfony.com/doc/master/components/serializer.html#converting-property-names-when-serializing-and-deserializing).
521521

522-
To use this feature, declare a new service with id `app.name_converter`. For example, you can convert `CamelCase` to
522+
To use this feature, declare a new name converter service. For example, you can convert `CamelCase` to
523523
`snake_case` with the following configuration:
524524

525525
```yaml
@@ -534,9 +534,11 @@ api_platform:
534534
name_converter: 'Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter'
535535
```
536536

537+
If symfony's `MetadataAwareNameConverter` is available it'll be used by default. If you specify one in ApiPlatform configuration, it'll be used. Note that you can use decoration to benefit from this name converter in your own implementation.
538+
537539
## Decorating a Serializer and Adding Extra Data
538540

539-
In the following example, we will see how we add extra informations to the serialized output. Here is how we add the
541+
In the following example, we will see how we add extra informations to the serialized output. Here is how we add the
540542
date on each request in `GET`:
541543

542544
```yaml
@@ -595,7 +597,7 @@ final class ApiNormalizer implements NormalizerInterface, DenormalizerInterface,
595597
{
596598
return $this->decorated->denormalize($data, $class, $format, $context);
597599
}
598-
600+
599601
public function setSerializer(SerializerInterface $serializer)
600602
{
601603
if($this->decorated instanceof SerializerAwareInterface) {
@@ -621,7 +623,7 @@ the `ApiPlatform\Core\Annotation\ApiProperty` annotation. For example:
621623
class Book
622624
{
623625
// ...
624-
626+
625627
/**
626628
* @ApiProperty(identifier=true)
627629
*/
@@ -656,7 +658,7 @@ App\Entity\Book:
656658
```
657659

658660
In some cases, you will want to set the identifier of a resource from the client (e.g. a client-side generated UUID, or a slug).
659-
In such cases, you must make the identifier property a writable class property. Specifically, to use client-generated IDs, you
661+
In such cases, you must make the identifier property a writable class property. Specifically, to use client-generated IDs, you
660662
must do the following:
661663

662664
1. create a setter for the identifier of the entity (e.g. `public function setId(string $id)`) or make it a `public` property ,

0 commit comments

Comments
 (0)