Skip to content

Commit 1294d21

Browse files
committed
Fix pattern properties with forward slashes (used as delimiter) by adding slash escaping (#65)
1 parent a57dee9 commit 1294d21

File tree

7 files changed

+26
-10
lines changed

7 files changed

+26
-10
lines changed

src/Model/Validator/PatternPropertiesValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(
6464
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'PatternProperties.phptpl',
6565
[
6666
'patternHash' => $this->key,
67-
'pattern' => "/{$this->pattern}/",
67+
'pattern' => base64_encode('/' . addcslashes($this->pattern, '/') . '/'),
6868
'validationProperty' => $this->validationProperty,
6969
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
7070
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ protected function addPatternPropertiesValidator(JsonSchema $propertySchema): vo
168168
}
169169

170170
foreach ($json['patternProperties'] as $pattern => $schema) {
171-
if (@preg_match("/$pattern/", '') === false) {
171+
$escapedPattern = addcslashes($pattern, '/');
172+
173+
if (@preg_match("/$escapedPattern/", '') === false) {
172174
throw new SchemaException(
173175
"Invalid pattern '$pattern' for pattern property in file {$propertySchema->getFile()}"
174176
);

src/SchemaProcessor/PostProcessor/Internal/ExtendObjectPropertiesMatchingPatternPropertiesPostProcessor.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ public function getCode(PropertyInterface $property, bool $batchUpdate = false):
4343
{
4444
$json = $this->schema->getJsonSchema()->getJson();
4545
// A batch update must execute the base validators to check the integrity of the object.
46-
// Consequently the schema hook must not add validation code in that places.
46+
// Consequently, the schema hook must not add validation code in that places.
4747
if ($batchUpdate || !isset($json['patternProperties'])) {
4848
return '';
4949
}
5050

5151
$matchesAnyPattern = false;
5252

5353
foreach (array_keys($json['patternProperties']) as $pattern) {
54-
if (preg_match("/{$pattern}/", $property->getName())) {
54+
if (preg_match('/' . addcslashes($pattern, '/') . '/', $property->getName())) {
5555
$matchesAnyPattern = true;
5656
}
5757
}
@@ -107,8 +107,11 @@ function (Validator $validator): bool {
107107

108108
/** @var PatternPropertiesValidator $patternPropertiesValidator */
109109
foreach ($patternPropertiesValidators as $patternPropertiesValidator) {
110-
if (!preg_match("/{$patternPropertiesValidator->getPattern()}/", $property->getName()) ||
111-
!isset(
110+
if (!preg_match(
111+
'/' . addcslashes($patternPropertiesValidator->getPattern(), '/') . '/',
112+
$property->getName()
113+
)
114+
|| !isset(
112115
$schema->getJsonSchema()->getJson()
113116
['patternProperties']
114117
[$patternPropertiesValidator->getPattern()]

src/SchemaProcessor/PostProcessor/Internal/PatternPropertiesPostProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
5353
$schema->getProperties(),
5454
function (array $carry, PropertyInterface $property) use ($schemaProperties, $validator): array {
5555
if (in_array($property->getName(), $schemaProperties) &&
56-
preg_match("/{$validator->getPattern()}/", $property->getName())
56+
preg_match('/' . addcslashes($validator->getPattern(), '/') . '/', $property->getName())
5757
) {
5858
$carry[] = $property;
5959
}

src/Templates/Validator/PatternProperties.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
foreach ($properties as $propertyKey => $value) {
99
try {
10-
if (!preg_match("{{ pattern }}", $propertyKey)) {
10+
if (!preg_match(base64_decode('{{ pattern }}'), $propertyKey)) {
1111
continue;
1212
}
1313

tests/Basic/PatternPropertiesTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testInvalidPatternThrowsAnException(): void
2121
$this->expectException(SchemaException::class);
2222
$this->expectExceptionMessageMatches("/Invalid pattern 'ab\[c' for pattern property in file .*\.json/");
2323

24-
$this->generateClassFromFile('InvalidPattern.json');
24+
$this->generateClassFromFileTemplate('PatternProperty.json', ['ab[c']);
2525
}
2626

2727
/**
@@ -85,4 +85,15 @@ public function testMultipleTransformingFiltersForPatternPropertiesAndObjectProp
8585

8686
$this->generateClassFromFile('MultipleTransformingFilters.json');
8787
}
88+
89+
/**
90+
* https://github.com/wol-soft/php-json-schema-model-generator/issues/65
91+
*/
92+
public function testPatternEscaping(): void
93+
{
94+
$className = $this->generateClassFromFileTemplate('PatternProperty.json', ['a/(b|c)']);
95+
$object = new $className(['a/b' => 'Hello']);
96+
97+
$this->assertSame(['a/b' => 'Hello'], $object->getRawModelDataInput());
98+
}
8899
}

tests/Schema/PatternPropertiesTest/InvalidPattern.json renamed to tests/Schema/PatternPropertiesTest/PatternProperty.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "object",
33
"patternProperties": {
4-
"ab[c": {
4+
"%s": {
55
"type": "string"
66
}
77
}

0 commit comments

Comments
 (0)