16
16
use ApiPlatform \Core \Api \IriConverterInterface ;
17
17
use ApiPlatform \Core \Api \OperationType ;
18
18
use ApiPlatform \Core \Api \ResourceClassResolverInterface ;
19
- use ApiPlatform \Core \Exception \InvalidArgumentException ;
20
19
use ApiPlatform \Core \Exception \ItemNotFoundException ;
21
20
use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
22
21
use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
25
24
use ApiPlatform \Core \Serializer \AbstractItemNormalizer ;
26
25
use ApiPlatform \Core \Util \ClassInfoTrait ;
27
26
use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
28
- use Symfony \Component \Serializer \Exception \LogicException ;
27
+ use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
28
+ use Symfony \Component \Serializer \Exception \RuntimeException ;
29
+ use Symfony \Component \Serializer \Exception \UnexpectedValueException ;
29
30
use Symfony \Component \Serializer \NameConverter \NameConverterInterface ;
30
31
use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
31
32
use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
@@ -63,22 +64,7 @@ public function supportsNormalization($data, $format = null, array $context = []
63
64
*/
64
65
public function normalize ($ object , $ format = null , array $ context = [])
65
66
{
66
- if (!$ this ->handleNonResource && $ object !== $ transformed = $ this ->transformOutput ($ object , $ context )) {
67
- if (!$ this ->serializer instanceof NormalizerInterface) {
68
- throw new LogicException ('Cannot normalize the transformed value because the injected serializer is not a normalizer ' );
69
- }
70
-
71
- $ context ['api_normalize ' ] = true ;
72
- $ context ['resource_class ' ] = $ this ->getObjectClass ($ transformed );
73
-
74
- return $ this ->serializer ->normalize ($ transformed , $ format , $ context );
75
- }
76
-
77
- if ($ this ->handleNonResource && $ context ['api_normalize ' ] ?? false ) {
78
- $ object = $ this ->transformOutput ($ object , $ context );
79
- $ context ['api_normalize ' ] = true ;
80
- $ context ['resource_class ' ] = $ this ->getObjectClass ($ object );
81
-
67
+ if ($ this ->handleNonResource || null !== $ outputClass = $ this ->getOutputClass ($ this ->getObjectClass ($ object ), $ context )) {
82
68
return parent ::normalize ($ object , $ format , $ context );
83
69
}
84
70
@@ -135,13 +121,15 @@ public function supportsDenormalization($data, $type, $format = null, array $con
135
121
136
122
/**
137
123
* {@inheritdoc}
124
+ *
125
+ * @throws NotNormalizableValueException
138
126
*/
139
127
public function denormalize ($ data , $ class , $ format = null , array $ context = [])
140
128
{
141
129
// Avoid issues with proxies if we populated the object
142
130
if (!isset ($ context [self ::OBJECT_TO_POPULATE ]) && isset ($ data ['data ' ]['id ' ])) {
143
131
if (isset ($ context ['api_allow_update ' ]) && true !== $ context ['api_allow_update ' ]) {
144
- throw new InvalidArgumentException ('Update is not allowed for this operation. ' );
132
+ throw new NotNormalizableValueException ('Update is not allowed for this operation. ' );
145
133
}
146
134
147
135
$ context [self ::OBJECT_TO_POPULATE ] = $ this ->iriConverter ->getItemFromIri (
@@ -184,6 +172,9 @@ protected function setAttributeValue($object, $attribute, $value, $format = null
184
172
* {@inheritdoc}
185
173
*
186
174
* @see http://jsonapi.org/format/#document-resource-object-linkage
175
+ *
176
+ * @throws RuntimeException
177
+ * @throws NotNormalizableValueException
187
178
*/
188
179
protected function denormalizeRelation (string $ attributeName , PropertyMetadata $ propertyMetadata , string $ className , $ value , string $ format = null , array $ context )
189
180
{
@@ -194,24 +185,26 @@ protected function denormalizeRelation(string $attributeName, PropertyMetadata $
194
185
if ($ this ->serializer instanceof DenormalizerInterface) {
195
186
return $ this ->serializer ->denormalize ($ value , $ className , $ format , $ context );
196
187
}
197
- throw new InvalidArgumentException (sprintf ('The injected serializer must be an instance of "%s". ' , DenormalizerInterface::class));
188
+ throw new RuntimeException (sprintf ('The injected serializer must be an instance of "%s". ' , DenormalizerInterface::class));
198
189
}
199
190
200
191
if (!\is_array ($ value ) || !isset ($ value ['id ' ], $ value ['type ' ])) {
201
- throw new InvalidArgumentException ('Only resource linkage supported currently, see: http://jsonapi.org/format/#document-resource-object-linkage. ' );
192
+ throw new NotNormalizableValueException ('Only resource linkage supported currently, see: http://jsonapi.org/format/#document-resource-object-linkage. ' );
202
193
}
203
194
204
195
try {
205
196
return $ this ->iriConverter ->getItemFromIri ($ value ['id ' ], $ context + ['fetch_data ' => true ]);
206
197
} catch (ItemNotFoundException $ e ) {
207
- throw new InvalidArgumentException ($ e ->getMessage (), $ e ->getCode (), $ e );
198
+ throw new RuntimeException ($ e ->getMessage (), $ e ->getCode (), $ e );
208
199
}
209
200
}
210
201
211
202
/**
212
203
* {@inheritdoc}
213
204
*
214
205
* @see http://jsonapi.org/format/#document-resource-object-linkage
206
+ *
207
+ * @throws RuntimeException
215
208
*/
216
209
protected function normalizeRelation (PropertyMetadata $ propertyMetadata , $ relatedObject , string $ resourceClass , string $ format = null , array $ context )
217
210
{
@@ -224,7 +217,7 @@ protected function normalizeRelation(PropertyMetadata $propertyMetadata, $relate
224
217
if ($ this ->serializer instanceof NormalizerInterface) {
225
218
return $ this ->serializer ->normalize ($ relatedObject , $ format , $ context );
226
219
}
227
- throw new InvalidArgumentException (sprintf ('The injected serializer must be an instance of "%s". ' , NormalizerInterface::class));
220
+ throw new RuntimeException (sprintf ('The injected serializer must be an instance of "%s". ' , NormalizerInterface::class));
228
221
}
229
222
} else {
230
223
$ iri = $ this ->iriConverter ->getIriFromItem ($ relatedObject );
@@ -236,7 +229,7 @@ protected function normalizeRelation(PropertyMetadata $propertyMetadata, $relate
236
229
$ context ['api_sub_level ' ] = true ;
237
230
238
231
if (!$ this ->serializer instanceof NormalizerInterface) {
239
- throw new InvalidArgumentException (sprintf ('The injected serializer must be an instance of "%s". ' , NormalizerInterface::class));
232
+ throw new RuntimeException (sprintf ('The injected serializer must be an instance of "%s". ' , NormalizerInterface::class));
240
233
}
241
234
$ data = $ this ->serializer ->normalize ($ relatedObject , $ format , $ context );
242
235
unset($ context ['api_sub_level ' ]);
@@ -328,7 +321,7 @@ private function getComponents($object, string $format = null, array $context)
328
321
*
329
322
* @param object $object
330
323
*
331
- * @throws InvalidArgumentException
324
+ * @throws UnexpectedValueException
332
325
*/
333
326
private function getPopulatedRelations ($ object , string $ format = null , array $ context , array $ relationships ): array
334
327
{
@@ -367,7 +360,7 @@ private function getPopulatedRelations($object, string $format = null, array $co
367
360
// Many to many relationship
368
361
foreach ($ attributeValue as $ attributeValueElement ) {
369
362
if (!isset ($ attributeValueElement ['data ' ])) {
370
- throw new InvalidArgumentException (sprintf ('The JSON API attribute \'%s \' must contain a "data" key. ' , $ relationshipName ));
363
+ throw new UnexpectedValueException (sprintf ('The JSON API attribute \'%s \' must contain a "data" key. ' , $ relationshipName ));
371
364
}
372
365
unset($ attributeValueElement ['data ' ]['attributes ' ]);
373
366
$ data [$ relationshipName ]['data ' ][] = $ attributeValueElement ['data ' ];
0 commit comments