Skip to content

Commit dbac05e

Browse files
committed
serialization, injection of wrong enums, adopt changes for InvalidTypeException
1 parent 2a389a7 commit dbac05e

16 files changed

+99
-21
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"symfony/polyfill-php81": "^1.28",
15-
"wol-soft/php-json-schema-model-generator-production": "dev-enumPostProcessor",
15+
"wol-soft/php-json-schema-model-generator-production": "^0.19.0",
1616
"wol-soft/php-micro-template": "^1.9.0",
1717

1818
"php": ">=7.2",

src/ModelGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ public function __construct(GeneratorConfiguration $generatorConfiguration = nul
5050
->addPostProcessor(new AdditionalPropertiesPostProcessor())
5151
->addPostProcessor(new PatternPropertiesPostProcessor())
5252
->addPostProcessor(new ExtendObjectPropertiesMatchingPatternPropertiesPostProcessor());
53-
54-
if ($this->generatorConfiguration->hasSerializationEnabled()) {
55-
$this->addPostProcessor(new SerializationPostProcessor());
56-
}
5753
}
5854

5955
/**
@@ -113,6 +109,10 @@ public function generateModels(SchemaProviderInterface $schemaProvider, string $
113109
throw new FileSystemException("Destination directory '$destination' doesn't exist or is not empty");
114110
}
115111

112+
if ($this->generatorConfiguration->hasSerializationEnabled()) {
113+
$this->addPostProcessor(new SerializationPostProcessor());
114+
}
115+
116116
$renderQueue = new RenderQueue();
117117
$schemaProcessor = new SchemaProcessor(
118118
$schemaProvider->getBaseDirectory(),

src/SchemaProcessor/PostProcessor/EnumPostProcessor.php

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

77
use Exception;
88
use PHPMicroTemplate\Render;
9+
use PHPModelGenerator\Exception\Generic\InvalidTypeException;
910
use PHPModelGenerator\Exception\SchemaException;
1011
use PHPModelGenerator\Filter\TransformingFilterInterface;
1112
use PHPModelGenerator\Model\GeneratorConfiguration;
@@ -16,6 +17,7 @@
1617
use PHPModelGenerator\Model\Validator;
1718
use PHPModelGenerator\Model\Validator\EnumValidator;
1819
use PHPModelGenerator\Model\Validator\FilterValidator;
20+
use PHPModelGenerator\Model\Validator\PropertyValidator;
1921
use PHPModelGenerator\ModelGenerator;
2022
use PHPModelGenerator\PropertyProcessor\Filter\FilterProcessor;
2123
use PHPModelGenerator\Utils\ArrayHash;
@@ -124,6 +126,24 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
124126
$property->filterValidators(static function (Validator $validator): bool {
125127
return !is_a($validator->getValidator(), EnumValidator::class);
126128
});
129+
130+
// if an enum value is provided the transforming filter will add a value pass through. As the filter doesn't
131+
// know the exact enum type the pass through allows every UnitEnum instance. Consequently add a validator to
132+
// avoid wrong enums by validating against the generated enum
133+
$property->addValidator(
134+
new class ($property, $enumName) extends PropertyValidator {
135+
public function __construct(PropertyInterface $property, string $enumName)
136+
{
137+
parent::__construct(
138+
$property,
139+
sprintf('$value instanceof UnitEnum && !($value instanceof %s)', $enumName),
140+
InvalidTypeException::class,
141+
[$enumName]
142+
);
143+
}
144+
},
145+
0
146+
);
127147
}
128148
}
129149

tests/Basic/AdditionalPropertiesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function invalidTypedAdditionalPropertiesDataProvider(): array
203203
],
204204
'invalid type for additional property (object)' => [
205205
['additional1' => new stdClass(), 'additional2' => 'Hello'],
206-
sprintf($exception, 'Invalid type for additional property. Requires string, got object')
206+
sprintf($exception, 'Invalid type for additional property. Requires string, got stdClass')
207207
],
208208
'empty short string' => [
209209
['additional1' => '', 'additional2' => 'Hello'],

tests/Basic/PatternPropertiesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function testTypedPatternPropertyWithInvalidInputThrowsAnException(
3333
): void {
3434
$this->expectValidationError(
3535
$configuration,
36-
'Invalid type for pattern property. Requires string, got ' . gettype($propertyValue)
36+
'Invalid type for pattern property. Requires string, got ' .
37+
(is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue))
3738
);
3839

3940
$className = $this->generateClassFromFile('TypedPatternProperty.json', $configuration);

tests/Objects/ArrayPropertyTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ public function testInvalidPropertyTypeThrowsAnException(
234234
): void {
235235
$this->expectValidationError(
236236
$configuration,
237-
'Invalid type for property. Requires array, got ' . gettype($propertyValue)
237+
'Invalid type for property. Requires array, got ' .
238+
(is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue))
238239
);
239240

240241
$className = $this->generateClassFromFile('ArrayProperty.json', $configuration);

tests/Objects/BooleanPropertyTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public function validInputProvider(): array
6969
public function testInvalidPropertyTypeThrowsAnException($propertyValue): void
7070
{
7171
$this->expectException(ValidationException::class);
72-
$this->expectExceptionMessage('Invalid type for property. Requires bool, got ' . gettype($propertyValue));
72+
$this->expectExceptionMessage(
73+
'Invalid type for property. Requires bool, got ' .
74+
(is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue))
75+
);
7376

7477
$className = $this->generateClassFromFile('BooleanProperty.json');
7578

tests/Objects/EnumPropertyTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ public function invalidEnumEntriesDataProvider(): array
119119
public function testInvalidItemTypeThrowsAnException($propertyValue): void
120120
{
121121
$this->expectException(ValidationException::class);
122-
$this->expectExceptionMessage('Invalid type for property. Requires string, got ' . gettype($propertyValue));
122+
$this->expectExceptionMessage(
123+
'Invalid type for property. Requires string, got ' .
124+
(is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue))
125+
);
123126

124127
$className = $this->generateEnumClass('string', static::ENUM_STRING);
125128

tests/Objects/IntegerPropertyTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public function validInputProvider(): array
6969
public function testInvalidPropertyTypeThrowsAnException($propertyValue): void
7070
{
7171
$this->expectException(ValidationException::class);
72-
$this->expectExceptionMessage('Invalid type for property. Requires int, got ' . gettype($propertyValue));
72+
$this->expectExceptionMessage(
73+
'Invalid type for property. Requires int, got ' .
74+
(is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue))
75+
);
7376

7477
$className = $this->generateClassFromFile('IntegerProperty.json');
7578

tests/Objects/MultiTypePropertyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function invalidValueDataProvider(): array
129129
{
130130
return [
131131
'Bool' => [true, 'Invalid type for property. Requires [float, string, array], got boolean'],
132-
'Object' => [new stdClass(), 'Invalid type for property. Requires [float, string, array], got object'],
132+
'Object' => [new stdClass(), 'Invalid type for property. Requires [float, string, array], got stdClass'],
133133
'Invalid int' => [9, 'Value for property must not be smaller than 10'],
134134
'zero' => [0, 'Value for property must not be smaller than 10'],
135135
'Invalid float' => [9.9, 'Value for property must not be smaller than 10'],

0 commit comments

Comments
 (0)