generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for manually annotating request parameters using attrib…
…utes (#686) * parameters annotations wip implementation * parameter extractors introduced so attributes extractor can work on top of third party packages extractors * Fix styling * parameter defaults --------- Co-authored-by: romalytvynenko <[email protected]>
- Loading branch information
1 parent
6e0232c
commit d3546e8
Showing
26 changed files
with
822 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)] | ||
class CookieParameter extends Parameter | ||
{ | ||
public readonly bool $required; | ||
|
||
public function __construct( | ||
string $name, | ||
?string $description = null, | ||
?bool $required = null, | ||
$deprecated = false, | ||
?string $type = null, | ||
bool $infer = true, | ||
mixed $default = new MissingValue, | ||
mixed $example = new MissingValue, | ||
array $examples = [], | ||
) { | ||
parent::__construct('cookie', $name, $description, $required, $deprecated, $type, $infer, $default, $example, $examples); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Dedoc\Scramble\Support\Generator\Example as OpenApiExample; | ||
use Dedoc\Scramble\Support\Generator\MissingValue as OpenApiMissingValue; | ||
|
||
class Example | ||
{ | ||
public function __construct( | ||
public mixed $value = new MissingValue, | ||
public ?string $summary = null, | ||
public ?string $description = null, | ||
public ?string $externalValue = null, | ||
) {} | ||
|
||
public static function toOpenApiExample(mixed $example) | ||
{ | ||
if ($example instanceof static) { | ||
return new OpenApiExample( | ||
value: $example->value instanceof MissingValue ? new OpenApiMissingValue : $example->value, | ||
summary: $example->summary, | ||
description: $example->description, | ||
externalValue: $example->externalValue, | ||
); | ||
} | ||
|
||
return $example; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)] | ||
class HeaderParameter extends Parameter | ||
{ | ||
public readonly bool $required; | ||
|
||
public function __construct( | ||
string $name, | ||
?string $description = null, | ||
?bool $required = null, | ||
$deprecated = false, | ||
?string $type = null, | ||
bool $infer = true, | ||
mixed $default = new MissingValue, | ||
mixed $example = new MissingValue, | ||
array $examples = [], | ||
) { | ||
parent::__construct('header', $name, $description, $required, $deprecated, $type, $infer, $default, $example, $examples); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
class MissingValue {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)] | ||
class Parameter | ||
{ | ||
public readonly bool $required; | ||
|
||
/** | ||
* @param 'query'|'path'|'header'|'cookie' $in | ||
* @param scalar|array|object|MissingValue $example | ||
* @param array<string, Example> $examples The key is a distinct name and the value is an example object. | ||
*/ | ||
public function __construct( | ||
public readonly string $in, | ||
public readonly string $name, | ||
public readonly ?string $description = null, | ||
?bool $required = null, | ||
public bool $deprecated = false, | ||
public ?string $type = null, | ||
public bool $infer = true, | ||
public mixed $default = new MissingValue, | ||
public mixed $example = new MissingValue, | ||
public array $examples = [], | ||
) { | ||
$this->required = $required !== null ? $required : $this->in === 'path'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)] | ||
class PathParameter extends Parameter | ||
{ | ||
public readonly bool $required; | ||
|
||
public function __construct( | ||
string $name, | ||
?string $description = null, | ||
?bool $required = null, | ||
$deprecated = false, | ||
?string $type = null, | ||
bool $infer = true, | ||
mixed $default = new MissingValue, | ||
mixed $example = new MissingValue, | ||
array $examples = [], | ||
) { | ||
parent::__construct('path', $name, $description, $required, $deprecated, $type, $infer, $default, $example, $examples); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)] | ||
class QueryParameter extends Parameter | ||
{ | ||
public readonly bool $required; | ||
|
||
public function __construct( | ||
string $name, | ||
?string $description = null, | ||
?bool $required = null, | ||
$deprecated = false, | ||
?string $type = null, | ||
bool $infer = true, | ||
mixed $default = new MissingValue, | ||
mixed $example = new MissingValue, | ||
array $examples = [], | ||
) { | ||
parent::__construct('query', $name, $description, $required, $deprecated, $type, $infer, $default, $example, $examples); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Configuration; | ||
|
||
use Dedoc\Scramble\Support\OperationExtensions\ParameterExtractor\FormRequestParametersExtractor; | ||
use Dedoc\Scramble\Support\OperationExtensions\ParameterExtractor\ValidateCallParametersExtractor; | ||
use Illuminate\Support\Arr; | ||
|
||
class ParametersExtractors | ||
{ | ||
protected array $extractors = []; | ||
|
||
protected array $appends = []; | ||
|
||
protected array $prepends = []; | ||
|
||
public function append(array|string $extractor) | ||
{ | ||
$this->appends = array_merge( | ||
$this->appends, | ||
Arr::wrap($extractor) | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function prepend(array|string $extractor) | ||
{ | ||
$this->prepends = array_merge( | ||
$this->prepends, | ||
Arr::wrap($extractor) | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function use(array $extractors) | ||
{ | ||
$this->extractors = $extractors; | ||
|
||
return $this; | ||
} | ||
|
||
public function all(): array | ||
{ | ||
$base = $this->extractors ?: [ | ||
FormRequestParametersExtractor::class, | ||
ValidateCallParametersExtractor::class, | ||
]; | ||
|
||
return array_values(array_unique([ | ||
...$this->prepends, | ||
...$base, | ||
...$this->appends, | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.