diff --git a/src/Support/Generator/TypeTransformer.php b/src/Support/Generator/TypeTransformer.php index 28ad5055..e6e67963 100644 --- a/src/Support/Generator/TypeTransformer.php +++ b/src/Support/Generator/TypeTransformer.php @@ -132,6 +132,8 @@ public function transform(Type $type): OpenApiType } elseif ($type instanceof ArrayItemType_) { $openApiType = $this->transform($type->value); + // @todo use PhpDocSchemaTransformer + /** @var PhpDocNode|null $valueDocNode */ $valueDocNode = $type->value->getAttribute('docNode'); /** @var PhpDocNode|null $arrayItemDocNode */ @@ -165,19 +167,21 @@ public function transform(Type $type): OpenApiType $openApiType->default($default[0]); } - if ($format = array_values($docNode->getTagsByName('@format'))[0]->value->value ?? null) { - $openApiType->format($format); - } - $deprecated = array_values($docNode->getTagsByName('@deprecated'))[0]->value ?? null; if ($deprecated instanceof DeprecatedTagValueNode) { $openApiType->deprecated(true); if ($deprecated->description) { - $existingDesc = $openApiType->description ? $openApiType->description.' ' : ''; - $openApiType->setDescription($existingDesc.$deprecated->description); + $openApiType->setDescription(implode(' ', array_filter([ + $openApiType->description, + $deprecated->description, + ]))); } } + + 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/src/Support/Generator/Types/Type.php b/src/Support/Generator/Types/Type.php index 8416fd18..3e5affa8 100644 --- a/src/Support/Generator/Types/Type.php +++ b/src/Support/Generator/Types/Type.php @@ -92,11 +92,17 @@ public function addProperties(Type $fromType): self { $this->attributes = $fromType->attributes; - $this->nullable = $fromType->nullable; - $this->enum = $fromType->enum; + $this->format = $fromType->format; $this->description = $fromType->description; + $this->contentMediaType = $fromType->contentMediaType; + $this->contentEncoding = $fromType->contentEncoding; $this->example = $fromType->example; $this->default = $fromType->default; + $this->examples = $fromType->examples; + $this->enum = $fromType->enum; + $this->nullable = $fromType->nullable; + $this->deprecated = $fromType->deprecated; + $this->pattern = $fromType->pattern; return $this; } diff --git a/src/Support/OperationExtensions/RulesExtractor/PhpDocSchemaTransformer.php b/src/Support/OperationExtensions/RulesExtractor/PhpDocSchemaTransformer.php index 16c208b4..08a35e4f 100644 --- a/src/Support/OperationExtensions/RulesExtractor/PhpDocSchemaTransformer.php +++ b/src/Support/OperationExtensions/RulesExtractor/PhpDocSchemaTransformer.php @@ -48,7 +48,10 @@ public function transform(Schema $type, PhpDocNode $docNode): Schema $type->deprecated(true); if ($deprecated->description) { - $type->setDescription($type->description.$deprecated->description); + $type->setDescription(implode(' ', array_filter([ + $type->description, + $deprecated->description, + ]))); } } diff --git a/tests/Support/OperationExtensions/RequestBodyExtensionTest.php b/tests/Support/OperationExtensions/RequestBodyExtensionTest.php index edec15c3..20f28b82 100644 --- a/tests/Support/OperationExtensions/RequestBodyExtensionTest.php +++ b/tests/Support/OperationExtensions/RequestBodyExtensionTest.php @@ -780,7 +780,11 @@ public function __invoke(Request $request) it('allows marking request fields as deprecated', function () { $document = generateForRoute(RouteFacade::post('test', function (Request $request) { $request->validate([ - /** @deprecated Since 1.0.0 is deprecated. */ + /** + * Woah + * + * @deprecated Since 1.0.0 is deprecated. + */ 'foo' => ['integer'], ]); })); @@ -789,7 +793,26 @@ public function __invoke(Request $request) expect($fooProperty)->toBe([ 'type' => 'integer', + 'description' => 'Woah Since 1.0.0 is deprecated.', + 'deprecated' => true, + ]); +}); + +it('allows marking request array fields as deprecated', function () { + $document = generateForRoute(RouteFacade::post('test', function (Request $request) { + $request->validate([ + /** @deprecated Since 1.0.0 is deprecated. */ + 'foo' => 'sometimes', + 'foo.*' => ['integer'], + ]); + })); + + $fooProperty = $document['paths']['/test']['post']['requestBody']['content']['application/json']['schema']['properties']['foo']; // woah... + + expect($fooProperty)->toBe([ + 'type' => 'array', 'description' => 'Since 1.0.0 is deprecated.', 'deprecated' => true, + 'items' => ['type' => 'integer'], ]); });