Skip to content

Commit

Permalink
added @format support
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Mar 25, 2024
1 parent 9872528 commit 39e47fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Support/Generator/TypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public function transform(Type $type)
$openApiType = $this->transform($type->value);

if ($docNode = $type->getAttribute('docNode')) {
/** @var PhpDocNode $docNode */

$varNode = $docNode->getVarTagValues()[0] ?? null;

$openApiType = $varNode && $varNode->type
Expand All @@ -121,6 +123,10 @@ public function transform(Type $type)
if ($examples = ExamplesExtractor::make($docNode)->extract(preferString: $openApiType instanceof StringType)) {
$openApiType->examples($examples);
}

if ($format = array_values($docNode->getTagsByName('@format'))[0]->value->value ?? null) {
$openApiType->format($format);
}
}
} elseif ($type instanceof Union) {
if (count($type->types) === 2 && collect($type->types)->contains(fn ($t) => $t instanceof \Dedoc\Scramble\Support\Type\NullType)) {
Expand Down
32 changes: 32 additions & 0 deletions tests/TypeToSchemaTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@
]);
});

it('supports @format tag in api resource', function () {
$transformer = new TypeTransformer($infer = app(Infer::class), $components = new Components, [JsonResourceTypeToSchema::class]);

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

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

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

class ComplexTypeHandlersTest_SampleType extends JsonResource
{
public function toArray($request)
Expand Down Expand Up @@ -300,6 +315,23 @@ public function toArray($request)
}
}

/**
* @property SamplePostModel $resource
*/
class ApiResourceTest_ResourceWithFormat extends JsonResource
{
public function toArray($request)
{
return [
/**
* @var string $now
* @format date-time
*/
'now' => now(),
];
}
}

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

0 comments on commit 39e47fc

Please sign in to comment.