Skip to content

Commit b5fda86

Browse files
committed
Improve responses
1 parent abea028 commit b5fda86

File tree

2 files changed

+118
-14
lines changed

2 files changed

+118
-14
lines changed

api.php

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,8 +3356,19 @@ public function build(): OpenApiDefinition
33563356
foreach ($tableNames as $tableName) {
33573357
$this->setPath($tableName);
33583358
}
3359+
$this->openapi->set("components|responses|pk_integer|description", "inserted primary key value (integer)");
3360+
$this->openapi->set("components|responses|pk_integer|content|application/json|schema|type", "integer");
3361+
$this->openapi->set("components|responses|pk_integer|content|application/json|schema|format", "int64");
3362+
$this->openapi->set("components|responses|pk_string|description", "inserted primary key value (string)");
3363+
$this->openapi->set("components|responses|pk_string|content|application/json|schema|type", "string");
3364+
$this->openapi->set("components|responses|pk_string|content|application/json|schema|format", "uuid");
3365+
$this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
3366+
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
3367+
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
33593368
foreach ($tableNames as $tableName) {
33603369
$this->setComponentSchema($tableName);
3370+
$this->setComponentResponse($tableName);
3371+
$this->setComponentRequestBody($tableName);
33613372
}
33623373
$this->setComponentParameters();
33633374
foreach ($tableNames as $index => $tableName) {
@@ -3372,33 +3383,74 @@ private function setPath(String $tableName) /*: void*/
33723383
$pk = $table->getPk();
33733384
$pkName = $pk ? $pk->getName() : '';
33743385
foreach ($this->operations as $operation => $method) {
3386+
if (!$pkName && $operation != 'list') {
3387+
continue;
3388+
}
33753389
if (in_array($operation, ['list', 'create'])) {
33763390
$path = sprintf('/records/%s', $tableName);
33773391
} else {
3378-
if (!$pkName) {
3379-
continue;
3380-
}
33813392
$path = sprintf('/records/%s/{%s}', $tableName, $pkName);
33823393
$this->openapi->set("paths|$path|$method|parameters|0|\$ref", "#/components/parameters/pk");
33833394
}
3395+
if (in_array($operation, ['create', 'update'])) {
3396+
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/single_" . urlencode($tableName));
3397+
}
33843398
$this->openapi->set("paths|$path|$method|tags|0", "$tableName");
33853399
$this->openapi->set("paths|$path|$method|description", "$operation $tableName");
3386-
$this->openapi->set("paths|$path|$method|responses|200|description", "$operation $tableName succeeded");
3400+
switch ($operation) {
3401+
case 'list':
3402+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/list_of_" . urlencode($tableName));
3403+
break;
3404+
case 'create':
3405+
if ($pk->getType() == 'integer') {
3406+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_integer");
3407+
} else {
3408+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_string");
3409+
}
3410+
break;
3411+
case 'read':
3412+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/single_" . urlencode($tableName));
3413+
break;
3414+
case 'update':
3415+
case 'delete':
3416+
case 'increment':
3417+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/rows_affected");
3418+
break;
3419+
}
3420+
33873421
}
33883422
}
33893423

33903424
private function setComponentSchema(String $tableName) /*: void*/
33913425
{
3392-
$this->openapi->set("components|schemas|$tableName|type", "object");
3426+
$this->openapi->set("components|schemas|single_$tableName|type", "object");
33933427
$table = $this->reflection->getTable($tableName);
33943428
foreach ($table->columnNames() as $columnName) {
33953429
$column = $table->get($columnName);
33963430
$properties = $this->types[$column->getType()];
33973431
foreach ($properties as $key => $value) {
3398-
$this->openapi->set("components|schemas|$tableName|properties|$columnName|$key", $value);
3432+
$this->openapi->set("components|schemas|single_$tableName|properties|$columnName|$key", $value);
33993433
}
3400-
34013434
}
3435+
$this->openapi->set("components|schemas|list_of_$tableName|type", "object");
3436+
$this->openapi->set("components|schemas|list_of_$tableName|properties|count|type", "integer");
3437+
$this->openapi->set("components|schemas|list_of_$tableName|properties|count|format", "int64");
3438+
$this->openapi->set("components|schemas|list_of_$tableName|properties|records|type", "array");
3439+
$this->openapi->set("components|schemas|list_of_$tableName|properties|records|items|\$ref", "#/components/schemas/single_" . urlencode($tableName));
3440+
}
3441+
3442+
private function setComponentResponse(String $tableName) /*: void*/
3443+
{
3444+
$this->openapi->set("components|responses|single_$tableName|description", "single $tableName record");
3445+
$this->openapi->set("components|responses|single_$tableName|content|application/json|schema|\$ref", "#/components/schemas/single_" . urlencode($tableName));
3446+
$this->openapi->set("components|responses|list_of_$tableName|description", "list of $tableName records");
3447+
$this->openapi->set("components|responses|list_of_$tableName|content|application/json|schema|\$ref", "#/components/schemas/list_of_" . urlencode($tableName));
3448+
}
3449+
3450+
private function setComponentRequestBody(String $tableName) /*: void*/
3451+
{
3452+
$this->openapi->set("components|requestBodies|single_$tableName|description", "single $tableName record");
3453+
$this->openapi->set("components|requestBodies|single_$tableName|content|application/json|schema|\$ref", "#/components/schemas/single_" . urlencode($tableName));
34023454
}
34033455

