Skip to content

Commit 4ab7890

Browse files
committed
Update to follow OpenAPI specs without warning
1 parent 6e8308c commit 4ab7890

File tree

3 files changed

+108
-17
lines changed

3 files changed

+108
-17
lines changed

src/Definitions/DefinitionGenerator.php

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,25 @@ function generateSchemas(): array {
9797
*/
9898
$column = $conn->getDoctrineColumn($table, $item);
9999

100-
$data = [
101-
'type' => Type::getTypeRegistry()->lookupName($column->getType())
102-
];
100+
$data = $this->convertDBalTypeToSwaggerType(
101+
Type::getTypeRegistry()->lookupName($column->getType())
102+
);
103+
104+
if ($data['type'] == 'string' && ($len = $column->getLength())) {
105+
$data['description'] .= "($len)";
106+
}
103107

104108
$description = $column->getComment();
105109
if (!is_null($description)) {
106-
$data['description'] = $description;
110+
$data['description'] .= ": $description";
107111
}
108112

109113
$default = $column->getDefault();
110114
if (!is_null($default)) {
111115
$data['default'] = $default;
112116
}
117+
118+
$data['nullable'] = ! $column->getNotnull();
113119

114120
$this->addExampleKey($data);
115121

@@ -159,9 +165,12 @@ function generateSchemas(): array {
159165

160166
$definition = [
161167
'type' => 'object',
162-
'required' => $required,
163-
'properties' => $properties,
168+
'properties' => (object) $properties,
164169
];
170+
171+
if ( ! empty($required)) {
172+
$definition['required'] = $required;
173+
}
165174

166175
$schemas[ $this->getModelName($obj) ] = $definition;
167176
}
@@ -220,6 +229,9 @@ private function addExampleKey(array & $property): void {
220229
case 'datetime':
221230
Arr::set($property, 'example', date('Y-m-d H:i:s'));
222231
break;
232+
case 'timestamp':
233+
Arr::set($property, 'example', date('Y-m-d H:i:s'));
234+
break;
223235
case 'string':
224236
Arr::set($property, 'example', 'string');
225237
break;
@@ -236,4 +248,81 @@ private function addExampleKey(array & $property): void {
236248
}
237249
}
238250
}
251+
252+
/**
253+
* @return array array of with 'type' and 'format' as keys
254+
*/
255+
private function convertDBalTypeToSwaggerType(string $type): array {
256+
$lowerType = strtolower($type);
257+
switch ($lowerType) {
258+
case 'bigserial':
259+
case 'bigint':
260+
$property = [
261+
'type' => 'integer',
262+
'format' => 'int64'
263+
];
264+
break;
265+
case 'serial':
266+
case 'integer':
267+
case 'mediumint':
268+
case 'smallint':
269+
case 'tinyint':
270+
case 'tinyint':
271+
case 'year':
272+
$property = ['type' => 'integer'];
273+
break;
274+
case 'float':
275+
$property = [
276+
'type' => 'number',
277+
'format' => 'float'
278+
];
279+
break;
280+
case 'decimal':
281+
case 'double':
282+
case 'real':
283+
$property = [
284+
'type' => 'number',
285+
'format' => 'double'
286+
];
287+
break;
288+
case 'boolean':
289+
$property = ['type' => 'boolean'];
290+
break;
291+
case 'date':
292+
$property = [
293+
'type' => 'string',
294+
'format' => 'date',
295+
];
296+
break;
297+
case 'datetime':
298+
case 'timestamp':
299+
$property = [
300+
'type' => 'string',
301+
'format' => 'date-time',
302+
];
303+
break;
304+
case 'binary':
305+
case 'varbinary':
306+
case 'blob':
307+
$property = [
308+
'type' => 'string',
309+
'format' => 'binary',
310+
];
311+
break;
312+
case 'time':
313+
case 'string':
314+
case 'text':
315+
case 'char':
316+
case 'varchar':
317+
case 'enum':
318+
case 'set':
319+
default:
320+
$property = ['type' => 'string'];
321+
break;
322+
}
323+
324+
$property['description'] = $type;
325+
326+
return $property;
327+
}
239328
}

src/Generator.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,13 @@ public function generate(): array {
105105
$this->hasSecurityDefinitions = true;
106106
}
107107

108+
$basePath = $this->routeFilter ?: $this->fromConfig('api_base_path');
108109
foreach ($applicationRoutes as $route) {
109110
if ($this->isFilteredRoute($route)) {
110111
continue;
111112
}
112113

113-
$uri = Str::replaceFirst($documentation['basePath'], '', $route->uri());
114+
$uri = Str::replaceFirst($basePath, '', $route->uri());
114115
$pathKey = 'paths.' . $uri;
115116

116117
if (!Arr::has($documentation, $pathKey)) {
@@ -143,7 +144,6 @@ private function generateBaseInformation(): array {
143144
'description' => $this->fromConfig('description'),
144145
'version' => $this->fromConfig('version')
145146
],
146-
'basePath' => $this->routeFilter ?: $this->fromConfig('api_base_path'),
147147
'servers' => $this->generateServersList(),
148148
'paths' => [],
149149
'tags' => $this->fromConfig('tags'),
@@ -268,7 +268,6 @@ private function generatePath(DataObjects\Route $route, string $method, ?string
268268
$this->addActionScopes($documentation, $route);
269269
}
270270

271-
272271
if (!Arr::has($documentation, 'tags') || \count(Arr::get($documentation, 'tags')) == 0) {
273272

274273
switch($this->fromConfig('default_tags_generation_strategy')) {
@@ -488,20 +487,23 @@ private function addActionsResponses(array & $information): void {
488487
private function addActionsParameters(array & $information, DataObjects\Route $route, string $method, ?ReflectionMethod $actionInstance): void {
489488
$rules = $this->retrieveFormRules($actionInstance) ?: [];
490489
$parameters = (new Parameters\PathParametersGenerator($route->originalUri()))->getParameters();
490+
$requestBody = [];
491491

492-
$key = 'parameters';
493492
if (\count($rules) > 0) {
494493
$parametersGenerator = $this->getParametersGenerator($rules, $method);
495-
switch ($parametersGenerator->getParameterLocation()) {
496-
case 'body':
497-
$key = 'requestBody';
498-
break;
494+
if ('body' == $parametersGenerator->getParameterLocation()) {
495+
$requestBody = $parametersGenerator->getParameters();
496+
} else {
497+
$parameters = array_merge($parameters, $parametersGenerator->getParameters());
499498
}
500-
$parameters = array_merge($parameters, $parametersGenerator->getParameters());
501499
}
502500

503501
if (\count($parameters) > 0) {
504-
Arr::set($information, $key, $parameters);
502+
Arr::set($information, 'parameters', $parameters);
503+
}
504+
505+
if (\count($requestBody) > 0) {
506+
Arr::set($information, 'requestBody', $requestBody);
505507
}
506508
}
507509

src/Parameters/Traits/GeneratesFromRules.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function getParameterType(array $parameterRules): string {
4242
/**
4343
* Get parameter format
4444
* @param array $parameterRules
45-
* @return string
45+
* @return array
4646
*/
4747
protected function getParameterExtra(string $type, array $parameterRules): array {
4848

0 commit comments

Comments
 (0)