summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authore0ipso2018-02-10 07:41:18 (GMT)
committerMateu Aguiló Bosch2018-02-10 07:41:18 (GMT)
commit7719e8c6a3f12b6ff0093898119726014113109d (patch)
treeadb4cb9c82a6a4767b0347760590760b228e8205
parent5aea206677d17d6d2d30bbc8ed41d2341d250787 (diff)
Issue #2943753 by e0ipso: Fix branch tests against D8.6
-rw-r--r--src/Normalizer/JsonBlueprintDenormalizer.php49
-rw-r--r--subrequests.services.yml2
-rw-r--r--tests/src/Unit/SubrequestsManagerTest.php1
3 files changed, 42 insertions, 10 deletions
diff --git a/src/Normalizer/JsonBlueprintDenormalizer.php b/src/Normalizer/JsonBlueprintDenormalizer.php
index c6330ff..b945ba5 100644
--- a/src/Normalizer/JsonBlueprintDenormalizer.php
+++ b/src/Normalizer/JsonBlueprintDenormalizer.php
@@ -31,10 +31,30 @@ class JsonBlueprintDenormalizer implements DenormalizerInterface, SerializerAwar
*/
protected $logger;
+ /**
+ * The schema validator.
+ *
+ * This property will only be set if the validator library is available.
+ *
+ * @var \JsonSchema\Validator|null
+ */
+ protected $validator;
+
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
+ /**
+ * Sets the validator service if available.
+ */
+ public function setValidator(Validator $validator = NULL) {
+ if ($validator) {
+ $this->validator = $validator;
+ }
+ elseif (class_exists(Validator::class)) {
+ $this->validator = new Validator();
+ }
+ }
/**
* {@inheritdoc}
@@ -50,10 +70,7 @@ class JsonBlueprintDenormalizer implements DenormalizerInterface, SerializerAwar
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
- assert(
- '$this->validateInput($data)',
- 'The input blueprint failed validation (see the logs for details). Please report this in the issue queue on drupal.org'
- );
+ $this->doValidateInput($data);
$data = array_map([$this, 'fillDefaults'], $data);
$subrequests = array_map(function ($item) {
return new Subrequest($item);
@@ -138,6 +155,18 @@ class JsonBlueprintDenormalizer implements DenormalizerInterface, SerializerAwar
return $raw_item;
}
+
+ /**
+ * Wraps validation in an assert to prevent execution in production.
+ *
+ * @see self::validateInput
+ */
+ public function doValidateInput($input) {
+ if (PHP_MAJOR_VERSION >= 7 || assert_options(ASSERT_ACTIVE)) {
+ assert($this->validateInput($input), 'A Subrequests blueprint failed validation (see the logs for details). Please report this in the issue queue on drupal.org');
+ }
+ }
+
/**
* Validates a response against the JSON API specification.
*
@@ -148,26 +177,26 @@ class JsonBlueprintDenormalizer implements DenormalizerInterface, SerializerAwar
* FALSE if the input failed validation, otherwise TRUE.
*/
protected function validateInput($input) {
- if (!class_exists("\\JsonSchema\\Validator")) {
+ // If the validator isn't set, then the validation library is not installed.
+ if (!$this->validator) {
return TRUE;
}
- $validator = new Validator();
$schema_path = dirname(dirname(__DIR__)) . '/schema.json';
- $validator->check($input, (object) ['$ref' => 'file://' . $schema_path]);
+ $this->validator->check($input, (object) ['$ref' => 'file://' . $schema_path]);
- if (!$validator->isValid()) {
+ if (!$this->validator->isValid()) {
// Log any potential errors.
$this->logger->debug('Response failed validation: @data', [
'@data' => Json::encode($input),
]);
$this->logger->debug('Validation errors: @errors', [
- '@errors' => Json::encode($validator->getErrors()),
+ '@errors' => Json::encode($this->validator->getErrors()),
]);
}
- return $validator->isValid();
+ return $this->validator->isValid();
}
/**
diff --git a/subrequests.services.yml b/subrequests.services.yml
index 1c6bd7d..0185fe8 100644
--- a/subrequests.services.yml
+++ b/subrequests.services.yml
@@ -15,6 +15,8 @@ services:
arguments: ['@logger.channel.subrequests']
tags:
- { name: normalizer, priority: 0 }
+ calls:
+ - [setValidator, []]
subrequests.denormalizer.subrequest.json:
class: Drupal\subrequests\Normalizer\JsonSubrequestDenormalizer
tags:
diff --git a/tests/src/Unit/SubrequestsManagerTest.php b/tests/src/Unit/SubrequestsManagerTest.php
index 2d2912c..c48d5d5 100644
--- a/tests/src/Unit/SubrequestsManagerTest.php
+++ b/tests/src/Unit/SubrequestsManagerTest.php
@@ -89,6 +89,7 @@ class SubrequestsManagerTest extends UnitTestCase {
]);
$tree->stack([$subrequests[0]]);
$tree->stack([$subrequests[1], $subrequests[2]]);
+ $tree->setMasterRequest(new Request());
$actual = $this->sut->request($tree);
$this->assertSame('<foo>', $actual[0]->headers->get('Content-ID'));
$this->assertSame('<oop>', $actual[1]->headers->get('Content-ID'));