Skip to content

Commit 286bbc5

Browse files
committed
Add isSuccessful property to Response annotation to override check by the status.
1 parent 114f173 commit 286bbc5

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Swagger yaml generator",
44
"keywords": ["api", "swagger", "open auth"],
55
"license": "MIT",
6-
"version": "1.4.10",
6+
"version": "1.4.11",
77
"authors": [
88
{
99
"name": "Digit",

oa/Response.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @Target("METHOD")
1919
* @Attributes({
2020
* @Attribute("status",type="integer"),
21+
* @Attribute("isSuccessful",type="boolean"),
2122
* @Attribute("contentType",type="string"),
2223
* @Attribute("description",type="string"),
2324
* @Attribute("asList",type="boolean"),
@@ -30,6 +31,7 @@ class Response extends BaseAnnotation
3031
public mixed $content = null;
3132
public string $contentType = 'application/json';
3233
public int $status = 200;
34+
public ?bool $isSuccessful = null;
3335
public ?string $description = null;
3436
public bool $asList = false;
3537
public bool $asPagedList = false;
@@ -179,12 +181,12 @@ public function __toString()
179181
protected function wrapInDefaultResponse(mixed $content = null): mixed
180182
{
181183
$content = $content ?? $this->content;
182-
$responseData = static::getDefaultResponse($this->contentType, $this->status);
184+
$responseData = static::getDefaultResponse($this->contentType, $this->status, $this->isSuccessful);
183185
if ($responseData === null) {
184186
return $content;
185187
}
186188
[$responseRaw, $resultKey] = array_values($responseData);
187-
if (($this->asPagedList || $this->asCursorPagedList) && static::isSuccessStatus($this->status)) {
189+
if (($this->asPagedList || $this->asCursorPagedList) && ($this->isSuccessful ?? static::isSuccessStatus($this->status))) {
188190
if ($this->asPagedList) {
189191
$responseRaw['pagination'] = static::getPagerExample();
190192
} elseif ($this->asCursorPagedList) {
@@ -200,13 +202,14 @@ protected function wrapInDefaultResponse(mixed $content = null): mixed
200202
/**
201203
* Get default response by content type [response, result_array_key].
202204
*
203-
* @param string $contentType
204-
* @param int $status
205+
* @param string $contentType
206+
* @param int $status
207+
* @param bool|null $isSuccessful Override check by status for success/error response
205208
* @return mixed|null
206209
*/
207-
protected static function getDefaultResponse(string $contentType, int $status = 200): mixed
210+
protected static function getDefaultResponse(string $contentType, int $status = 200, ?bool $isSuccessful = null): mixed
208211
{
209-
$key = static::isSuccessStatus($status) ? 'ok' : 'error';
212+
$key = ($isSuccessful ?? static::isSuccessStatus($status)) ? 'ok' : 'error';
210213
$responses = [
211214
'application/json' => [
212215
'ok' => [

0 commit comments

Comments
 (0)