@@ -226,36 +226,45 @@ public function denormalize($data, $class, $format = null, array $context = [])
226
226
$ normalizedData = is_scalar ($ data ) ? [$ data ] : $ this ->prepareForDenormalization ($ data );
227
227
$ class = $ this ->getClassDiscriminatorResolvedClass ($ normalizedData , $ class );
228
228
}
229
+
229
230
$ resourceClass = $ this ->resourceClassResolver ->getResourceClass ($ objectToPopulate , $ class );
230
231
$ context ['api_denormalize ' ] = true ;
231
232
$ context ['resource_class ' ] = $ resourceClass ;
232
233
233
- if (null !== ($ inputClass = $ this ->getInputClass ($ resourceClass , $ context )) && null !== ($ dataTransformer = $ this ->getDataTransformer ($ data , $ resourceClass , $ context ))) {
234
- $ dataTransformerContext = $ context ;
234
+ if (null !== $ inputClass = $ this ->getInputClass ($ resourceClass , $ context )) {
235
+ if (null !== $ dataTransformer = $ this ->getDataTransformer ($ data , $ resourceClass , $ context )) {
236
+ $ dataTransformerContext = $ context ;
235
237
236
- unset($ context ['input ' ]);
237
- unset($ context ['resource_class ' ]);
238
+ unset($ context ['input ' ]);
239
+ unset($ context ['resource_class ' ]);
238
240
239
- if (!$ this ->serializer instanceof DenormalizerInterface) {
240
- throw new LogicException ('Cannot denormalize the input because the injected serializer is not a denormalizer ' );
241
- }
241
+ if (!$ this ->serializer instanceof DenormalizerInterface) {
242
+ throw new LogicException ('Cannot denormalize the input because the injected serializer is not a denormalizer ' );
243
+ }
242
244
243
- if ($ dataTransformer instanceof DataTransformerInitializerInterface) {
244
- $ context [AbstractObjectNormalizer::OBJECT_TO_POPULATE ] = $ dataTransformer ->initialize ($ inputClass , $ context );
245
- $ context [AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE ] = true ;
246
- }
245
+ if ($ dataTransformer instanceof DataTransformerInitializerInterface) {
246
+ $ context [AbstractObjectNormalizer::OBJECT_TO_POPULATE ] = $ dataTransformer ->initialize ($ inputClass , $ context );
247
+ $ context [AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE ] = true ;
248
+ }
247
249
248
- try {
249
- $ denormalizedInput = $ this ->serializer ->denormalize ($ data , $ inputClass , $ format , $ context );
250
- } catch (NotNormalizableValueException $ e ) {
251
- throw new UnexpectedValueException ('The input data is misformatted. ' , $ e ->getCode (), $ e );
252
- }
250
+ try {
251
+ $ denormalizedInput = $ this ->serializer ->denormalize ($ data , $ inputClass , $ format , $ context );
252
+ } catch (NotNormalizableValueException $ e ) {
253
+ throw new UnexpectedValueException ('The input data is misformatted. ' , $ e ->getCode (), $ e );
254
+ }
255
+
256
+ if (!\is_object ($ denormalizedInput )) {
257
+ throw new UnexpectedValueException ('Expected denormalized input to be an object. ' );
258
+ }
253
259
254
- if (!\is_object ($ denormalizedInput )) {
255
- throw new UnexpectedValueException ('Expected denormalized input to be an object. ' );
260
+ return $ dataTransformer ->transform ($ denormalizedInput , $ resourceClass , $ dataTransformerContext );
256
261
}
257
262
258
- return $ dataTransformer ->transform ($ denormalizedInput , $ resourceClass , $ dataTransformerContext );
263
+ // Are we in a Request context?
264
+ if ($ context ['operation ' ] ?? $ context ['operation_type ' ] ?? false ) {
265
+ $ resourceClass = $ inputClass ;
266
+ $ context ['resource_class ' ] = $ inputClass ;
267
+ }
259
268
}
260
269
261
270
$ supportsPlainIdentifiers = $ this ->supportsPlainIdentifiers ();
@@ -290,6 +299,10 @@ public function denormalize($data, $class, $format = null, array $context = [])
290
299
$ previousObject = null !== $ objectToPopulate ? clone $ objectToPopulate : null ;
291
300
$ object = parent ::denormalize ($ data , $ resourceClass , $ format , $ context );
292
301
302
+ if (!$ this ->resourceClassResolver ->isResourceClass ($ context ['resource_class ' ])) {
303
+ return $ object ;
304
+ }
305
+
293
306
// Revert attributes that aren't allowed to be changed after a post-denormalize check
294
307
foreach (array_keys ($ data ) as $ attribute ) {
295
308
if (!$ this ->canAccessAttributePostDenormalize ($ object , $ previousObject , $ attribute , $ context )) {
@@ -414,6 +427,10 @@ protected function extractAttributes($object, $format = null, array $context = [
414
427
*/
415
428
protected function getAllowedAttributes ($ classOrObject , array $ context , $ attributesAsString = false )
416
429
{
430
+ if (!$ this ->resourceClassResolver ->isResourceClass ($ context ['resource_class ' ])) {
431
+ return parent ::getAllowedAttributes ($ classOrObject , $ context , $ attributesAsString );
432
+ }
433
+
417
434
$ options = $ this ->getFactoryOptions ($ context );
418
435
$ propertyNames = $ this ->propertyNameCollectionFactory ->create ($ context ['resource_class ' ], $ options );
419
436
@@ -456,6 +473,10 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
456
473
*/
457
474
protected function canAccessAttribute ($ object , string $ attribute , array $ context = []): bool
458
475
{
476
+ if (!$ this ->resourceClassResolver ->isResourceClass ($ context ['resource_class ' ])) {
477
+ return true ;
478
+ }
479
+
459
480
$ options = $ this ->getFactoryOptions ($ context );
460
481
/** @var PropertyMetadata|ApiProperty */
461
482
$ propertyMetadata = $ this ->propertyMetadataFactory ->create ($ context ['resource_class ' ], $ attribute , $ options );
@@ -857,6 +878,10 @@ protected function transformOutput($object, array $context = [], string $outputC
857
878
858
879
private function createAttributeValue ($ attribute , $ value , $ format = null , array $ context = [])
859
880
{
881
+ if (!$ this ->resourceClassResolver ->isResourceClass ($ context ['resource_class ' ])) {
882
+ return $ value ;
883
+ }
884
+
860
885
/** @var ApiProperty|PropertyMetadata */
861
886
$ propertyMetadata = $ this ->propertyMetadataFactory ->create ($ context ['resource_class ' ], $ attribute , $ this ->getFactoryOptions ($ context ));
862
887
$ type = $ propertyMetadata instanceof PropertyMetadata ? $ propertyMetadata ->getType () : ($ propertyMetadata ->getBuiltinTypes ()[0 ] ?? null );
0 commit comments