From 143f818748e51d615532f1a9e43ef19e613fb2cf Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 6 Dec 2023 13:32:02 +0100 Subject: [PATCH 1/2] fix: Do not convert union types to string Signed-off-by: jld3103 --- src/OpenApiType.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/OpenApiType.php b/src/OpenApiType.php index b86e85e..ecff9ff 100644 --- a/src/OpenApiType.php +++ b/src/OpenApiType.php @@ -53,7 +53,6 @@ public function toArray(string $openapiVersion, bool $isParameter = false): arra $asContentString = $isParameter && ( $this->type == "object" || $this->ref !== null || - $this->oneOf !== null || $this->anyOf !== null || $this->allOf !== null); if ($asContentString) { From dc7b58381cd280d17b0a26caf7b12a8dcf164052 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 6 Dec 2023 13:32:25 +0100 Subject: [PATCH 2/2] fix: Merge enum values from union types Signed-off-by: jld3103 --- src/OpenApiType.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/OpenApiType.php b/src/OpenApiType.php index ecff9ff..0d2cfbf 100644 --- a/src/OpenApiType.php +++ b/src/OpenApiType.php @@ -174,6 +174,7 @@ static function resolve(string $context, array $definitions, ParamTagValueNode|N } $items = array_unique($items, SORT_REGULAR); + $items = self::mergeEnums($items); if (count($items) == 1) { $type = $items[0]; @@ -210,6 +211,28 @@ enum: [$node->constExpr->value], Logger::panic($context, "Unable to resolve OpenAPI type for type '" . get_class($node) . "'"); } + /** + * @param OpenApiType[] $types + */ + private static function mergeEnums(array $types) { + $enums = []; + $nonEnums = []; + + foreach ($types as $type) { + if ($type->enum !== null) { + if (array_key_exists($type->type, $enums)) { + $enums[$type->type] = array_merge($enums[$type->type], $type->enum); + } else { + $enums[$type->type] = $type->enum; + } + } else { + $nonEnums[] = $type; + } + } + + return array_merge($nonEnums, array_map(fn(string $type) => new OpenApiType(type: $type, enum: $enums[$type]), array_keys($enums))); + } + private static function resolveIdentifier(string $context, array $definitions, string $name): OpenApiType { if ($name == "array") { Logger::error($context, "Instead of 'array' use:\n'new stdClass()' for empty objects\n'array' for non-empty objects\n'array' for empty lists\n'array' for lists");