mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-07-27 19:51:38 +00:00
Removed MCP Schema Factory decorator as fix was merge and the upstream version actually contained more fixes
Some checks are pending
Build assets artifact / Build assets artifact (push) Waiting to run
Docker Image Build / build (linux/amd64, amd64, ubuntu-latest) (push) Waiting to run
Docker Image Build / build (linux/arm/v7, armv7, ubuntu-24.04-arm) (push) Waiting to run
Docker Image Build / build (linux/arm64, arm64, ubuntu-24.04-arm) (push) Waiting to run
Docker Image Build / merge (push) Blocked by required conditions
Docker Image Build (FrankenPHP) / build (linux/amd64, amd64, ubuntu-latest) (push) Waiting to run
Docker Image Build (FrankenPHP) / build (linux/arm/v7, armv7, ubuntu-24.04-arm) (push) Waiting to run
Docker Image Build (FrankenPHP) / build (linux/arm64, arm64, ubuntu-24.04-arm) (push) Waiting to run
Docker Image Build (FrankenPHP) / merge (push) Blocked by required conditions
Static analysis / Static analysis (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, sqlite) (push) Waiting to run
Some checks are pending
Build assets artifact / Build assets artifact (push) Waiting to run
Docker Image Build / build (linux/amd64, amd64, ubuntu-latest) (push) Waiting to run
Docker Image Build / build (linux/arm/v7, armv7, ubuntu-24.04-arm) (push) Waiting to run
Docker Image Build / build (linux/arm64, arm64, ubuntu-24.04-arm) (push) Waiting to run
Docker Image Build / merge (push) Blocked by required conditions
Docker Image Build (FrankenPHP) / build (linux/amd64, amd64, ubuntu-latest) (push) Waiting to run
Docker Image Build (FrankenPHP) / build (linux/arm/v7, armv7, ubuntu-24.04-arm) (push) Waiting to run
Docker Image Build (FrankenPHP) / build (linux/arm64, arm64, ubuntu-24.04-arm) (push) Waiting to run
Docker Image Build (FrankenPHP) / merge (push) Blocked by required conditions
Static analysis / Static analysis (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, mysql) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, postgres) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, sqlite) (push) Waiting to run
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, sqlite) (push) Waiting to run
This commit is contained in:
parent
ef690fe37f
commit
51ce453d1a
1 changed files with 0 additions and 162 deletions
|
|
@ -1,162 +0,0 @@
|
||||||
<?php
|
|
||||||
/*
|
|
||||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as published
|
|
||||||
* by the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace App\Mcp\JsonSchema;
|
|
||||||
|
|
||||||
use ApiPlatform\JsonSchema\Schema;
|
|
||||||
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
|
|
||||||
use ApiPlatform\Metadata\Operation;
|
|
||||||
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
|
|
||||||
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
|
|
||||||
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overwrite the default JSON Schema factory to resolve $ref and allOf into a flat schema.
|
|
||||||
* This is a workaround until https://github.com/api-platform/core/pull/7962 is merged
|
|
||||||
*/
|
|
||||||
#[AsAlias('api_platform.mcp.json_schema.schema_factory')]
|
|
||||||
readonly class FixedSchemaFactory implements SchemaFactoryInterface
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
private readonly SchemaFactoryInterface $decorated,
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
|
|
||||||
{
|
|
||||||
$schema = $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
|
|
||||||
|
|
||||||
$definitions = [];
|
|
||||||
foreach ($schema->getDefinitions() as $key => $definition) {
|
|
||||||
$definitions[$key] = $definition instanceof \ArrayObject ? $definition->getArrayCopy() : (array) $definition;
|
|
||||||
}
|
|
||||||
|
|
||||||
$rootKey = $schema->getRootDefinitionKey();
|
|
||||||
if (null !== $rootKey) {
|
|
||||||
$root = $definitions[$rootKey] ?? [];
|
|
||||||
} else {
|
|
||||||
// Collection schemas (and others) put allOf/type directly on the root
|
|
||||||
$root = $schema->getArrayCopy(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
$flat = self::resolveNode($root, $definitions);
|
|
||||||
|
|
||||||
$flatSchema = new Schema(Schema::VERSION_JSON_SCHEMA);
|
|
||||||
unset($flatSchema['$schema']);
|
|
||||||
foreach ($flat as $key => $value) {
|
|
||||||
$flatSchema[$key] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $flatSchema;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively resolve $ref, allOf, and nested structures into a flat schema node.
|
|
||||||
*
|
|
||||||
* @param array $resolving Tracks the current $ref resolution chain to detect circular references
|
|
||||||
*/
|
|
||||||
public static function resolveNode(array|\ArrayObject $node, array $definitions, array &$resolving = []): array
|
|
||||||
{
|
|
||||||
if ($node instanceof \ArrayObject) {
|
|
||||||
$node = $node->getArrayCopy();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($node['$ref'])) {
|
|
||||||
$refKey = str_replace('#/definitions/', '', $node['$ref']);
|
|
||||||
if (!isset($definitions[$refKey]) || isset($resolving[$refKey])) {
|
|
||||||
return ['type' => 'object'];
|
|
||||||
}
|
|
||||||
$resolving[$refKey] = true;
|
|
||||||
$resolved = self::resolveNode($definitions[$refKey], $definitions, $resolving);
|
|
||||||
unset($resolving[$refKey]);
|
|
||||||
|
|
||||||
return $resolved;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($node['allOf'])) {
|
|
||||||
$merged = ['type' => 'object', 'properties' => []];
|
|
||||||
$requiredSets = [];
|
|
||||||
foreach ($node['allOf'] as $entry) {
|
|
||||||
$resolved = self::resolveNode($entry, $definitions, $resolving);
|
|
||||||
if (isset($resolved['properties'])) {
|
|
||||||
foreach ($resolved['properties'] as $k => $v) {
|
|
||||||
$merged['properties'][$k] = $v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isset($resolved['required'])) {
|
|
||||||
$requiredSets[] = $resolved['required'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($requiredSets) {
|
|
||||||
$merged['required'] = array_merge(...$requiredSets);
|
|
||||||
}
|
|
||||||
if ([] === $merged['properties']) {
|
|
||||||
unset($merged['properties']);
|
|
||||||
}
|
|
||||||
if (isset($node['description'])) {
|
|
||||||
$merged['description'] = $node['description'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::resolveDeep($merged, $definitions, $resolving);
|
|
||||||
}
|
|
||||||
|
|
||||||
// oneOf/anyOf nodes must not receive a type fallback — their type is expressed
|
|
||||||
// through the sub-schemas. Adding 'type: object' here would break schemas like
|
|
||||||
// HydraItemBaseSchema's @context, which is oneOf: [string, object].
|
|
||||||
if (isset($node['oneOf']) || isset($node['anyOf'])) {
|
|
||||||
return self::resolveDeep($node, $definitions, $resolving);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isset($node['type'])) {
|
|
||||||
$node['type'] = 'object';
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::resolveDeep($node, $definitions, $resolving);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively resolve nested properties and array items.
|
|
||||||
*/
|
|
||||||
private static function resolveDeep(array $node, array $definitions, array &$resolving): array
|
|
||||||
{
|
|
||||||
if (isset($node['items'])) {
|
|
||||||
$node['items'] = self::resolveNode(
|
|
||||||
$node['items'] instanceof \ArrayObject ? $node['items']->getArrayCopy() : $node['items'],
|
|
||||||
$definitions,
|
|
||||||
$resolving,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($node['properties']) && \is_array($node['properties'])) {
|
|
||||||
foreach ($node['properties'] as $propName => $propSchema) {
|
|
||||||
$node['properties'][$propName] = self::resolveNode(
|
|
||||||
$propSchema instanceof \ArrayObject ? $propSchema->getArrayCopy() : $propSchema,
|
|
||||||
$definitions,
|
|
||||||
$resolving,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue