Skip to content

Commit 0620a07

Browse files
Merge branch '4.4' into 5.4
* 4.4: [PropertyInfo] CS fix [Serializer] Try all possible denormalization route with union types when ALLOW_EXTRA_ATTRIBUTES=false CS fix [Cache] Respect $save option in ChainAdapter [ExpressionLanguage] fix tests (bis) [ExpressionLanguage] fix tests [Cache] Respect $save option in ArrayAdapter [HttpKernel] Disable session tracking while collecting profiler data [MonologBridge] Fixed support of elasticsearch 7.+ in ElasticsearchLogstashHandler [DoctrineBridge] Extend type guessing on enum fields Fix for missing sender name in case with usage of the EnvelopeListener
2 parents 541a55d + e62812e commit 0620a07

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

PropertyInfo/DoctrineExtractor.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,18 @@ public function getTypes(string $class, string $property, array $context = [])
135135
}
136136

137137
if ($metadata->hasField($property)) {
138-
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
139-
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
140-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass)];
141-
}
142-
143138
$typeOfField = $metadata->getTypeOfField($property);
144139

145140
if (!$builtinType = $this->getPhpType($typeOfField)) {
146141
return null;
147142
}
148143

144+
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
145+
$enumType = null;
146+
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
147+
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
148+
}
149+
149150
switch ($builtinType) {
150151
case Type::BUILTIN_TYPE_OBJECT:
151152
switch ($typeOfField) {
@@ -171,11 +172,22 @@ public function getTypes(string $class, string $property, array $context = [])
171172
switch ($typeOfField) {
172173
case Types::ARRAY:
173174
case 'json_array':
175+
// return null if $enumType is set, because we can't determine if collectionKeyType is string or int
176+
if ($enumType) {
177+
return null;
178+
}
179+
174180
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
175181

176182
case Types::SIMPLE_ARRAY:
177183
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
178184
}
185+
case Type::BUILTIN_TYPE_INT:
186+
case Type::BUILTIN_TYPE_STRING:
187+
if ($enumType) {
188+
return [$enumType];
189+
}
190+
break;
179191
}
180192

181193
return [new Type($builtinType, $nullable)];

Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public function testExtractEnum()
138138
}
139139
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumString::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumString', []));
140140
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumInt', []));
141+
$this->assertEquals(null, $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumStringArray', []));
142+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class))], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumIntArray', []));
143+
$this->assertEquals(null, $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumCustom', []));
141144
}
142145

143146
public function typesProvider()

Tests/PropertyInfo/Fixtures/DoctrineEnum.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@ class DoctrineEnum
3535
* @Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
3636
*/
3737
protected $enumInt;
38+
39+
/**
40+
* @Column(type="array", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString")
41+
*/
42+
protected $enumStringArray;
43+
44+
/**
45+
* @Column(type="simple_array", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
46+
*/
47+
protected $enumIntArray;
48+
49+
/**
50+
* @Column(type="custom_foo", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
51+
*/
52+
protected $enumCustom;
3853
}

0 commit comments

Comments
 (0)