Skip to content

Commit 562e860

Browse files
author
Enno Woortmann
committed
patch serialization of numeric only pattern properties
1 parent f742843 commit 562e860

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/SchemaProcessor/PostProcessor/Internal/SerializationPostProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function addPatternPropertiesSerialization(
157157
new class () implements SerializationHookInterface {
158158
public function getCode(): string
159159
{
160-
return '$data = array_merge($this->serializePatternProperties($depth, $except), $data);';
160+
return '$data += $this->serializePatternProperties($depth, $except);';
161161
}
162162
},
163163
);

tests/Basic/PatternPropertiesTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use PHPModelGenerator\Exception\SchemaException;
88
use PHPModelGenerator\Model\GeneratorConfiguration;
9+
use PHPModelGenerator\ModelGenerator;
10+
use PHPModelGenerator\SchemaProcessor\PostProcessor\AdditionalPropertiesAccessorPostProcessor;
11+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PatternPropertiesAccessorPostProcessor;
912
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTestCase;
1013
use stdClass;
1114

@@ -97,4 +100,22 @@ public function testPatternEscaping(): void
97100

98101
$this->assertSame(['a/b' => 'Hello'], $object->getRawModelDataInput());
99102
}
103+
104+
public function testNumericPatternProperties(): void
105+
{
106+
$this->modifyModelGenerator = static function (ModelGenerator $generator): void {
107+
$generator->addPostProcessor(new PatternPropertiesAccessorPostProcessor(),);
108+
};
109+
110+
$className = $this->generateClassFromFileTemplate(
111+
'PatternProperty.json',
112+
['^[0-9]+$'],
113+
(new GeneratorConfiguration())->setSerialization(true),
114+
);
115+
116+
$object = new $className([10 => 'Hello', '12' => 'World']);
117+
118+
$this->assertSame(['10' => 'Hello', '12' => 'World'], $object->toArray());
119+
$this->assertSame(['10' => 'Hello', '12' => 'World'], $object->getPatternProperties('^[0-9]+$'));
120+
}
100121
}

0 commit comments

Comments
 (0)