coosos/jms-serializer-bidirectional-relation
Adds JMS Serializer subscribers that embed a _mapping_bidirectional_relation in serialized data so bidirectional associations can be restored on deserialize. Supports Symfony or standalone setup via event subscribers, with annotations to enable mapping on root models and exclude fields.
User ↔ Post where User has posts and Post has author) during JSON serialization/deserialization. This is particularly valuable in APIs, caching layers, or event-driven architectures where object graphs must be reconstructed faithfully.serializable trait or packages like spatie/laravel-data handle serialization but lack built-in bidirectional relation support. This package fills a gap for complex object graphs.@SerializerBidirectionalRelation to root models and @ExcludeFromMapping to fields, which may introduce slight maintenance friction but is explicit and declarative.Serializer component. Migration to Symfony’s serializer would require rewriting subscribers or finding a fork/maintained alternative.Why JMS Serializer?
Serializer (which has built-in support for bidirectional relations via DenormalizationContext).spatie/laravel-arrayable or custom logic (e.g., manual relation population post-deserialization) suffice for simpler cases?Maintenance Plan
Denormalizer interfaces in Symfony’s serializer)?Testing Strategy
Performance
Alternatives Assessment
DenormalizationContext or Laravel’s App\Events for relation reconstruction) vs. this package.Denormalizer could replicate functionality. Evaluate if the effort justifies the package’s risks.Assessment Phase:
User::posts() ↔ Post::user()).Dependency Setup:
composer require coosos/jms-serializer-bidirectional-relation
config/services.php:
$container->registerForAutoconfiguration(MapSerializerSubscriber::class)->addTag('jms_serializer.event_subscriber');
$container->registerForAutoconfiguration(MapDeserializerSubscriber::class)->addTag('jms_serializer.event_subscriber');
SerializerBuilder as shown in the README.Annotation Rollout:
@SerializerBidirectionalRelation to root DTOs/models requiring bidirectional mapping.@ExcludeFromMapping where needed (e.g., computed properties).User ↔ Post).Validation:
$user = $serializer->deserialize($json, User::class, 'json');
$this->assertCount(1, $user->posts);
$this->assertEquals($user, $user->posts->first()->user);
ObjectConstructor or MetadataFactory for Eloquent hydration. May require custom Handler interfaces for complex cases.spatie/laravel-fractal or darkaonline/l5-swagger, ensure the package’s _mapping_bidirectional_relation key doesn’t conflict with API contracts._mapping_bidirectional_relation key is preserved and doesn’t bloat cache size.@SerializerBidirectionalRelation to new root models).Serializer if critical updates are needed._mapping_bidirectional_relation key in serialized output can be inspected for correctness, but debugging deserialization failures may require logging subscriber events.MapDeserializerSubscriber to trace relation reconstruction:
public function onDeserialize(DeserializeEvent $event) {
\Log::debug('Bidirectional mapping', [
'data' => $event->getData(),
'context' => $event->getContext(),
]);
// ...
}
User ↔ Post IDs are consistent)._mapping_bidirectional_relation increases payload size by ~10–50% (depends on graph complexity). Compress responses if bandwidth is a concern.Serializer with custom DenormalizationContext for better control (though requires rewriting logic).| Failure Scenario | Impact | Mitigation |
|---|---|---|
Missing @SerializerBidirectionalRelation |
Silent relation loss | CI linting for missing annotations; runtime validation. |
Corrupted _mapping_bidirectional_relation |
Partial/invalid relations | Sanitize input data; use a fallback des |
How can I help you explore Laravel packages today?