Problem/Motivation

  /**
   * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize().
   */
  public function normalize($object, $format = NULL, array $context = array()) {
    $attributes = array();
    foreach ($object as $name => $field) {
      $attributes[$name] = $this->serializer->normalize($field, $format);
    }
    return $attributes;
  }

The child items that are iterated over do not get the context passed in. I would guess this is an artefact of just a missed parameter when the serializer component was updated to pass a $context parameter.

Proposed resolution

Add $context to the normalize() calls for each field.

Remaining tasks

Tests, Add the variablle, done

User interface changes

None

API changes

None

Data model changes

None

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug, because its a missing parameter
Issue priority Normal, because its just one special bit of the code flow.
Disruption No disruption
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

damiankloip created an issue. See original summary.

damiankloip’s picture

Title: context data is not passed to normalize() field calls in ComplexDataNormalizer » Context data is not passed to normalize() field calls in ComplexDataNormalizer
damiankloip’s picture

Status: Active » Needs review
Issue tags: -Needs tests
FileSize
3.94 KB
4.68 KB

The last submitted patch, 3: 2566335-test-only-FAIL.patch, failed testing.

The last submitted patch, 3: 2566335-test-only-FAIL.patch, failed testing.

dawehner’s picture

  1. +++ b/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php
    @@ -34,7 +34,7 @@ class ComplexDataNormalizer extends NormalizerBase {
    -      $attributes[$name] = $this->serializer->normalize($field, $format);
    +      $attributes[$name] = $this->serializer->normalize($field, $format, $context);
    

    Why is $context not always a required parameter? Odd world

  2. +++ b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php
    @@ -0,0 +1,161 @@
    +  /**
    +   * Tests the supportsNormalization() method.
    +   */
    

    Let's just go with @covers ::supportsNormalization and skip the human words

  3. +++ b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php
    @@ -0,0 +1,161 @@
    +    $this->serializer->expects($this->at(0))
    ...
    +    $this->serializer->expects($this->at(1))
    

    Do we really want to check the order of the execution of the serialization? Seems to limit our implementation a bit.

  4. +++ b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php
    @@ -0,0 +1,161 @@
    +    $complex_data = new TestComplexData(['a' => 'A', 'b' => 'B']);
    

    Ah great, pass in two values, this is not a bad idea to test.

damiankloip’s picture

1. Yeah.. I think because it was introduced and needed BC
2. Yeah! c&p fail
3. Not sure what you mean, I would think we would like to know that each item is passed in? We know what the items will be from the TestComplexData class
4. KISS

damiankloip’s picture

FileSize
4.51 KB
1.68 KB

With prophecy.

damiankloip’s picture

Ugh, and with the @covers

dawehner’s picture

  1. +++ b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php
    @@ -0,0 +1,153 @@
    +  /**
    +   * The mock serializer.
    +   *
    +   * @var \Symfony\Component\Serializer\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
    +   */
    +  protected $serializer;
    

    Not needed anymore

  2. +++ b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php
    @@ -0,0 +1,153 @@
    +    $serializer_prophecy = $this->prophesize('Symfony\Component\Serializer\Serializer');
    +
    

    Can we use Serializer::class ? its way nicer

damiankloip’s picture

FileSize
4.34 KB
1.44 KB

Thanks!

dawehner’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Thank you!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7461ba2 and pushed to 8.0.x. Thanks!

  • alexpott committed 7461ba2 on 8.0.x
    Issue #2566335 by damiankloip, dawehner: Context data is not passed to...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.