Skip to content

Commit

Permalink
Merge pull request #45 from nextcloud/feat/boolean-quirk-enum
Browse files Browse the repository at this point in the history
feat: Make boolean quirk an enum
  • Loading branch information
nickvergessen authored Dec 21, 2023
2 parents cc80ec1 + bd9a003 commit cbf8396
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] : [],
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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],
);
}

Expand Down

0 comments on commit cbf8396

Please sign in to comment.