34043456
private function setComponentParameters() /*: void*/

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,19 @@ public function build(): OpenApiDefinition
4545
foreach ($tableNames as $tableName) {
4646
$this->setPath($tableName);
4747
}
48+
$this->openapi->set("components|responses|pk_integer|description", "inserted primary key value (integer)");
49+
$this->openapi->set("components|responses|pk_integer|content|application/json|schema|type", "integer");
50+
$this->openapi->set("components|responses|pk_integer|content|application/json|schema|format", "int64");
51+
$this->openapi->set("components|responses|pk_string|description", "inserted primary key value (string)");
52+
$this->openapi->set("components|responses|pk_string|content|application/json|schema|type", "string");
53+
$this->openapi->set("components|responses|pk_string|content|application/json|schema|format", "uuid");
54+
$this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
55+
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
56+
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
4857
foreach ($tableNames as $tableName) {
4958
$this->setComponentSchema($tableName);
59+
$this->setComponentResponse($tableName);
60+
$this->setComponentRequestBody($tableName);
5061
}
5162
$this->setComponentParameters();
5263
foreach ($tableNames as $index => $tableName) {
@@ -61,33 +72,74 @@ private function setPath(String $tableName) /*: void*/
6172
$pk = $table->getPk();
6273
$pkName = $pk ? $pk->getName() : '';
6374
foreach ($this->operations as $operation => $method) {
75+
if (!$pkName && $operation != 'list') {
76+
continue;
77+
}
6478
if (in_array($operation, ['list', 'create'])) {
6579
$path = sprintf('/records/%s', $tableName);
6680
} else {
67-
if (!$pkName) {
68-
continue;
69-
}
7081
$path = sprintf('/records/%s/{%s}', $tableName, $pkName);
7182
$this->openapi->set("paths|$path|$method|parameters|0|\$ref", "#/components/parameters/pk");
7283
}
84+
if (in_array($operation, ['create', 'update'])) {
85+
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/single_" . urlencode($tableName));
86+
}
7387
$this->openapi->set("paths|$path|$method|tags|0", "$tableName");
7488
$this->openapi->set("paths|$path|$method|description", "$operation $tableName");
75-
$this->openapi->set("paths|$path|$method|responses|200|description", "$operation $tableName succeeded");
89+
switch ($operation) {
90+
case 'list':
91+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/list_of_" . urlencode($tableName));
92+
break;
93+
case 'create':
94+
if ($pk->getType() == 'integer') {
95+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_integer");
96+
} else {
97+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_string");
98+
}
99+
break;
100+
case 'read':
101+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/single_" . urlencode($tableName));
102+
break;
103+
case 'update':
104+
case 'delete':
105+
case 'increment':
106+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/rows_affected");
107+
break;
108+
}
109+
76110
}
77111
}
78112

79113
private function setComponentSchema(String $tableName) /*: void*/
80114
{
81-
$this->openapi->set("components|schemas|$tableName|type", "object");
115+
$this->openapi->set("components|schemas|single_$tableName|type", "object");
82116
$table = $this->reflection->getTable($tableName);
83117
foreach ($table->columnNames() as $columnName) {
84118
$column = $table->get($columnName);
85119
$properties = $this->types[$column->getType()];
86120
foreach ($properties as $key => $value) {
87-
$this->openapi->set("components|schemas|$tableName|properties|$columnName|$key", $value);
121+
$this->openapi->set("components|schemas|single_$tableName|properties|$columnName|$key", $value);
88122
}
89-
90123
}
124+
$this->openapi->set("components|schemas|list_of_$tableName|type", "object");
125+
$this->openapi->set("components|schemas|list_of_$tableName|properties|count|type", "integer");
126+
$this->openapi->set("components|schemas|list_of_$tableName|properties|count|format", "int64");
127+
$this->openapi->set("components|schemas|list_of_$tableName|properties|records|type", "array");
128+
$this->openapi->set("components|schemas|list_of_$tableName|properties|records|items|\$ref", "#/components/schemas/single_" . urlencode($tableName));
129+
}
130+
131+
private function setComponentResponse(String $tableName) /*: void*/
132+
{
133+
$this->openapi->set("components|responses|single_$tableName|description", "single $tableName record");
134+
$this->openapi->set("components|responses|single_$tableName|content|application/json|schema|\$ref", "#/components/schemas/single_" . urlencode($tableName));
135+
$this->openapi->set("components|responses|list_of_$tableName|description", "list of $tableName records");
136+
$this->openapi->set("components|responses|list_of_$tableName|content|application/json|schema|\$ref", "#/components/schemas/list_of_" . urlencode($tableName));
137+
}
138+
139+
private function setComponentRequestBody(String $tableName) /*: void*/
140+
{
141+
$this->openapi->set("components|requestBodies|single_$tableName|description", "single $tableName record");
142+
$this->openapi->set("components|requestBodies|single_$tableName|content|application/json|schema|\$ref", "#/components/schemas/single_" . urlencode($tableName));
91143
}
92144

93145
private function setComponentParameters() /*: void*/

0 commit comments

Comments
 (0)