Skip to content

Commit c645af0

Browse files
committed
make openapi docs sparser for views
1 parent fb74df8 commit c645af0

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

api.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -3605,7 +3605,16 @@ private function setPath(String $tableName) /*: void*/
36053605
private function setComponentSchema(String $tableName) /*: void*/
36063606
{
36073607
$table = $this->reflection->getTable($tableName);
3608+
$type = $table->getType($tableName);
3609+
$pk = $table->getPk();
3610+
$pkName = $pk ? $pk->getName() : '';
36083611
foreach ($this->operations as $operation => $method) {
3612+
if (!$pkName && $operation != 'list') {
3613+
continue;
3614+
}
3615+
if ($type != 'table' && $operation != 'list') {
3616+
continue;
3617+
}
36093618
if ($operation == 'delete') {
36103619
continue;
36113620
}
@@ -3637,7 +3646,17 @@ private function setComponentSchema(String $tableName) /*: void*/
36373646

36383647
private function setComponentResponse(String $tableName) /*: void*/
36393648
{
3649+
$table = $this->reflection->getTable($tableName);
3650+
$type = $table->getType($tableName);
3651+
$pk = $table->getPk();
3652+
$pkName = $pk ? $pk->getName() : '';
36403653
foreach (['list', 'read'] as $operation) {
3654+
if (!$pkName && $operation != 'list') {
3655+
continue;
3656+
}
3657+
if ($type != 'table' && $operation != 'list') {
3658+
continue;
3659+
}
36413660
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
36423661
continue;
36433662
}
@@ -3652,12 +3671,18 @@ private function setComponentResponse(String $tableName) /*: void*/
36523671

36533672
private function setComponentRequestBody(String $tableName) /*: void*/
36543673
{
3655-
foreach (['create', 'update', 'increment'] as $operation) {
3656-
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
3657-
continue;
3674+
$table = $this->reflection->getTable($tableName);
3675+
$type = $table->getType($tableName);
3676+
$pk = $table->getPk();
3677+
$pkName = $pk ? $pk->getName() : '';
3678+
if ($pkName && $type == 'table') {
3679+
foreach (['create', 'update', 'increment'] as $operation) {
3680+
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
3681+
continue;
3682+
}
3683+
$this->openapi->set("components|requestBodies|$operation-$tableName|description", "single $tableName record");
3684+
$this->openapi->set("components|requestBodies|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . urlencode($tableName));
36583685
}
3659-
$this->openapi->set("components|requestBodies|$operation-$tableName|description", "single $tableName record");
3660-
$this->openapi->set("components|requestBodies|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . urlencode($tableName));
36613686
}
36623687
}
36633688

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,16 @@ private function setPath(String $tableName) /*: void*/
137137
private function setComponentSchema(String $tableName) /*: void*/
138138
{
139139
$table = $this->reflection->getTable($tableName);
140+
$type = $table->getType($tableName);
141+
$pk = $table->getPk();
142+
$pkName = $pk ? $pk->getName() : '';
140143
foreach ($this->operations as $operation => $method) {
144+
if (!$pkName && $operation != 'list') {
145+
continue;
146+
}
147+
if ($type != 'table' && $operation != 'list') {
148+
continue;
149+
}
141150
if ($operation == 'delete') {
142151
continue;
143152
}
@@ -169,7 +178,17 @@ private function setComponentSchema(String $tableName) /*: void*/
169178

170179
private function setComponentResponse(String $tableName) /*: void*/
171180
{
181+
$table = $this->reflection->getTable($tableName);
182+
$type = $table->getType($tableName);
183+
$pk = $table->getPk();
184+
$pkName = $pk ? $pk->getName() : '';
172185
foreach (['list', 'read'] as $operation) {
186+
if (!$pkName && $operation != 'list') {
187+
continue;
188+
}
189+
if ($type != 'table' && $operation != 'list') {
190+
continue;
191+
}
173192
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
174193
continue;
175194
}
@@ -184,12 +203,18 @@ private function setComponentResponse(String $tableName) /*: void*/
184203

185204
private function setComponentRequestBody(String $tableName) /*: void*/
186205
{
187-
foreach (['create', 'update', 'increment'] as $operation) {
188-
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
189-
continue;
206+
$table = $this->reflection->getTable($tableName);
207+
$type = $table->getType($tableName);
208+
$pk = $table->getPk();
209+
$pkName = $pk ? $pk->getName() : '';
210+
if ($pkName && $type == 'table') {
211+
foreach (['create', 'update', 'increment'] as $operation) {
212+
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
213+
continue;
214+
}
215+
$this->openapi->set("components|requestBodies|$operation-$tableName|description", "single $tableName record");
216+
$this->openapi->set("components|requestBodies|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . urlencode($tableName));
190217
}
191-
$this->openapi->set("components|requestBodies|$operation-$tableName|description", "single $tableName record");
192-
$this->openapi->set("components|requestBodies|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . urlencode($tableName));
193218
}
194219
}
195220

0 commit comments

Comments
 (0)