Skip to content

Commit

Permalink
Add isSuccessful property to Response annotation to override chec…
Browse files Browse the repository at this point in the history
…k by the status.
  • Loading branch information
digitv committed Jul 11, 2024
1 parent 114f173 commit 286bbc5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Swagger yaml generator",
"keywords": ["api", "swagger", "open auth"],
"license": "MIT",
"version": "1.4.10",
"version": "1.4.11",
"authors": [
{
"name": "Digit",
Expand Down
15 changes: 9 additions & 6 deletions oa/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @Target("METHOD")
* @Attributes({
* @Attribute("status",type="integer"),
* @Attribute("isSuccessful",type="boolean"),
* @Attribute("contentType",type="string"),
* @Attribute("description",type="string"),
* @Attribute("asList",type="boolean"),
Expand All @@ -30,6 +31,7 @@ class Response extends BaseAnnotation
public mixed $content = null;
public string $contentType = 'application/json';
public int $status = 200;
public ?bool $isSuccessful = null;
public ?string $description = null;
public bool $asList = false;
public bool $asPagedList = false;
Expand Down Expand Up @@ -179,12 +181,12 @@ public function __toString()
protected function wrapInDefaultResponse(mixed $content = null): mixed
{
$content = $content ?? $this->content;
$responseData = static::getDefaultResponse($this->contentType, $this->status);
$responseData = static::getDefaultResponse($this->contentType, $this->status, $this->isSuccessful);
if ($responseData === null) {
return $content;
}
[$responseRaw, $resultKey] = array_values($responseData);
if (($this->asPagedList || $this->asCursorPagedList) && static::isSuccessStatus($this->status)) {
if (($this->asPagedList || $this->asCursorPagedList) && ($this->isSuccessful ?? static::isSuccessStatus($this->status))) {
if ($this->asPagedList) {
$responseRaw['pagination'] = static::getPagerExample();
} elseif ($this->asCursorPagedList) {
Expand All @@ -200,13 +202,14 @@ protected function wrapInDefaultResponse(mixed $content = null): mixed
/**
* Get default response by content type [response, result_array_key].
*
* @param string $contentType
* @param int $status
* @param string $contentType
* @param int $status
* @param bool|null $isSuccessful Override check by status for success/error response
* @return mixed|null
*/
protected static function getDefaultResponse(string $contentType, int $status = 200): mixed
protected static function getDefaultResponse(string $contentType, int $status = 200, ?bool $isSuccessful = null): mixed
{
$key = static::isSuccessStatus($status) ? 'ok' : 'error';
$key = ($isSuccessful ?? static::isSuccessStatus($status)) ? 'ok' : 'error';
$responses = [
'application/json' => [
'ok' => [
Expand Down

0 comments on commit 286bbc5

Please sign in to comment.