Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/Support/Generator/TypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,29 @@ public function transform(Type $type): OpenApiType
$items = array_map($this->transform(...), $otherTypes->values()->toArray()); // @phpstan-ignore argument.type
$literalSchemas = [];

if ($stringLiterals->count()) {
$items[] = $literalSchemas[] = (new StringType)->enum(
$stringLiterals->map->value->unique()->values()->toArray() // @phpstan-ignore property.notFound
);
if ($stringLiteralsCount = $stringLiterals->count()) {
if ($stringLiteralsCount > 1) {
$items[] = $literalSchemas[] = (new StringType)->enum(
$stringLiterals->map->value->unique()->values()->toArray() // @phpstan-ignore property.notFound
);
} else {
$items[] = (new StringType)->const($stringLiterals->first()->value);
}
}

if ($integerLiterals->count()) {
$items[] = $literalSchemas[] = (new IntegerType)->enum(
$integerLiterals->map->value->unique()->values()->toArray() // @phpstan-ignore property.notFound
);
if ($integerLiteralsCount = $integerLiterals->count()) {
if ($integerLiteralsCount > 1) {
$items[] = $literalSchemas[] = (new IntegerType)->enum(
$integerLiterals->map->value->unique()->values()->toArray() // @phpstan-ignore property.notFound
);
} else {
$items[] = (new IntegerType)->const($integerLiterals->first()->value);
}
}

// In case $otherTypes consist just of null and there is string or integer literals, make type nullable
$otherTypesIsNullable = count($otherTypes) === 1 && collect($otherTypes)->contains(fn ($t) => $t instanceof \Dedoc\Scramble\Support\Type\NullType);
if ($otherTypesIsNullable && ($stringLiterals->count() || $integerLiterals->count())) {
if ($otherTypesIsNullable && count($literalSchemas)) {
$items = array_map(fn ($s) => $s->nullable(true), $literalSchemas);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/InferExtensions/JsonResourceExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ function JsonResourceExtensionTest_analyze(Infer $infer, OpenApiContext $context
'anyOf' => [
[
'type' => 'string',
'enum' => ['foo'],
'const' => 'foo',
],
[
'type' => 'integer',
'enum' => [42],
'const' => 42,
],
],
],
Expand Down Expand Up @@ -103,8 +103,8 @@ public function toArray(Request $request)
'properties' => [
'property' => [
'anyOf' => [
['type' => 'string', 'enum' => ['foo']],
['type' => 'integer', 'enum' => [123]],
['type' => 'string', 'const' => 'foo'],
['type' => 'integer', 'const' => 123],
],
],
],
Expand Down
10 changes: 2 additions & 8 deletions tests/ValidationRulesDocumentingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,7 @@ function validationRulesToDocumentationWithDeep($rulesToParameters)

expect($params[0]->toArray()['schema'])->toBe([
'type' => 'string',
'enum' => [
'draft',
'archived',
],
'enum' => ['draft', 'archived'],
]);
});

Expand All @@ -491,10 +488,7 @@ function validationRulesToDocumentationWithDeep($rulesToParameters)

expect($params[0]->toArray()['schema'])->toBe([
'type' => 'string',
'enum' => [
'published',
'archived',
],
'enum' => ['published', 'archived'],
]);
});
}
Expand Down