diff --git a/src/Support/Generator/TypeTransformer.php b/src/Support/Generator/TypeTransformer.php index 30978b9a..50706d58 100644 --- a/src/Support/Generator/TypeTransformer.php +++ b/src/Support/Generator/TypeTransformer.php @@ -2,6 +2,7 @@ namespace Dedoc\Scramble\Support\Generator; +use Carbon\CarbonInterface; use Dedoc\Scramble\Infer; use Dedoc\Scramble\OpenApiContext; use Dedoc\Scramble\PhpDoc\PhpDocTypeHelper; @@ -181,7 +182,11 @@ public function transform(Type $type) } elseif ($type instanceof \Dedoc\Scramble\Support\Type\MixedType) { $openApiType = new MixedType; } elseif ($type instanceof \Dedoc\Scramble\Support\Type\ObjectType) { - $openApiType = new ObjectType; + if ($type->isInstanceOf(CarbonInterface::class)) { + $openApiType = (new StringType)->format('date-time'); + } else { + $openApiType = new ObjectType; + } } elseif ($type instanceof \Dedoc\Scramble\Support\Type\IntersectionType) { $openApiType = (new AllOf)->setItems(array_filter(array_map( fn ($t) => $this->transform($t), diff --git a/tests/Files/SamplePostModel.php b/tests/Files/SamplePostModel.php index b447b16c..13d54e4f 100644 --- a/tests/Files/SamplePostModel.php +++ b/tests/Files/SamplePostModel.php @@ -18,6 +18,7 @@ class SamplePostModel extends Model 'read_time' => 'int', 'status' => Status::class, 'settings' => 'array', + 'approved_at' => 'datetime', ]; public function getReadTimeAttribute() diff --git a/tests/InferExtensions/ModelExtensionTest.php b/tests/InferExtensions/ModelExtensionTest.php index 42b10f45..0a009b15 100644 --- a/tests/InferExtensions/ModelExtensionTest.php +++ b/tests/InferExtensions/ModelExtensionTest.php @@ -30,6 +30,7 @@ 'title' => 'string', 'settings' => 'array|null', 'body' => 'string', + 'approved_at' => 'Carbon\Carbon|null', 'created_at' => 'Carbon\Carbon|null', 'updated_at' => 'Carbon\Carbon|null', /* Appended attributes */ diff --git a/tests/__snapshots__/InferTypesTest__it_infers_model_type__1.yml b/tests/__snapshots__/InferTypesTest__it_infers_model_type__1.yml index e1446eb6..e884b6eb 100644 --- a/tests/__snapshots__/InferTypesTest__it_infers_model_type__1.yml +++ b/tests/__snapshots__/InferTypesTest__it_infers_model_type__1.yml @@ -6,6 +6,7 @@ properties: title: { type: string } settings: { type: [array, 'null'], items: { } } body: { type: string } + approved_at: { type: [string, 'null'], format: date-time } created_at: { type: [string, 'null'], format: date-time } updated_at: { type: [string, 'null'], format: date-time } parent: { $ref: '#/components/schemas/SamplePostModel' } @@ -18,5 +19,6 @@ required: - title - settings - body + - approved_at - created_at - updated_at diff --git a/tests/__snapshots__/InferTypesTest__it_infers_model_type_when_toArray_is_implemented__1.yml b/tests/__snapshots__/InferTypesTest__it_infers_model_type_when_toArray_is_implemented__1.yml index 994f75d6..8e519c24 100644 --- a/tests/__snapshots__/InferTypesTest__it_infers_model_type_when_toArray_is_implemented__1.yml +++ b/tests/__snapshots__/InferTypesTest__it_infers_model_type_when_toArray_is_implemented__1.yml @@ -4,7 +4,7 @@ properties: children: { type: array, items: { $ref: '#/components/schemas/SamplePostModelWithToArray' } } read_time: { type: string } user: { $ref: '#/components/schemas/SampleUserModel' } - created_at: { type: [object, 'null'] } + created_at: { type: [string, 'null'], format: date-time } required: - id - children diff --git a/tests/migrations/2016_01_01_000000_create_posts_table.php b/tests/migrations/2016_01_01_000000_create_posts_table.php index b439eb8a..04c1e5d8 100644 --- a/tests/migrations/2016_01_01_000000_create_posts_table.php +++ b/tests/migrations/2016_01_01_000000_create_posts_table.php @@ -20,6 +20,7 @@ public function up() $table->string('title'); $table->json('settings')->nullable(); $table->text('body'); + $table->dateTime('approved_at')->nullable(); $table->timestamps(); }); }