Skip to content

Commit bd5f179

Browse files
Fix #2686: deprecate allow_plain_identifiers option (#1326)
* Fix #2686: deprecate allow_plain_identifiers option * fix review * fix review * fix review * update example
1 parent 2b22cce commit bd5f179

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

core/bootstrap.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
139139
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
140140
use Symfony\Component\Serializer\Serializer;
141141

142+
/** @deprecated since API Platform 2.7, will be removed in API Platform 3.0 */
142143
$allowPlainIdentifiers = false;
143144
$debug = true;
144145
$defaultContext = [];

core/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ api_platform:
3232
path_segment_name_generator: 'api_platform.path_segment_name_generator.underscore'
3333

3434
# Allow using plain IDs for JSON format.
35+
# Deprecated since API Platform 2.7, will be removed in API Platform 3.0
3536
allow_plain_identifiers: false
3637

3738
validator:

core/serialization.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,54 @@ class Person
408408
409409
```
410410

411+
### Plain Identifiers
412+
413+
Instead of sending an IRI to set a relation, you may want to send a plain identifier. To do so, you must create your own denormalizer:
414+
415+
```php
416+
<?php
417+
// api/src/Serializer/PlainIdentifierDenormalizer
418+
419+
namespace App\Serializer;
420+
421+
use ApiPlatform\Core\Api\IriConverterInterface;
422+
use App\Entity\Dummy;
423+
use App\Entity\RelatedDummy;
424+
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
425+
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
426+
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
427+
428+
class PlainIdentifierDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface
429+
{
430+
use DenormalizerAwareTrait;
431+
432+
private $iriConverter;
433+
434+
public function __construct(IriConverterInterface $iriConverter)
435+
{
436+
$this->iriConverter = $iriConverter;
437+
}
438+
439+
/**
440+
* {@inheritdoc}
441+
*/
442+
public function denormalize($data, $class, $format = null, array $context = [])
443+
{
444+
$data['relatedDummy'] = $this->iriConverter->getItemIriFromResourceClass(RelatedDummy::class, ['id' => $data['relatedDummy']]);
445+
446+
return $this->denormalizer->denormalize($data, $class, $format, $context + [__CLASS__ => true]);
447+
}
448+
449+
/**
450+
* {@inheritdoc}
451+
*/
452+
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
453+
{
454+
return \in_array($format, ['json', 'jsonld'], true) && is_a($type, Dummy::class, true) && !empty($data['relatedDummy']) && !isset($context[__CLASS__]);
455+
}
456+
}
457+
```
458+
411459
## Calculated Field
412460

413461
Sometimes you need to expose calculated fields. This can be done by leveraging the groups. This time not on a property, but on a method.

0 commit comments

Comments
 (0)