Skip to content

Commit e174ef7

Browse files
authored
Ensure Context can be serialized (#1789)
1 parent f6f5612 commit e174ef7

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/Annotations/Components.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class Components extends AbstractAnnotation
123123
*/
124124
public static function componentTypes(): array
125125
{
126-
return array_filter(array_keys(self::$_nested), fn ($value): bool => $value !== Attachable::class);
126+
return array_filter(array_keys(self::$_nested), fn (string $value): bool => $value !== Attachable::class);
127127
}
128128

129129
/**

src/Context.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ public function getDebugLocation(): string
183183
return $location;
184184
}
185185

186+
/**
187+
* Excludes `reflector` property.
188+
*/
189+
public function __serialize(): array
190+
{
191+
$data = (array) $this;
192+
$data['reflector'] = null;
193+
unset($data['reflector']);
194+
195+
return $data;
196+
}
197+
186198
/**
187199
* Traverse the context tree to get the property value.
188200
*/

src/Processors/ExpandEnums.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ protected function expandContextEnum(Analysis $analysis): void
8181
// no (or invalid) schema type means name
8282
$useName = Generator::isDefault($schemaType) || ($enumType && $this->native2spec($enumType) != $schemaType);
8383

84-
$schema->enum = array_map(fn ($case) => ($useName || !($case instanceof \ReflectionEnumBackedCase)) ? $case->name : $case->getBackingValue(), $re->getCases());
84+
$schema->enum = array_map(fn (\ReflectionEnumUnitCase $case) => ($useName || !($case instanceof \ReflectionEnumBackedCase)) ? $case->name : $case->getBackingValue(), $re->getCases());
8585

8686
if ($this->enumNames !== null && !$useName) {
8787
$schemaX = Generator::isDefault($schema->x) ? [] : $schema->x;
88-
$schemaX[$this->enumNames] = array_map(fn ($case): string => $case->name, $re->getCases());
88+
$schemaX[$this->enumNames] = array_map(fn (\ReflectionEnumUnitCase $case): string => $case->name, $re->getCases());
8989

9090
$schema->x = $schemaX;
9191
}

tests/Annotations/OpenApiTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ public function testInvalidVersion(): void
3939
$openapi->openapi = '2';
4040
$openapi->validate();
4141
}
42+
43+
public function testSerialize(): void
44+
{
45+
$openapi = $this->analysisFromFixtures(['Customer.php'])->openapi;
46+
$unserialized = unserialize(serialize($openapi));
47+
48+
$this->assertSpecEquals($openapi, $unserialized);
49+
}
4250
}

0 commit comments

Comments
 (0)