Skip to content

Commit

Permalink
fix: improvements in name generation and guessing types.
Browse files Browse the repository at this point in the history
  • Loading branch information
ErickJMenezes committed Sep 5, 2022
1 parent c25ee1d commit 937973c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/OpenGen/Adapters/OpenApiV3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,13 @@ protected function addSecurityRequirementParameter(Method $method, OpenApi $open
}
}

protected function mustIncludeParameter(string $in, int|string|array $search): bool
protected function mustIncludeParameter(?string $in, null|int|string|array $search): bool
{
$search = arrayWrap($search);
$search = arrayWrap($search ?? []);

if (is_null($in)) {
return false;
}

if (!isset($this->ignoreParameters[$in])) {
return true;
Expand Down
10 changes: 9 additions & 1 deletion src/OpenGen/Adapters/SwaggerV2Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function adaptParametersToOpenApiV3Format(OpenApi $specification): void
foreach ($operation->parameters as $parameter) {
$parameter = $parameter->getSerializableData();
$schema = new Schema([
'type' => $parameter->type ?? $parameter?->schema?->type,
'type' => $parameter->type ?? $parameter?->schema?->type ?? $this->guessSchemaType($parameter->schema, $parameter->in),
'required' => $parameter->required ?? false,
'description' => $parameter->description ?? null
]);
Expand Down Expand Up @@ -196,4 +196,12 @@ protected function addSecurityRequirementParameter(Method $method, OpenApi $open

parent::addSecurityRequirementParameter($method, $openApi, $securityName);
}

private function guessSchemaType(object $schema, string $in): string
{
if ($in === 'body') {
return count($schema->parameters ?? []) > 1 ? 'object' : 'array';
}
return 'string';
}
}
26 changes: 25 additions & 1 deletion src/OpenGen/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ public static function camelCase(string $value): string
return self::$camelCache[$value];
}

$pieces = explode(' ', str_replace(['-', '_', '/', '\\'], ' ', $value));
$separators = ['-', '_', '/', '\\'];
$pieces = explode(
' ',
str_replace(
$separators,
' ',
self::contains($value, $separators) ? strtolower($value) : $value,
)
);

$newPieces = [];
foreach ($pieces as $piece) {
Expand All @@ -43,4 +51,20 @@ public static function camelCase(string $value): string

return self::$camelCache[$value] = lcfirst(implode('', $newPieces));
}

public static function contains($haystack, $needles, $ignoreCase = false)
{
if ($ignoreCase) {
$haystack = mb_strtolower($haystack);
$needles = array_map('mb_strtolower', (array) $needles);
}

foreach ((array) $needles as $needle) {
if ($needle !== '' && str_contains($haystack, $needle)) {
return true;
}
}

return false;
}
}

0 comments on commit 937973c

Please sign in to comment.