fixed json schema recursion issues

This commit is contained in:
2024-03-14 23:01:21 +02:00
parent 842bd71a18
commit 137c338719
4 changed files with 24 additions and 11 deletions
+24 -7
View File
@@ -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;
-2
View File
@@ -8,7 +8,6 @@ use PhpOption\Option;
class RefProperty implements Property
{
/**
* @param RefType $type
* @param string $id
* @param Option<string> $title
* @param Option<string> $description
@@ -18,7 +17,6 @@ class RefProperty implements Property
* @param Option<string> $comment
*/
public function __construct(
public RefType $type,
public string $id,
public Option $title,
public Option $description,
-1
View File
@@ -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, ""),
-1
View File
@@ -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, ""),