From 137c338719972f0f70661f548d63c925247ab2f1 Mon Sep 17 00:00:00 2001 From: lexx Date: Thu, 14 Mar 2024 23:01:21 +0200 Subject: [PATCH] fixed json schema recursion issues --- src/JsonSchema/Definition.php | 31 +++++++++++++++++++------ src/JsonSchema/Property/RefProperty.php | 2 -- src/Schema/Ui/File.php | 1 - src/Schema/Ui/Reference.php | 1 - 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/JsonSchema/Definition.php b/src/JsonSchema/Definition.php index 5807615..341e4ea 100644 --- a/src/JsonSchema/Definition.php +++ b/src/JsonSchema/Definition.php @@ -51,7 +51,7 @@ class Definition private static function getRefPath(string $schemaName): string { - return '#/$defs/' . $schemaName ; + return '#/$defs/' . $schemaName; } @@ -78,13 +78,30 @@ class Definition } if ($property instanceof RefProperty) { - $newProperty[$property->type->value] = $property->_ref->map(function (string $ref) { - return [ - '$ref' => self::getRefPath($ref) - ]; - }); + if ($property->maxItems->getOrElse(null) === 1) { + $newProperty["type"] = "object"; + if ($property->_ref->count() == 1) { + $newProperty['$ref'] = $property->_ref->map(fn($ref)=>self::getRefPath($ref))->first(); + } else { + $newProperty["oneOf"] = $property->_ref->map(function (string $ref) { + return [ + '$ref' => self::getRefPath($ref) + ]; + }); + } + } else { + $newProperty["type"] = "array"; + if ($property->_ref->count() == 1) { + $newProperty["items"]['$ref'] = $property->_ref->map(fn($ref)=>self::getRefPath($ref))->first(); + } else { + $newProperty["items"]["anyOf"] = $property->_ref->map(function (string $ref) { + return [ + '$ref' => self::getRefPath($ref) + ]; + }); + } - unset($newProperty["type"]); + } } $newProperties[$property->id] = $newProperty; diff --git a/src/JsonSchema/Property/RefProperty.php b/src/JsonSchema/Property/RefProperty.php index a45aae9..1692a45 100644 --- a/src/JsonSchema/Property/RefProperty.php +++ b/src/JsonSchema/Property/RefProperty.php @@ -8,7 +8,6 @@ use PhpOption\Option; class RefProperty implements Property { /** - * @param RefType $type * @param string $id * @param Option $title * @param Option $description @@ -18,7 +17,6 @@ class RefProperty implements Property * @param Option $comment */ public function __construct( - public RefType $type, public string $id, public Option $title, public Option $description, diff --git a/src/Schema/Ui/File.php b/src/Schema/Ui/File.php index 8749415..41fcb90 100644 --- a/src/Schema/Ui/File.php +++ b/src/Schema/Ui/File.php @@ -60,7 +60,6 @@ class File implements FieldInterface, MinMaxInterface public function toJsonSchema(): Property { return new RefProperty( - type: RefType::ANY_OF, id: $this->name, title: Option::fromValue($this->label), description: Option::fromValue($this->help, ""), diff --git a/src/Schema/Ui/Reference.php b/src/Schema/Ui/Reference.php index 594bcfb..861e188 100644 --- a/src/Schema/Ui/Reference.php +++ b/src/Schema/Ui/Reference.php @@ -66,7 +66,6 @@ class Reference implements FieldInterface, MinMaxInterface public function toJsonSchema(): Property { return new RefProperty( - type: RefType::ANY_OF, id: $this->name, title: Option::fromValue($this->label), description: Option::fromValue($this->help, ""),