Skip to content

Commit d25dd45

Browse files
author
RobertGroot
committed
Bugfix arrays
1 parent 6bdc4f4 commit d25dd45

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

src/Data/Property.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace Xolvio\OpenApiGenerator\Data;
44

55
use ReflectionClass;
6+
use ReflectionProperty;
67
use Spatie\LaravelData\Data;
78
use Spatie\LaravelData\Data as LaravelData;
89
use Spatie\LaravelData\DataCollection;
9-
use Spatie\LaravelData\Support\DataClass;
10-
use Spatie\LaravelData\Support\DataProperty;
1110

1211
class Property extends Data
1312
{
@@ -31,23 +30,25 @@ public static function fromDataClass(string $class): DataCollection
3130
throw new \RuntimeException('Class does not extend LaravelData');
3231
}
3332

34-
/** @var class-string<LaravelData> $class */
35-
$data_class = DataClass::create(new ReflectionClass($class));
36-
37-
$properties = $data_class->properties
38-
->map(fn (DataProperty $property) => self::fromProperty($property));
33+
$reflection = new ReflectionClass($class);
34+
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
3935

4036
/** @var DataCollection<int,self> */
41-
$collection = self::collection($properties->all());
37+
$collection = self::collection(
38+
array_map(
39+
fn (ReflectionProperty $property) => self::fromProperty($property),
40+
$properties
41+
)
42+
);
4243

4344
return $collection;
4445
}
4546

46-
public static function fromProperty(DataProperty $property): self
47+
public static function fromProperty(ReflectionProperty $reflection): self
4748
{
4849
return new self(
49-
name: $property->name,
50-
type: Schema::fromDataPropery($property),
50+
name: $reflection->getName(),
51+
type: Schema::fromReflectopnProperty($reflection),
5152
);
5253
}
5354
}

src/Data/Schema.php

+27-4
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,20 @@ public function __construct(
4343
$this->nullable = $this->nullable ? $this->nullable : null;
4444
}
4545

46-
public static function fromDataPropery(DataProperty $property): self
46+
public static function fromReflectopnProperty(ReflectionProperty $reflection): self
4747
{
48+
$property = DataProperty::create($reflection);
49+
4850
$type = $property->type;
4951

50-
if ($type->dataClass) {
51-
return self::fromData($type->dataClass, $type->isNullable || $type->isOptional);
52+
/** @var null|string */
53+
$data_class = $type->dataClass;
54+
55+
if ($type->isDataObject && $data_class) {
56+
return self::fromData($data_class, $type->isNullable || $type->isOptional);
57+
}
58+
if ($type->isDataCollectable && $data_class) {
59+
return self::fromDataCollection($data_class, $type->isNullable || $type->isOptional);
5260
}
5361

5462
/** @var null|string */
@@ -58,7 +66,7 @@ public static function fromDataPropery(DataProperty $property): self
5866
throw new \RuntimeException("Parameter {$property->name} has no type defined");
5967
}
6068

61-
return self::fromDataReflection(type_name: $other_type, nullable: $type->isNullable);
69+
return self::fromDataReflection(type_name: $other_type, reflection: $reflection, nullable: $type->isNullable);
6270
}
6371

6472
public static function fromDataReflection(
@@ -187,6 +195,21 @@ protected static function fromData(string $type_name, bool $nullable): self
187195
);
188196
}
189197

198+
protected static function fromDataCollection(string $type_name, bool $nullable): self
199+
{
200+
$type_name = ltrim($type_name, '\\');
201+
202+
if (! is_a($type_name, LaravelData::class, true)) {
203+
throw new \RuntimeException("Type {$type_name} is not a Data class");
204+
}
205+
206+
return new self(
207+
type: 'array',
208+
items: self::fromData($type_name, false),
209+
nullable: $nullable,
210+
);
211+
}
212+
190213
protected static function fromListDocblock(ReflectionMethod|ReflectionFunction|ReflectionProperty $reflection, bool $nullable): self
191214
{
192215
$docs = $reflection->getDocComment();

tests/Unit/PropertyTest.php

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use Spatie\LaravelData\Support\DataProperty;
43
use Xolvio\OpenApiGenerator\Data\Property;
54
use Xolvio\OpenApiGenerator\Test\ContentTypeData;
65
use Xolvio\OpenApiGenerator\Test\NotData;
@@ -21,7 +20,7 @@
2120

2221
expect($types)
2322
->toBe([
24-
'message' => [
23+
[
2524
'type' => 'string',
2625
],
2726
]);
@@ -32,44 +31,44 @@
3231

3332
expect($types)
3433
->toBe([
35-
'integer' => [
34+
[
3635
'type' => 'integer',
3736
],
38-
'nullable_integer' => [
37+
[
3938
'type' => 'integer',
4039
'nullable' => true,
4140
],
42-
'string' => [
41+
[
4342
'type' => 'string',
4443
],
45-
'nullable_string' => [
44+
[
4645
'type' => 'string',
4746
'nullable' => true,
4847
],
49-
'bool' => [
48+
[
5049
'type' => 'boolean',
5150
],
52-
'nullable_bool' => [
51+
[
5352
'type' => 'boolean',
5453
'nullable' => true,
5554
],
56-
'float' => [
55+
[
5756
'type' => 'number',
5857
],
59-
'nullable_float' => [
58+
[
6059
'type' => 'number',
6160
'nullable' => true,
6261
],
63-
'nullable_self' => [
62+
[
6463
'nullable' => true,
6564
'allOf' => [
6665
['$ref' => '#/components/schemas/RequestData'],
6766
],
6867
],
69-
'other' => [
68+
[
7069
'$ref' => '#/components/schemas/ReturnData',
7170
],
72-
'nullable_other' => [
71+
[
7372
'nullable' => true,
7473
'allOf' => [
7574
['$ref' => '#/components/schemas/ReturnData'],
@@ -83,7 +82,7 @@
8382
$reflection = new ReflectionClass($class);
8483

8584
foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $reflection_property) {
86-
$property = Property::fromProperty(DataProperty::create($reflection_property));
85+
$property = Property::fromProperty($reflection_property);
8786

8887
expect($property->getName())
8988
->toBe($reflection_property->getName());

0 commit comments

Comments
 (0)