Skip to content

Commit c4f0583

Browse files
committed
Fixed get part details tool
1 parent fc8769e commit c4f0583

2 files changed

Lines changed: 168 additions & 2 deletions

File tree

src/Entity/Parts/Part.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
#[ORM\Index(columns: ['datetime_added', 'name', 'last_modified', 'id', 'needs_review'], name: 'parts_idx_datet_name_last_id_needs')]
8787
#[ORM\Index(columns: ['name'], name: 'parts_idx_name')]
8888
#[ORM\Index(columns: ['ipn'], name: 'parts_idx_ipn')]
89-
#[ORM\Index(columns: ['gtin'], name: 'parts_idx_gtin')]
89+
#[ORM\Index(name: 'parts_idx_gtin', columns: ['gtin'])]
9090
#[ApiResource(
9191
operations: [
9292
new Get(normalizationContext: [
@@ -122,8 +122,12 @@
122122
title: 'Get part details by ID',
123123
description: 'Get detailed information about a specific part by its database ID',
124124
annotations: ['readOnlyHint' => true, 'destructiveHint' => false, 'idempotentHint' => true, 'openWorldHint' => false],
125-
normalizationContext: ['groups' => ['part:read', 'provider_reference:read', 'api:basic:read', 'part_lot:read', 'orderdetail:read', 'pricedetail:read', 'parameter:read', 'attachment:read', 'eda_info:read']],
125+
normalizationContext: [
126+
'groups' => ['part:read', 'provider_reference:read', 'api:basic:read', 'part_lot:read', 'orderdetail:read', 'pricedetail:read', 'parameter:read', 'attachment:read', 'eda_info:read'],
127+
'item_uri_template' => '/api/parts/{id}',
128+
],
126129
input: ElementByIdInput::class,
130+
validate: true,
127131
processor: GetPartByIdProcessor::class
128132
),
129133
],
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace App\Mcp\JsonSchema;
24+
25+
use ApiPlatform\JsonSchema\Schema;
26+
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
27+
use ApiPlatform\Metadata\Operation;
28+
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
29+
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
30+
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
31+
32+
/**
33+
* Overwrite the default JSON Schema factory to resolve $ref and allOf into a flat schema.
34+
* This is a workaround until https://github.com/api-platform/core/pull/7962 is merged
35+
*/
36+
#[AsAlias('api_platform.mcp.json_schema.schema_factory')]
37+
readonly class FixedSchemaFactory implements SchemaFactoryInterface
38+
{
39+
public function __construct(
40+
private readonly SchemaFactoryInterface $decorated,
41+
) {
42+
}
43+
44+
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
45+
{
46+
$schema = $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
47+
48+
$definitions = [];
49+
foreach ($schema->getDefinitions() as $key => $definition) {
50+
$definitions[$key] = $definition instanceof \ArrayObject ? $definition->getArrayCopy() : (array) $definition;
51+
}
52+
53+
$rootKey = $schema->getRootDefinitionKey();
54+
if (null !== $rootKey) {
55+
$root = $definitions[$rootKey] ?? [];
56+
} else {
57+
// Collection schemas (and others) put allOf/type directly on the root
58+
$root = $schema->getArrayCopy(false);
59+
}
60+
61+
$flat = self::resolveNode($root, $definitions);
62+
63+
$flatSchema = new Schema(Schema::VERSION_JSON_SCHEMA);
64+
unset($flatSchema['$schema']);
65+
foreach ($flat as $key => $value) {
66+
$flatSchema[$key] = $value;
67+
}
68+
69+
return $flatSchema;
70+
}
71+
72+
/**
73+
* Recursively resolve $ref, allOf, and nested structures into a flat schema node.
74+
*
75+
* @param array $resolving Tracks the current $ref resolution chain to detect circular references
76+
*/
77+
public static function resolveNode(array|\ArrayObject $node, array $definitions, array &$resolving = []): array
78+
{
79+
if ($node instanceof \ArrayObject) {
80+
$node = $node->getArrayCopy();
81+
}
82+
83+
if (isset($node['$ref'])) {
84+
$refKey = str_replace('#/definitions/', '', $node['$ref']);
85+
if (!isset($definitions[$refKey]) || isset($resolving[$refKey])) {
86+
return ['type' => 'object'];
87+
}
88+
$resolving[$refKey] = true;
89+
$resolved = self::resolveNode($definitions[$refKey], $definitions, $resolving);
90+
unset($resolving[$refKey]);
91+
92+
return $resolved;
93+
}
94+
95+
if (isset($node['allOf'])) {
96+
$merged = ['type' => 'object', 'properties' => []];
97+
$requiredSets = [];
98+
foreach ($node['allOf'] as $entry) {
99+
$resolved = self::resolveNode($entry, $definitions, $resolving);
100+
if (isset($resolved['properties'])) {
101+
foreach ($resolved['properties'] as $k => $v) {
102+
$merged['properties'][$k] = $v;
103+
}
104+
}
105+
if (isset($resolved['required'])) {
106+
$requiredSets[] = $resolved['required'];
107+
}
108+
}
109+
110+
if ($requiredSets) {
111+
$merged['required'] = array_merge(...$requiredSets);
112+
}
113+
if ([] === $merged['properties']) {
114+
unset($merged['properties']);
115+
}
116+
if (isset($node['description'])) {
117+
$merged['description'] = $node['description'];
118+
}
119+
120+
return self::resolveDeep($merged, $definitions, $resolving);
121+
}
122+
123+
// oneOf/anyOf nodes must not receive a type fallback — their type is expressed
124+
// through the sub-schemas. Adding 'type: object' here would break schemas like
125+
// HydraItemBaseSchema's @context, which is oneOf: [string, object].
126+
if (isset($node['oneOf']) || isset($node['anyOf'])) {
127+
return self::resolveDeep($node, $definitions, $resolving);
128+
}
129+
130+
if (!isset($node['type'])) {
131+
$node['type'] = 'object';
132+
}
133+
134+
return self::resolveDeep($node, $definitions, $resolving);
135+
}
136+
137+
/**
138+
* Recursively resolve nested properties and array items.
139+
*/
140+
private static function resolveDeep(array $node, array $definitions, array &$resolving): array
141+
{
142+
if (isset($node['items'])) {
143+
$node['items'] = self::resolveNode(
144+
$node['items'] instanceof \ArrayObject ? $node['items']->getArrayCopy() : $node['items'],
145+
$definitions,
146+
$resolving,
147+
);
148+
}
149+
150+
if (isset($node['properties']) && \is_array($node['properties'])) {
151+
foreach ($node['properties'] as $propName => $propSchema) {
152+
$node['properties'][$propName] = self::resolveNode(
153+
$propSchema instanceof \ArrayObject ? $propSchema->getArrayCopy() : $propSchema,
154+
$definitions,
155+
$resolving,
156+
);
157+
}
158+
}
159+
160+
return $node;
161+
}
162+
}

0 commit comments

Comments
 (0)