Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Carbon attributes in resources being not documented as string<date-time> in Open API spec #332

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading