|
@@ -3817,6 +3817,25 @@ class OpenApiBuilder
|
3817
|
3817
|
return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
|
3818
|
3818
|
}
|
3819
|
3819
|
|
|
3820
|
+ private function getAllTableReferences(): array
|
|
3821
|
+ {
|
|
3822
|
+ $tableReferences = array();
|
|
3823
|
+ foreach ($this->reflection->getTableNames() as $tableName) {
|
|
3824
|
+ $table = $this->reflection->getTable($tableName);
|
|
3825
|
+ foreach ($table->getColumnNames() as $columnName) {
|
|
3826
|
+ $column = $table->getColumn($columnName);
|
|
3827
|
+ $referencedTableName = $column->getFk();
|
|
3828
|
+ if ($referencedTableName) {
|
|
3829
|
+ if (!isset($tableReferences[$referencedTableName])) {
|
|
3830
|
+ $tableReferences[$referencedTableName] = array();
|
|
3831
|
+ }
|
|
3832
|
+ $tableReferences[$referencedTableName][] = "$tableName.$columnName";
|
|
3833
|
+ }
|
|
3834
|
+ }
|
|
3835
|
+ }
|
|
3836
|
+ return $tableReferences;
|
|
3837
|
+ }
|
|
3838
|
+
|
3820
|
3839
|
public function build(): OpenApiDefinition
|
3821
|
3840
|
{
|
3822
|
3841
|
$this->openapi->set("openapi", "3.0.0");
|
|
@@ -3836,8 +3855,10 @@ class OpenApiBuilder
|
3836
|
3855
|
$this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
|
3837
|
3856
|
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
|
3838
|
3857
|
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
|
|
3858
|
+ $tableReferences = $this->getAllTableReferences();
|
3839
|
3859
|
foreach ($tableNames as $tableName) {
|
3840
|
|
- $this->setComponentSchema($tableName);
|
|
3860
|
+ $references = isset($tableReferences[$tableName])?$tableReferences[$tableName]:array();
|
|
3861
|
+ $this->setComponentSchema($tableName, $references);
|
3841
|
3862
|
$this->setComponentResponse($tableName);
|
3842
|
3863
|
$this->setComponentRequestBody($tableName);
|
3843
|
3864
|
}
|
|
@@ -3927,7 +3948,7 @@ class OpenApiBuilder
|
3927
|
3948
|
}
|
3928
|
3949
|
}
|
3929
|
3950
|
|
3930
|
|
- private function setComponentSchema(String $tableName) /*: void*/
|
|
3951
|
+ private function setComponentSchema(String $tableName, array $references) /*: void*/
|
3931
|
3952
|
{
|
3932
|
3953
|
$table = $this->reflection->getTable($tableName);
|
3933
|
3954
|
$type = $table->getType();
|
|
@@ -3967,6 +3988,7 @@ class OpenApiBuilder
|
3967
|
3988
|
}
|
3968
|
3989
|
if ($column->getPk()) {
|
3969
|
3990
|
$this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
|
|
3991
|
+ $this->openapi->set("$prefix|properties|$columnName|x-referenced", $references);
|
3970
|
3992
|
}
|
3971
|
3993
|
$fk = $column->getFk();
|
3972
|
3994
|
if ($fk) {
|
|
@@ -4032,19 +4054,19 @@ class OpenApiBuilder
|
4032
|
4054
|
$this->openapi->set("components|parameters|filter|schema|items|type", "string");
|
4033
|
4055
|
$this->openapi->set("components|parameters|filter|description", "Filters to be applied. Each filter consists of a column, an operator and a value (comma separated). Example: id,eq,1");
|
4034
|
4056
|
$this->openapi->set("components|parameters|filter|required", false);
|
4035
|
|
-
|
|
4057
|
+
|
4036
|
4058
|
$this->openapi->set("components|parameters|include|name", "include");
|
4037
|
4059
|
$this->openapi->set("components|parameters|include|in", "query");
|
4038
|
4060
|
$this->openapi->set("components|parameters|include|schema|type", "string");
|
4039
|
4061
|
$this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
|
4040
|
4062
|
$this->openapi->set("components|parameters|include|required", false);
|
4041
|
|
-
|
|
4063
|
+
|
4042
|
4064
|
$this->openapi->set("components|parameters|exclude|name", "exclude");
|
4043
|
4065
|
$this->openapi->set("components|parameters|exclude|in", "query");
|
4044
|
4066
|
$this->openapi->set("components|parameters|exclude|schema|type", "string");
|
4045
|
4067
|
$this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
|
4046
|
4068
|
$this->openapi->set("components|parameters|exclude|required", false);
|
4047
|
|
-
|
|
4069
|
+
|
4048
|
4070
|
$this->openapi->set("components|parameters|order|name", "order");
|
4049
|
4071
|
$this->openapi->set("components|parameters|order|in", "query");
|
4050
|
4072
|
$this->openapi->set("components|parameters|order|schema|type", "array");
|
|
@@ -5411,19 +5433,18 @@ class Api
|
5411
|
5433
|
} catch (\Throwable $e) {
|
5412
|
5434
|
if ($e instanceof \PDOException) {
|
5413
|
5435
|
if (strpos(strtolower($e->getMessage()), 'duplicate') !== false) {
|
5414
|
|
- return $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
|
5415
|
|
- }
|
5416
|
|
- if (strpos(strtolower($e->getMessage()), 'default value') !== false) {
|
5417
|
|
- return $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
|
5418
|
|
- }
|
5419
|
|
- if (strpos(strtolower($e->getMessage()), 'allow nulls') !== false) {
|
5420
|
|
- return $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
|
5421
|
|
- }
|
5422
|
|
- if (strpos(strtolower($e->getMessage()), 'constraint') !== false) {
|
5423
|
|
- return $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
|
|
5436
|
+ $response = $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
|
|
5437
|
+ } elseif (strpos(strtolower($e->getMessage()), 'default value') !== false) {
|
|
5438
|
+ $response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
|
|
5439
|
+ } elseif (strpos(strtolower($e->getMessage()), 'allow nulls') !== false) {
|
|
5440
|
+ $response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
|
|
5441
|
+ } elseif (strpos(strtolower($e->getMessage()), 'constraint') !== false) {
|
|
5442
|
+ $response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
|
5424
|
5443
|
}
|
5425
|
5444
|
}
|
5426
|
|
- $response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
|
|
5445
|
+ if (!$response) {
|
|
5446
|
+ $response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
|
|
5447
|
+ }
|
5427
|
5448
|
if ($this->debug) {
|
5428
|
5449
|
$response->addHeader('X-Exception-Message', $e->getMessage());
|
5429
|
5450
|
$response->addHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());
|