Skip to content

Commit 3cd8272

Browse files
author
RobertGroot
committed
Added date support
1 parent aff7a78 commit 3cd8272

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

src/Commands/GenerateOpenApiCommand.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Xolvio\OpenApiGenerator\Commands;
44

5-
use App\Facades\Str;
65
use Illuminate\Console\Command;
76
use Illuminate\Routing\Route;
87
use Illuminate\Support\Facades\File;
@@ -50,7 +49,7 @@ protected function getRoutes(): array
5049
$initial_routes = array_values(array_filter(
5150
FacadeRoute::getRoutes()->getRoutes(),
5251
function (Route $route) {
53-
$first_prefix = explode('/', $route->getPrefix())[0];
52+
$first_prefix = explode('/', $route->getPrefix() ?? '')[0];
5453

5554
return in_array($first_prefix, config('openapi-generator.included_route_prefixes', []), true)
5655
&& ! $this->strStartsWith($route->getName() ?? '', config('openapi-generator.ignored_route_names', []));

src/Data/OpenApi.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public function toArray(): array
6868
$this->resolveSchemas();
6969

7070
$paths = [
71-
'paths' => count($this->paths) > 0 ? array_map(
72-
fn (array $path) => array_map(
73-
fn (Operation $operation) => $operation->toArray(),
74-
$path
75-
),
71+
'paths' => count($this->paths) > 0 ? array_map(
72+
fn (array $path) => array_map(
73+
fn (Operation $operation) => $operation->toArray(),
74+
$path
75+
),
7676
$this->paths
7777
) : new stdClass(), ];
7878

src/Data/Schema.php

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Xolvio\OpenApiGenerator\Data;
44

5+
use DateTimeInterface;
56
use Illuminate\Database\Eloquent\Model;
67
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
78
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
@@ -51,6 +52,10 @@ public static function fromDataReflection(string|ReflectionNamedType $type_name,
5152

5253
$is_class = class_exists($type_name);
5354

55+
if (is_a($type_name, DateTimeInterface::class, true)) {
56+
return self::fromDateTime($nullable);
57+
}
58+
5459
if (! $is_class && 'array' !== $type_name) {
5560
return self::fromBuiltin($type_name, $nullable);
5661
}
@@ -127,6 +132,11 @@ protected static function fromBuiltin(string $type_name, bool $nullable): self
127132
return new self(type: $type_name, nullable: $nullable);
128133
}
129134

135+
protected static function fromDateTime(bool $nullable): self
136+
{
137+
return new self(type: 'string', format: 'date-time', nullable: $nullable);
138+
}
139+
130140
protected static function fromEnum(string $type, bool $nullable): self
131141
{
132142
$enum = (new ReflectionEnum($type));

tests/Dummy/RequestData.php

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Xolvio\OpenApiGenerator\Test;
44

5+
use DateTime;
6+
use DateTimeInterface;
57
use Spatie\LaravelData\Data;
68

79
class RequestData extends Data
@@ -18,6 +20,8 @@ public function __construct(
1820
public ?RequestData $nullable_self,
1921
public ReturnData $other,
2022
public ?ReturnData $nullable_other,
23+
public DateTimeInterface $date,
24+
public ?DateTimeInterface $nullable_date,
2125
) {
2226
}
2327

@@ -35,6 +39,8 @@ public static function create(): self
3539
nullable_self: null,
3640
other: new ReturnData(),
3741
nullable_other: null,
42+
date: new DateTime(),
43+
nullable_date: null,
3844
);
3945
}
4046
}

tests/Unit/PropertyTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@
7474
['$ref' => '#/components/schemas/ReturnData'],
7575
],
7676
],
77+
[
78+
'type' => 'string',
79+
],
80+
[
81+
'type' => 'string',
82+
'nullable' => true,
83+
],
7784
]);
7885
});
7986

tests/Unit/SchemaTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@
6363
it('can create data schema', function () {
6464
$schema = Schema::fromDataClass(RequestData::class);
6565
expect($schema)->toHaveProperty('type', 'object');
66-
expect($schema->toArray()['properties'])->toHaveLength(11);
66+
expect($schema->toArray()['properties'])->toHaveLength(13);
6767
});

0 commit comments

Comments
 (0)