Skip to content

Commit 27f37b2

Browse files
committed
utf8 in component ids
1 parent 5cfdace commit 27f37b2

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/Tqdev/PhpCrudApi/OpenApi/OpenApiRecordsBuilder.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class OpenApiRecordsBuilder
3636
'boolean' => ['type' => 'boolean'],
3737
];
3838

39+
private function normalize(string $value): string
40+
{
41+
return iconv('UTF-8', 'ASCII//TRANSLIT', $value);
42+
}
43+
3944
public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection)
4045
{
4146
$this->openapi = $openapi;
@@ -109,6 +114,7 @@ private function isOperationOnColumnAllowed(string $operation, string $tableName
109114

110115
private function setPath(string $tableName) /*: void*/
111116
{
117+
$normalizedTableName = $this->normalize($tableName);
112118
$table = $this->reflection->getTable($tableName);
113119
$type = $table->getType();
114120
$pk = $table->getPk();
@@ -141,14 +147,14 @@ private function setPath(string $tableName) /*: void*/
141147
$this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
142148
}
143149
if (in_array($operation, ['create', 'update', 'increment'])) {
144-
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . rawurlencode($tableName));
150+
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-$normalizedTableName");
145151
}
146152
$this->openapi->set("paths|$path|$method|tags|0", "$tableName");
147-
$this->openapi->set("paths|$path|$method|operationId", "$operation" . "_" . "$tableName");
153+
$this->openapi->set("paths|$path|$method|operationId", "$operation" . "_" . "$normalizedTableName");
148154
$this->openapi->set("paths|$path|$method|description", "$operation $tableName");
149155
switch ($operation) {
150156
case 'list':
151-
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . rawurlencode($tableName));
157+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-$normalizedTableName");
152158
break;
153159
case 'create':
154160
if ($pk->getType() == 'integer') {
@@ -158,7 +164,7 @@ private function setPath(string $tableName) /*: void*/
158164
}
159165
break;
160166
case 'read':
161-
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . rawurlencode($tableName));
167+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-$normalizedTableName");
162168
break;
163169
case 'update':
164170
case 'delete':
@@ -214,6 +220,7 @@ private function getPattern(ReflectedColumn $column): string
214220

215221
private function setComponentSchema(string $tableName, array $references) /*: void*/
216222
{
223+
$normalizedTableName = $this->normalize($tableName);
217224
$table = $this->reflection->getTable($tableName);
218225
$type = $table->getType();
219226
$pk = $table->getPk();
@@ -235,13 +242,13 @@ private function setComponentSchema(string $tableName, array $references) /*: vo
235242
continue;
236243
}
237244
if ($operation == 'list') {
238-
$this->openapi->set("components|schemas|$operation-$tableName|type", "object");
239-
$this->openapi->set("components|schemas|$operation-$tableName|properties|results|type", "integer");
240-
$this->openapi->set("components|schemas|$operation-$tableName|properties|results|format", "int64");
241-
$this->openapi->set("components|schemas|$operation-$tableName|properties|records|type", "array");
242-
$prefix = "components|schemas|$operation-$tableName|properties|records|items";
245+
$this->openapi->set("components|schemas|$operation-$normalizedTableName|type", "object");
246+
$this->openapi->set("components|schemas|$operation-$normalizedTableName|properties|results|type", "integer");
247+
$this->openapi->set("components|schemas|$operation-$normalizedTableName|properties|results|format", "int64");
248+
$this->openapi->set("components|schemas|$operation-$normalizedTableName|properties|records|type", "array");
249+
$prefix = "components|schemas|$operation-$normalizedTableName|properties|records|items";
243250
} else {
244-
$prefix = "components|schemas|$operation-$tableName";
251+
$prefix = "components|schemas|$operation-$normalizedTableName";
245252
}
246253
$this->openapi->set("$prefix|type", "object");
247254
foreach ($table->getColumnNames() as $columnName) {
@@ -272,6 +279,7 @@ private function setComponentSchema(string $tableName, array $references) /*: vo
272279

273280
private function setComponentResponse(string $tableName) /*: void*/
274281
{
282+
$normalizedTableName = $this->normalize($tableName);
275283
$table = $this->reflection->getTable($tableName);
276284
$type = $table->getType();
277285
$pk = $table->getPk();
@@ -287,16 +295,17 @@ private function setComponentResponse(string $tableName) /*: void*/
287295
continue;
288296
}
289297
if ($operation == 'list') {
290-
$this->openapi->set("components|responses|$operation-$tableName|description", "list of $tableName records");
298+
$this->openapi->set("components|responses|$operation-$normalizedTableName|description", "list of $tableName records");
291299
} else {
292-
$this->openapi->set("components|responses|$operation-$tableName|description", "single $tableName record");
300+
$this->openapi->set("components|responses|$operation-$normalizedTableName|description", "single $tableName record");
293301
}
294-
$this->openapi->set("components|responses|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . rawurlencode($tableName));
302+
$this->openapi->set("components|responses|$operation-$normalizedTableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-$normalizedTableName");
295303
}
296304
}
297305

298306
private function setComponentRequestBody(string $tableName) /*: void*/
299307
{
308+
$normalizedTableName = $this->normalize($tableName);
300309
$table = $this->reflection->getTable($tableName);
301310
$type = $table->getType();
302311
$pk = $table->getPk();
@@ -306,8 +315,8 @@ private function setComponentRequestBody(string $tableName) /*: void*/
306315
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
307316
continue;
308317
}
309-
$this->openapi->set("components|requestBodies|$operation-$tableName|description", "single $tableName record");
310-
$this->openapi->set("components|requestBodies|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . rawurlencode($tableName));
318+
$this->openapi->set("components|requestBodies|$operation-$normalizedTableName|description", "single $tableName record");
319+
$this->openapi->set("components|requestBodies|$operation-$normalizedTableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-$normalizedTableName");
311320
}
312321
}
313322
}

0 commit comments

Comments
 (0)