1313
1414namespace ApiPlatform \Elasticsearch \Serializer ;
1515
16- use ApiPlatform \Metadata \HttpOperation ;
17- use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
1816use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
1917use Symfony \Component \PropertyInfo \PropertyTypeExtractorInterface ;
20- use Symfony \Component \Serializer \Exception \LogicException ;
2118use Symfony \Component \Serializer \Mapping \ClassDiscriminatorResolverInterface ;
2219use Symfony \Component \Serializer \Mapping \Factory \ClassMetadataFactoryInterface ;
2320use Symfony \Component \Serializer \NameConverter \NameConverterInterface ;
24- use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
2521use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
2622use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
2723use Symfony \Component \Serializer \SerializerAwareInterface ;
2824use Symfony \Component \Serializer \SerializerInterface ;
2925
3026/**
31- * Document denormalizer for Elasticsearch.
27+ * Document normalizer for Elasticsearch.
3228 *
3329 * @experimental
3430 *
3531 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
3632 */
37- final class DocumentNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
33+ final class DocumentNormalizer implements NormalizerInterface, SerializerAwareInterface
3834{
3935 public const FORMAT = 'elasticsearch ' ;
4036
4137 private readonly ObjectNormalizer $ decoratedNormalizer ;
4238
4339 public function __construct (
44- private readonly ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory ,
4540 ?ClassMetadataFactoryInterface $ classMetadataFactory = null ,
4641 private readonly ?NameConverterInterface $ nameConverter = null ,
4742 ?PropertyAccessorInterface $ propertyAccessor = null ,
@@ -56,66 +51,32 @@ public function __construct(
5651 /**
5752 * {@inheritdoc}
5853 */
59- public function supportsDenormalization (mixed $ data , string $ type , ?string $ format = null , array $ context = []): bool
60- {
61- return self ::FORMAT === $ format && $ this ->decoratedNormalizer ->supportsDenormalization ($ data , $ type , $ format , $ context );
62- }
63-
64- /**
65- * {@inheritdoc}
66- */
67- public function denormalize (mixed $ data , string $ type , ?string $ format = null , array $ context = []): mixed
54+ public function supportsNormalization (mixed $ data , ?string $ format = null , array $ context = []): bool
6855 {
69- if (\is_string ($ data ['_id ' ] ?? null ) && \is_array ($ data ['_source ' ] ?? null )) {
70- $ data = $ this ->populateIdentifier ($ data , $ type )['_source ' ];
56+ // Ensure that a resource is being normalized
57+ if (!\is_object ($ data )) {
58+ return false ;
7159 }
7260
73- return $ this ->decoratedNormalizer ->denormalize ($ data , $ type , $ format , $ context );
74- }
75-
76- /**
77- * {@inheritdoc}
78- */
79- public function supportsNormalization (mixed $ data , ?string $ format = null , array $ context = []): bool
80- {
81- // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
61+ // Only normalize for the elasticsearch format
8262 return self ::FORMAT === $ format ;
8363 }
8464
8565 /**
8666 * {@inheritdoc}
87- *
88- * @throws LogicException
8967 */
9068 public function normalize (mixed $ data , ?string $ format = null , array $ context = []): array |string |int |float |bool |\ArrayObject |null
9169 {
92- throw new LogicException (\sprintf ('%s is a write-only format. ' , self ::FORMAT ));
93- }
94-
95- /**
96- * Populates the resource identifier with the document identifier if not present in the original JSON document.
97- */
98- private function populateIdentifier (array $ data , string $ class ): array
99- {
100- $ identifier = 'id ' ;
101- $ resourceMetadata = $ this ->resourceMetadataCollectionFactory ->create ($ class );
102-
103- $ operation = $ resourceMetadata ->getOperation ();
104- if ($ operation instanceof HttpOperation) {
105- $ uriVariable = $ operation ->getUriVariables ()[0 ] ?? null ;
106-
107- if ($ uriVariable ) {
108- $ identifier = $ uriVariable ->getIdentifiers ()[0 ] ?? 'id ' ;
109- }
110- }
111-
112- $ identifier = null === $ this ->nameConverter ? $ identifier : $ this ->nameConverter ->normalize ($ identifier , $ class , self ::FORMAT );
70+ $ normalizedData = $ this ->decoratedNormalizer ->normalize ($ data , $ format , $ context );
11371
114- if (!isset ($ data ['_source ' ][$ identifier ])) {
115- $ data ['_source ' ][$ identifier ] = $ data ['_id ' ];
72+ // Add _id and _source if not already present
73+ // This is a basic implementation and might need to be more sophisticated based on specific needs.
74+ // It assumes 'id' is the primary identifier for the resource.
75+ if (\is_array ($ normalizedData ) && !isset ($ normalizedData ['_id ' ]) && isset ($ normalizedData ['id ' ])) {
76+ $ normalizedData = ['_id ' => (string ) $ normalizedData ['id ' ], '_source ' => $ normalizedData ];
11677 }
11778
118- return $ data ;
79+ return $ normalizedData ;
11980 }
12081
12182 /**
0 commit comments