diff --git a/src/Support/Generator/TypeTransformer.php b/src/Support/Generator/TypeTransformer.php index ddaee0cb..c885e135 100644 --- a/src/Support/Generator/TypeTransformer.php +++ b/src/Support/Generator/TypeTransformer.php @@ -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 @@ -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)) { diff --git a/tests/TypeToSchemaTransformerTest.php b/tests/TypeToSchemaTransformerTest.php index 25280fe6..6a03a428 100644 --- a/tests/TypeToSchemaTransformerTest.php +++ b/tests/TypeToSchemaTransformerTest.php @@ -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) @@ -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';