Skip to content

Commit

Permalink
Merge pull request #332 from dedoc/239-timestamp-columns-are-incorrec…
Browse files Browse the repository at this point in the history
…tly-recognized-as-objects-instead-of-a-string-with-the-date-time-format

Fixed `Carbon` attributes in resources being not documented as `string<date-time>` in Open API spec
  • Loading branch information
romalytvynenko authored Mar 11, 2024
2 parents 4628d2d + 58c1647 commit 6280da6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Support/TypeToSchemaExtensions/JsonResourceTypeToSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dedoc\Scramble\Support\TypeToSchemaExtensions;

use Carbon\Carbon;
use Dedoc\Scramble\Extensions\TypeToSchemaExtension;
use Dedoc\Scramble\Support\Generator\Reference;
use Dedoc\Scramble\Support\Generator\Response;
Expand All @@ -14,6 +15,7 @@
use Dedoc\Scramble\Support\Type\Generic;
use Dedoc\Scramble\Support\Type\Literal\LiteralBooleanType;
use Dedoc\Scramble\Support\Type\ObjectType;
use Dedoc\Scramble\Support\Type\StringType;
use Dedoc\Scramble\Support\Type\Type;
use Dedoc\Scramble\Support\Type\TypeWalker;
use Dedoc\Scramble\Support\Type\Union;
Expand Down Expand Up @@ -70,6 +72,19 @@ private function flattenMergeValues(array $items)
return [$item];
}

if (
$item->value instanceof Union
&& (new TypeWalker)->first($item->value, fn (Type $t) => $t->isInstanceOf(Carbon::class))
) {
(new TypeWalker)->replace($item->value, function (Type $t) {
return $t->isInstanceOf(Carbon::class)
? tap(new StringType, fn ($t) => $t->setAttribute('format', 'date-time'))
: null;
});

return [$item];
}

if ($item->value->isInstanceOf(JsonResource::class)) {
$resource = $this->getResourceType($item->value);

Expand Down
31 changes: 31 additions & 0 deletions tests/TypeToSchemaTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Dedoc\Scramble\Support\TypeToSchemaExtensions\AnonymousResourceCollectionTypeToSchema;
use Dedoc\Scramble\Support\TypeToSchemaExtensions\EnumToSchema;
use Dedoc\Scramble\Support\TypeToSchemaExtensions\JsonResourceTypeToSchema;
use Dedoc\Scramble\Tests\Files\SamplePostModel;
use Illuminate\Http\Resources\Json\JsonResource;

use function Spatie\Snapshots\assertMatchesSnapshot;
Expand Down Expand Up @@ -132,6 +133,21 @@
]);
});

it('infers date column directly referenced in json as date-time', function () {
$transformer = new TypeTransformer($infer = app(Infer::class), $components = new Components, [JsonResourceTypeToSchema::class]);

$type = new ObjectType(InferTypesTest_JsonResourceWithCarbonAttribute::class);

expect($transformer->transform($type)->toArray())->toBe([
'$ref' => '#/components/schemas/InferTypesTest_JsonResourceWithCarbonAttribute',
]);

expect($components->getSchema(InferTypesTest_JsonResourceWithCarbonAttribute::class)->toArray()['properties']['created_at'])->toBe([
'type' => ['string', 'null'],
'format' => 'date-time',
]);
});

class ComplexTypeHandlersTest_SampleType extends JsonResource
{
public function toArray($request)
Expand Down Expand Up @@ -220,6 +236,21 @@ public function toArray($request)
}
}

/**
* @property SamplePostModel $resource
*/
class InferTypesTest_JsonResourceWithCarbonAttribute extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}

enum StatusTwo: string
{
case DRAFT = 'draft';
Expand Down

0 comments on commit 6280da6

Please sign in to comment.