diff --git a/src/Serializer/StructuralElementNormalizer.php b/src/Serializer/StructuralElementNormalizer.php index 7ce774f9..bf3e1097 100644 --- a/src/Serializer/StructuralElementNormalizer.php +++ b/src/Serializer/StructuralElementNormalizer.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace App\Serializer; use App\Entity\Base\AbstractStructuralDBElement; +use App\Serializer\APIPlatform\SkippableItemNormalizer; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; @@ -36,6 +37,8 @@ class StructuralElementNormalizer implements NormalizerInterface, NormalizerAwar { use NormalizerAwareTrait; + public const ALREADY_CALLED = 'STRUCTURAL_ELEMENT_NORMALIZER_ALREADY_CALLED'; + public function supportsNormalization($data, ?string $format = null, array $context = []): bool { //Only normalize if we are doing a file export operation @@ -43,15 +46,25 @@ class StructuralElementNormalizer implements NormalizerInterface, NormalizerAwar return false; } + if (isset($context[self::ALREADY_CALLED]) && in_array($data, $context[self::ALREADY_CALLED], true)) { + //If we already handled this object, skip it + return false; + } + return $data instanceof AbstractStructuralDBElement; } - public function normalize($object, ?string $format = null, array $context = []): \ArrayObject|bool|float|int|string + public function normalize($object, ?string $format = null, array $context = []): \ArrayObject|bool|float|int|string|array { if (!$object instanceof AbstractStructuralDBElement) { throw new \InvalidArgumentException('This normalizer only supports AbstractStructural objects!'); } + //Avoid infinite recursion by checking if we already handled this object + $context[self::ALREADY_CALLED] = $context[self::ALREADY_CALLED] ?? []; + $context[SkippableItemNormalizer::DISABLE_ITEM_NORMALIZER] = true; + $context[self::ALREADY_CALLED][] = $object; + $data = $this->normalizer->normalize($object, $format, $context); //If the data is not an array, we can't do anything with it @@ -75,7 +88,8 @@ class StructuralElementNormalizer implements NormalizerInterface, NormalizerAwar public function getSupportedTypes(?string $format): array { return [ - AbstractStructuralDBElement::class => true, + //We cannot cache the result, as it depends on the context + AbstractStructuralDBElement::class => false, ]; } } diff --git a/src/Services/InfoProviderSystem/Providers/Element14Provider.php b/src/Services/InfoProviderSystem/Providers/Element14Provider.php index de51de46..91249156 100644 --- a/src/Services/InfoProviderSystem/Providers/Element14Provider.php +++ b/src/Services/InfoProviderSystem/Providers/Element14Provider.php @@ -46,7 +46,7 @@ class Element14Provider implements InfoProviderInterface 'rohsPhthalatesCompliant', 'SVHC', 'tariffCode', 'usEccn', 'hazardCode']; private readonly HttpClientInterface $element14Client; - + public function __construct(HttpClientInterface $element14Client, private readonly Element14Settings $settings) { /* We use the mozilla CA from the composer ca bundle directly, as some debian systems seems to have problems @@ -77,7 +77,7 @@ class Element14Provider implements InfoProviderInterface public function isActive(): bool { - return trim($this->settings->apiKey) !== ''; + return $this->settings->apiKey !== null && trim($this->settings->apiKey) !== ''; } /** @@ -308,4 +308,4 @@ class Element14Provider implements InfoProviderInterface ProviderCapabilities::DATASHEET, ]; } -} \ No newline at end of file +} diff --git a/src/Services/InfoProviderSystem/Providers/TMEClient.php b/src/Services/InfoProviderSystem/Providers/TMEClient.php index df198717..ae2ab0d1 100644 --- a/src/Services/InfoProviderSystem/Providers/TMEClient.php +++ b/src/Services/InfoProviderSystem/Providers/TMEClient.php @@ -48,7 +48,7 @@ class TMEClient public function isUsable(): bool { - return !($this->settings->apiToken === '' || $this->settings->apiSecret === ''); + return !($this->settings->apiToken === null || $this->settings->apiSecret === null); } /** @@ -59,7 +59,7 @@ class TMEClient public function isUsingPrivateToken(): bool { //Private tokens are longer than anonymous ones (50 instead of 45 characters) - return strlen($this->settings->apiToken) > 45; + return strlen($this->settings->apiToken ?? '') > 45; } /** @@ -94,4 +94,4 @@ class TMEClient return $params; } -} \ No newline at end of file +} diff --git a/tests/Serializer/StructuralElementNormalizerTest.php b/tests/Serializer/StructuralElementNormalizerTest.php index b151f249..79f739fa 100644 --- a/tests/Serializer/StructuralElementNormalizerTest.php +++ b/tests/Serializer/StructuralElementNormalizerTest.php @@ -41,6 +41,9 @@ class StructuralElementNormalizerTest extends WebTestCase //Get an service instance. self::bootKernel(); $this->service = self::getContainer()->get(StructuralElementNormalizer::class); + //Inject the serializer, as the normalizer as this is not handled by the DI container + $this->service->setNormalizer(self::getContainer()->get('serializer')); + } public function testNormalize(): void diff --git a/tests/Validator/Constraints/ValidGoogleAuthCodeValidatorTest.php b/tests/Validator/Constraints/ValidGoogleAuthCodeValidatorTest.php index a1bc1a74..6eb9270e 100644 --- a/tests/Validator/Constraints/ValidGoogleAuthCodeValidatorTest.php +++ b/tests/Validator/Constraints/ValidGoogleAuthCodeValidatorTest.php @@ -87,7 +87,7 @@ class ValidGoogleAuthCodeValidatorTest extends ConstraintValidatorTestCase return []; } - public function eraseCredentials() + public function eraseCredentials(): void { }