When this module enabled and trying to serialize \ normalize data from code without routing (CLI (e.g. drush cron)) its end with fatal error.

The problem is here:

  /**
   * {@inheritdoc}
   */
  public function supportsNormalization($data, $format = NULL) {
    $supported = parent::supportsNormalization($data, $format);
    if ($supported) {
      $route = $this->routeMatch->getRouteObject();
      return $route->hasRequirement('_cart_api');
    }
    return $supported;
  }

The $route can be NULL and there is no condition for this.

Full backtrace:

Error: Call to a member function hasRequirement() on null in /var/www/html/web/modules/contrib/commerce_cart_api/src/Normalizer/FieldItemNormalizer.php on line 43 #0 /var/www/html/vendor/symfony/serializer/Serializer.php(242): Drupal\commerce_cart_api\Normalizer\FieldItemNormalizer->supportsNormalization(Object(Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem), 'json', Array)
#1 /var/www/html/vendor/symfony/serializer/Serializer.php(142): Symfony\Component\Serializer\Serializer->getNormalizer(Object(Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem), 'json', Array)
#2 /var/www/html/web/core/modules/serialization/src/Normalizer/ListNormalizer.php(29): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem), 'json', Array)
#3 /var/www/html/web/modules/custom/svzar/src/Normalizer/Rest/FieldNormalizer.php(51): Drupal\serialization\Normalizer\ListNormalizer->normalize(Object(Drupal\Core\Field\FieldItemList), 'json', Array)
#4 /var/www/html/vendor/symfony/serializer/Serializer.php(143): Drupal\svzar\Normalizer\Rest\FieldNormalizer->normalize(Object(Drupal\Core\Field\FieldItemList), 'json', Array)
#5 /var/www/html/web/core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php(30): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\Core\Field\FieldItemList), 'json', Array)
#6 /var/www/html/web/modules/custom/svzar_commerce/src/ProductVariation/Normalizer/VariationNormalizer.php(57): Drupal\serialization\Normalizer\ContentEntityNormalizer->normalize(Object(Drupal\commerce_product\Entity\ProductVariation), 'json', Array)
#7 /var/www/html/vendor/symfony/serializer/Serializer.php(143): Drupal\svzar_commerce\ProductVariation\Normalizer\VariationNormalizer->normalize(Object(Drupal\commerce_product\Entity\ProductVariation), 'json', Array)
#8 /var/www/html/vendor/symfony/serializer/Serializer.php(116): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\commerce_product\Entity\ProductVariation), 'json', Array)
#9 /var/www/html/web/modules/custom/svzar_commerce/src/ProductVariation/ProductVariationSpecAggregator.php(40): Symfony\Component\Serializer\Serializer->serialize(Object(Drupal\commerce_product\Entity\ProductVariation), 'json')
#10 /var/www/html/normalizer_test.php(10): Drupal\svzar_commerce\ProductVariation\ProductVariationSpecAggregator->getAll(Object(Drupal\commerce_product\Entity\ProductVariation))
#11 /var/www/html/vendor/drush/drush/src/Commands/core/PhpCommands.php(112): include('/var/www/html/n...')
#12 [internal function]: Drush\Commands\core\PhpCommands->script(Array, Array)
#13 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(257): call_user_func_array(Array, Array)
#14 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback(Array, Object(Consolidation\AnnotatedCommand\CommandData))
#15 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(176): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#16 /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(302): Consolidation\AnnotatedCommand\CommandProcessor->process(Object(Symfony\Component\Console\Output\ConsoleOutput), Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#17 /var/www/html/vendor/symfony/console/Command/Command.php(255): Consolidation\AnnotatedCommand\AnnotatedCommand->execute(Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /var/www/html/vendor/symfony/console/Application.php(1000): Symfony\Component\Console\Command\Command->run(Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /var/www/html/vendor/symfony/console/Application.php(255): Symfony\Component\Console\Application->doRunCommand(Object(Consolidation\AnnotatedCommand\AnnotatedCommand), Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 /var/www/html/vendor/symfony/console/Application.php(148): Symfony\Component\Console\Application->doRun(Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(118): Symfony\Component\Console\Application->run(Object(Drush\Symfony\DrushArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(49): Drush\Runtime\Runtime->doRun(Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 /var/www/html/vendor/drush/drush/drush.php(72): Drush\Runtime\Runtime->run(Array)
#24 /var/www/html/vendor/drush/drush/includes/preflight.inc(18): require('/var/www/html/v...')
#25 phar:///usr/local/bin/drush/bin/drush.php(141): drush_main()
#26 /usr/local/bin/drush(10): require('phar:///usr/loc...')
#27 {main}
Error: Call to a member function hasRequirement() on null in Drupal\commerce_cart_api\Normalizer\FieldItemNormalizer->supportsNormalization() (line 43 of /var/www/html/web/modules/contrib/commerce_cart_api/src/Normalizer/FieldItemNormalizer.php).
CommentFileSizeAuthor
#3 3116623-3.patch1.58 KBgoz
#2 3116623-2.patch1.11 KBniklan

Comments

Niklan created an issue. See original summary.

niklan’s picture

Status: Active » Needs review
StatusFileSize
new1.11 KB

Here is the patch fixes this issue.

goz’s picture

StatusFileSize
new1.58 KB

I'm not a big fan to this return based on $route condition.
We should let return default value ($supposed) in case route is null.
See attached patch

bachbach’s picture

i can confirme that patch #3 resolved the issue
i had this issue on the context of migrating contents from D7 to D8
thanks

  • jsacksick committed deb242c on 8.x-1.x
    Issue #3116623 by Niklan, GoZ, jsacksick: Error: Fix Call to a member...
jsacksick’s picture

Status: Needs review » Fixed

I committed a slightly different patch (similar to what already exists in the entity reference normalizer). Thanks everyone!

Status: Fixed » Closed (fixed)

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