Unverified Commit a07be20d authored by Eli-T's avatar Eli-T Committed by e0ipso
Browse files

Issue #3048493 by Eli-T, e0ipso: JSON data types other than string can't be...

Issue #3048493 by Eli-T, e0ipso: JSON data types other than string can't be replaced with replacement tokens
parent 6c04700b
......@@ -6,7 +6,7 @@
"galbar/jsonpath": "^1.0"
},
"require-dev": {
"justinrainbow/json-schema": "^4.1"
"justinrainbow/json-schema": "^5.2"
},
"license": "GPL-2.0+",
"authors": [
......
......@@ -142,6 +142,13 @@ class JsonPathReplacer {
*/
protected function replaceTokenSubject($token, $value, $token_subject) {
// Escape regular expression.
if (is_int($value) || is_float($value) || is_bool($value)) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
$regexp = sprintf('/%s/', preg_quote("\"$token\""), '/');
$token_subject = preg_replace($regexp, $value, $token_subject);
}
$regexp = sprintf('/%s/', preg_quote($token), '/');
return preg_replace($regexp, $value, $token_subject);
}
......@@ -377,7 +384,7 @@ class JsonPathReplacer {
protected function validateJsonPathReplacements($to_replace) {
$is_valid = is_array($to_replace)
&& array_reduce($to_replace, function ($valid, $replacement) {
return $valid && (is_string($replacement) || is_int($replacement));
return $valid && (is_string($replacement) || is_int($replacement) || is_bool($replacement) || is_float($replacement));
}, TRUE);
if (!$is_valid) {
throw new BadRequestHttpException(sprintf(
......
......@@ -184,7 +184,7 @@ class JsonBlueprintDenormalizer implements DenormalizerInterface, SerializerAwar
$schema_path = dirname(dirname(__DIR__)) . '/schema.json';
$this->validator->check($input, (object) ['$ref' => 'file://' . $schema_path]);
$this->validator->validate($input, (object) ['$ref' => 'file://' . $schema_path]);
if (!$this->validator->isValid()) {
// Log any potential errors.
......
......@@ -113,4 +113,38 @@ class JsonPathReplacerTest extends UnitTestCase {
$this->assertEquals($expected_paths, $paths);
}
/**
* @covers ::replaceBatch
*/
public function testReplaceBatchTypes() {
$batch = $responses = [];
$batch[] = new Subrequest([
'uri' => '/test/types',
'action' => 'create',
'requestId' => 'xyz',
'headers' => [],
'_resolved' => FALSE,
'body' => [
'You are number' => '{{foo.body@$.Number}}',
'Where am I' => '{{foo.body@$.Location}}',
'World of number two' => '{{foo.body@$.Two}}',
'Question' => 'Who is number {{foo.body@$.Who}}?',
'Michael' => '{{foo.body@$.Feigenbaum}}',
],
'waitFor' => ['foo'],
]);
$response = Response::create('{"Number":6, "Location":"In the village", "Two":false, "Who":1, "Feigenbaum":4.6692}');
$response->headers->set('Content-ID', '<foo>');
$responses[] = $response;
$actual = $this->sut->replaceBatch($batch, $responses);
$this->assertInternalType('int', $actual[0]->body['You are number']);
$this->assertInternalType('string', $actual[0]->body['Where am I']);
$this->assertInternalType('boolean', $actual[0]->body['World of number two']);
$this->assertInternalType('string', $actual[0]->body['Question']);
$this->assertInternalType('float', $actual[0]->body['Michael']);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment