From 299fea39f5c28be00147619e7d7c6632bfcec5c0 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 12 Dec 2023 18:04:43 +0100 Subject: [PATCH] feat: Make boolean quirk an enum Signed-off-by: jld3103 --- src/OpenApiType.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/OpenApiType.php b/src/OpenApiType.php index 6b8dea8..7ba6e2d 100644 --- a/src/OpenApiType.php +++ b/src/OpenApiType.php @@ -68,13 +68,25 @@ public function toArray(string $openapiVersion, bool $isParameter = false): arra ] : [], ); } + + $type = $this->type; + $defaultValue = $this->defaultValue; + $enum = $this->enum; + if ($isParameter && $type == "boolean") { + $type = "integer"; + $enum = [0, 1]; + if ($this->hasDefaultValue) { + $defaultValue = $defaultValue === true ? 1 : 0; + } + } + $values = array_merge( $this->ref != null ? ["\$ref" => $this->ref] : [], - $this->type != null ? ["type" => $isParameter && $this->type == "boolean" ? "integer" : $this->type] : [], + $type != null ? ["type" => $type] : [], $this->format != null ? ["format" => $this->format] : [], $this->nullable ? ["nullable" => true] : [], - $this->hasDefaultValue && $this->defaultValue !== null ? ["default" => $isParameter && $this->type == "boolean" ? $this->defaultValue === true ? 1 : 0 : $this->defaultValue] : [], - $this->enum != null ? ["enum" => $this->enum] : [], + $this->hasDefaultValue && $defaultValue !== null ? ["default" => $defaultValue] : [], + $enum != null ? ["enum" => $enum] : [], $this->description != null && $this->description != "" && !$isParameter ? ["description" => $this->description] : [], $this->items != null ? ["items" => $this->items->toArray($openapiVersion)] : [], $this->minLength !== null ? ["minLength" => $this->minLength] : [], @@ -185,7 +197,7 @@ public static function resolve(string $context, array $definitions, ParamTagValu $values = []; /** @var ConstTypeNode $type */ foreach ($node->types as $type) { - $values[] = (int) $type->constExpr->value; + $values[] = (int)$type->constExpr->value; } if (count(array_filter($values, fn (string $value) => $value == '')) > 0) { @@ -250,7 +262,7 @@ enum: [$node->constExpr->value], return new OpenApiType( type: "integer", format: "int64", - enum: [(int) $node->constExpr->value], + enum: [(int)$node->constExpr->value], ); }