Skip to content

Commit ed44453

Browse files
committed
Escaping of regular expressions (fixes #40, closes #42)
1 parent 7a38046 commit ed44453

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[![Latest Version](https://img.shields.io/packagist/v/wol-soft/php-json-schema-model-generator.svg)](https://packagist.org/packages/wol-soft/php-json-schema-model-generator)
22
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/)
33
[![Maintainability](https://api.codeclimate.com/v1/badges/7eb29e7366dc3d6a5f44/maintainability)](https://codeclimate.com/github/wol-soft/php-json-schema-model-generator/maintainability)
4-
[![Test Coverage](https://api.codeclimate.com/v1/badges/7eb29e7366dc3d6a5f44/test_coverage)](https://codeclimate.com/github/wol-soft/php-json-schema-model-generator/test_coverage)
54
[![Build Status](https://github.com/wol-soft/php-json-schema-model-generator/actions/workflows/main.yml/badge.svg)](https://github.com/wol-soft/php-json-schema-model-generator/actions/workflows/main.yml)
65
[![Coverage Status](https://coveralls.io/repos/github/wol-soft/php-json-schema-model-generator/badge.svg?branch=master)](https://coveralls.io/github/wol-soft/php-json-schema-model-generator?branch=master)
76
[![MIT License](https://img.shields.io/packagist/l/wol-soft/php-json-schema-model-generator.svg)](https://github.com/wol-soft/php-json-schema-model-generator/blob/master/LICENSE)

docs/source/examples/scenarioBasedTesting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ Finally we've written our first scenario with a few entities. Now let's write a
353353
$pet = self::$scenario->getPet('doggie');
354354
355355
// execute the API request
356-
$response = $this->request('GET', "/pet/{$pet->getId}");
356+
$response = $this->request('GET', "/pet/{$pet->getId()}");
357357
358358
// execute assertions
359359
$this->assertSame('doggie', $response->getBody()['name']);

src/PropertyProcessor/Property/StringProcessor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ protected function addPatternValidator(PropertyInterface $property, JsonSchema $
5858
return;
5959
}
6060

61-
if (@preg_match("/{$json[static::JSON_FIELD_PATTERN]}/", '') === false) {
61+
$escapedPattern = addcslashes(str_replace('\\\\', '\\\\\\\\', $json[static::JSON_FIELD_PATTERN]), '/');
62+
63+
if (@preg_match("/$escapedPattern/", '') === false) {
6264
throw new SchemaException(
6365
sprintf(
6466
"Invalid pattern '%s' for property '%s' in file %s",
@@ -69,12 +71,12 @@ protected function addPatternValidator(PropertyInterface $property, JsonSchema $
6971
);
7072
}
7173

72-
$json[static::JSON_FIELD_PATTERN] = addcslashes($json[static::JSON_FIELD_PATTERN], "'");
74+
$encodedPattern = base64_encode("/$escapedPattern/");
7375

7476
$property->addValidator(
7577
new PropertyValidator(
7678
$property,
77-
$this->getTypeCheck() . "!preg_match('/{$json[static::JSON_FIELD_PATTERN]}/', \$value)",
79+
$this->getTypeCheck() . "!preg_match(base64_decode('$encodedPattern'), \$value)",
7880
PatternException::class,
7981
[$json[static::JSON_FIELD_PATTERN]]
8082
)

tests/Objects/StringPropertyTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function testPatternMatchingStringIsValid(
189189
string $pattern,
190190
?string $propertyValue
191191
): void {
192-
$className = $this->generateClassFromFileTemplate('StringPropertyPattern.json', [$pattern], $configuration);
192+
$className = $this->generateClassFromFileTemplate('StringPropertyPattern.json', [$pattern], $configuration, false);
193193

194194
$object = new $className(['property' => $propertyValue]);
195195
$this->assertSame($propertyValue, $object->getProperty());
@@ -210,12 +210,13 @@ public function validPatternProvider(): array
210210
[
211211
'Null' => ['^The', null],
212212
'String starts with' => ['^The', 'The Test starts with The'],
213-
'No spaces in string' => ['^[^\\s]+$', 'ThisStringContainsNoSpace'],
213+
'No spaces in string' => ['^[^\\\\s]+$', 'ThisStringContainsNoSpace'],
214214
'A formatted date' => ['^[0-9]{4}-[0-9]{2}-[0-9]{2}$', '2018-12-12'],
215215
'A formatted date inside a text' => [
216216
'[0-9]{4}-[0-9]{2}-[0-9]{2}',
217217
'Contains a Date 2018-12-12 and something else'
218218
],
219+
'Regex escape test' => ['^\\\\\\\\/\'$', '\\\\/\''],
219220
]
220221
);
221222
}

0 commit comments

Comments
 (0)