diff --git a/src/Support/Generator/TypeTransformer.php b/src/Support/Generator/TypeTransformer.php index 53c7bd2b..e3ea3df1 100644 --- a/src/Support/Generator/TypeTransformer.php +++ b/src/Support/Generator/TypeTransformer.php @@ -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); } diff --git a/tests/InferExtensions/JsonResourceExtensionTest.php b/tests/InferExtensions/JsonResourceExtensionTest.php index fd5e9539..4bdd34e9 100644 --- a/tests/InferExtensions/JsonResourceExtensionTest.php +++ b/tests/InferExtensions/JsonResourceExtensionTest.php @@ -56,11 +56,11 @@ function JsonResourceExtensionTest_analyze(Infer $infer, OpenApiContext $context 'anyOf' => [ [ 'type' => 'string', - 'enum' => ['foo'], + 'const' => 'foo', ], [ 'type' => 'integer', - 'enum' => [42], + 'const' => 42, ], ], ], @@ -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], ], ], ], diff --git a/tests/ValidationRulesDocumentingTest.php b/tests/ValidationRulesDocumentingTest.php index e91920d2..5e5aa983 100644 --- a/tests/ValidationRulesDocumentingTest.php +++ b/tests/ValidationRulesDocumentingTest.php @@ -475,10 +475,7 @@ function validationRulesToDocumentationWithDeep($rulesToParameters) expect($params[0]->toArray()['schema'])->toBe([ 'type' => 'string', - 'enum' => [ - 'draft', - 'archived', - ], + 'enum' => ['draft', 'archived'], ]); }); @@ -491,10 +488,7 @@ function validationRulesToDocumentationWithDeep($rulesToParameters) expect($params[0]->toArray()['schema'])->toBe([ 'type' => 'string', - 'enum' => [ - 'published', - 'archived', - ], + 'enum' => ['published', 'archived'], ]); }); }