Skip to content

Commit

Permalink
Documenting CarbonInterface as date-time string schema (#700)
Browse files Browse the repository at this point in the history
* fix: define datetime casts as date-time string types

* ref

---------

Co-authored-by: Roman Lytvynenko <[email protected]>
  • Loading branch information
axelrindle and romalytvynenko authored Feb 12, 2025
1 parent 8865094 commit dfe3bf2
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Support/Generator/TypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions tests/Files/SamplePostModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SamplePostModel extends Model
'read_time' => 'int',
'status' => Status::class,
'settings' => 'array',
'approved_at' => 'datetime',
];

public function getReadTimeAttribute()
Expand Down
1 change: 1 addition & 0 deletions tests/InferExtensions/ModelExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'title' => 'string',
'settings' => 'array<mixed>|null',
'body' => 'string',
'approved_at' => 'Carbon\Carbon|null',
'created_at' => 'Carbon\Carbon|null',
'updated_at' => 'Carbon\Carbon|null',
/* Appended attributes */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -18,5 +19,6 @@ required:
- title
- settings
- body
- approved_at
- created_at
- updated_at
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/migrations/2016_01_01_000000_create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function up()
$table->string('title');
$table->json('settings')->nullable();
$table->text('body');
$table->dateTime('approved_at')->nullable();
$table->timestamps();
});
}
Expand Down

0 comments on commit dfe3bf2

Please sign in to comment.