Skip to content

Commit

Permalink
Fix pattern properties with forward slashes (used as delimiter) by ad…
Browse files Browse the repository at this point in the history
…ding slash escaping (#65)
  • Loading branch information
wol-soft committed Jan 26, 2023
1 parent a57dee9 commit 1294d21
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Model/Validator/PatternPropertiesValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'PatternProperties.phptpl',
[
'patternHash' => $this->key,
'pattern' => "/{$this->pattern}/",
'pattern' => base64_encode('/' . addcslashes($this->pattern, '/') . '/'),
'validationProperty' => $this->validationProperty,
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
Expand Down
4 changes: 3 additions & 1 deletion src/PropertyProcessor/Property/BaseProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ protected function addPatternPropertiesValidator(JsonSchema $propertySchema): vo
}

foreach ($json['patternProperties'] as $pattern => $schema) {
if (@preg_match("/$pattern/", '') === false) {
$escapedPattern = addcslashes($pattern, '/');

if (@preg_match("/$escapedPattern/", '') === false) {
throw new SchemaException(
"Invalid pattern '$pattern' for pattern property in file {$propertySchema->getFile()}"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public function getCode(PropertyInterface $property, bool $batchUpdate = false):
{
$json = $this->schema->getJsonSchema()->getJson();
// A batch update must execute the base validators to check the integrity of the object.
// Consequently the schema hook must not add validation code in that places.
// Consequently, the schema hook must not add validation code in that places.
if ($batchUpdate || !isset($json['patternProperties'])) {
return '';
}

$matchesAnyPattern = false;

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

/** @var PatternPropertiesValidator $patternPropertiesValidator */
foreach ($patternPropertiesValidators as $patternPropertiesValidator) {
if (!preg_match("/{$patternPropertiesValidator->getPattern()}/", $property->getName()) ||
!isset(
if (!preg_match(
'/' . addcslashes($patternPropertiesValidator->getPattern(), '/') . '/',
$property->getName()
)
|| !isset(
$schema->getJsonSchema()->getJson()
['patternProperties']
[$patternPropertiesValidator->getPattern()]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
$schema->getProperties(),
function (array $carry, PropertyInterface $property) use ($schemaProperties, $validator): array {
if (in_array($property->getName(), $schemaProperties) &&
preg_match("/{$validator->getPattern()}/", $property->getName())
preg_match('/' . addcslashes($validator->getPattern(), '/') . '/', $property->getName())
) {
$carry[] = $property;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Validator/PatternProperties.phptpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

foreach ($properties as $propertyKey => $value) {
try {
if (!preg_match("{{ pattern }}", $propertyKey)) {
if (!preg_match(base64_decode('{{ pattern }}'), $propertyKey)) {
continue;
}

Expand Down
13 changes: 12 additions & 1 deletion tests/Basic/PatternPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testInvalidPatternThrowsAnException(): void
$this->expectException(SchemaException::class);
$this->expectExceptionMessageMatches("/Invalid pattern 'ab\[c' for pattern property in file .*\.json/");

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

/**
Expand Down Expand Up @@ -85,4 +85,15 @@ public function testMultipleTransformingFiltersForPatternPropertiesAndObjectProp

$this->generateClassFromFile('MultipleTransformingFilters.json');
}

/**
* https://github.com/wol-soft/php-json-schema-model-generator/issues/65
*/
public function testPatternEscaping(): void
{
$className = $this->generateClassFromFileTemplate('PatternProperty.json', ['a/(b|c)']);
$object = new $className(['a/b' => 'Hello']);

$this->assertSame(['a/b' => 'Hello'], $object->getRawModelDataInput());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "object",
"patternProperties": {
"ab[c": {
"%s": {
"type": "string"
}
}
Expand Down

0 comments on commit 1294d21

Please sign in to comment.