Skip to content

Commit ff50430

Browse files
Merge pull request #11 from netgen/NGSTACK-1017-fix-jsonld-post-schema-enritchment
Ngstack 1017 fix jsonld post schema enritchment
2 parents 8534b34 + b7032f4 commit ff50430

4 files changed

Lines changed: 73 additions & 41 deletions

File tree

.github/workflows/static_analysis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ jobs:
2929
- run: composer global config --no-plugins allow-plugins.symfony/flex true
3030
- run: composer global require --no-scripts symfony/flex
3131

32-
- run: composer config extra.symfony.require ~7.3.0
32+
- run: composer config extra.symfony.require ~7.4.0
3333

34-
- run: composer update --prefer-dist
34+
- run: composer update --prefer-dist --with-all-dependencies
3535

36-
- run: composer ${{ matrix.script }}
36+
- run: composer ${{ matrix.script }}

composer.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
"minimum-stability": "beta",
2323
"require": {
2424
"php": ">=8.4",
25-
"symfony/routing": "^7.3 || ^8.0",
26-
"symfony/config": "^7.3 || ^8.0",
27-
"symfony/dependency-injection": "^7.3 || ^8.0",
28-
"symfony/event-dispatcher": "^7.3 || ^8.0",
29-
"symfony/http-foundation": "^7.3 || ^8.0",
30-
"symfony/http-kernel": "^7.3 || ^8.0",
31-
"symfony/property-access": "^7.3 || ^8.0",
32-
"symfony/framework-bundle": "^7.3 || ^8.0",
33-
"symfony/console": "^7.3 || ^8.0",
34-
"symfony/security-core": "^7.3 || ^8.0",
35-
"symfony/security-http": "^7.3 || ^8.0",
36-
"symfony/yaml": "^7.3 || ^8.0",
25+
"symfony/routing": "^7.4.13 || ^8.0.13",
26+
"symfony/http-foundation": "^7.4.13 || ^8.0.13",
27+
"symfony/security-http": "^7.4.13 || ^8.0.13",
28+
"symfony/yaml": "^7.4.12 || ^8.0.12",
29+
"symfony/config": "^7.4 || ^8.0",
30+
"symfony/dependency-injection": "^7.4 || ^8.0",
31+
"symfony/event-dispatcher": "^7.4 || ^8.0",
32+
"symfony/http-kernel": "^7.4 || ^8.0",
33+
"symfony/property-access": "^7.4 || ^8.0",
34+
"symfony/framework-bundle": "^7.4 || ^8.0",
35+
"symfony/console": "^7.4 || ^8.0",
36+
"symfony/security-core": "^7.4 || ^8.0",
3737
"api-platform/symfony": "^4.2",
3838
"api-platform/doctrine-orm": "^4.2",
3939
"lexik/jwt-authentication-bundle": "^3.1",

src/ApiPlatform/JsonSchema/SchemaFactoryDecorator.php

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,52 +58,79 @@ public function buildSchema(string $className, string $format = 'json', string $
5858
/** @param ArrayObject<string, mixed> $definitions */
5959
private function ensureJsonldInputPropertyForInputSchemas(string $reference, string $schemaPrefix, ArrayObject $definitions): void
6060
{
61-
$definitionName = str_replace($schemaPrefix, '', $reference);
61+
foreach (
62+
$this->collectReferences(
63+
$definitions[
64+
$this->stripSchemaPrefix($schemaPrefix, $reference)
65+
]['properties'] ?? [],
66+
$schemaPrefix,
67+
) as $definitionKey) {
68+
$this->addJsonldInputProperty($definitionKey, $definitions);
69+
}
70+
}
6271

63-
foreach ($definitions[$definitionName]['properties'] ?? [] as $property) {
64-
if (isset($property['type'])) {
72+
/**
73+
* @param array<string, ArrayObject<string, mixed>> $properties
74+
*
75+
* @return iterable<int, string>
76+
*/
77+
private function collectReferences(array $properties, string $schemaPrefix): iterable
78+
{
79+
foreach ($properties as $property) {
80+
if (
81+
isset($property['type'])
82+
&& !isset($property['items'])
83+
) {
6584
continue;
6685
}
6786

6887
if (isset($property['$ref'])) {
69-
$this->addJsonldInputProperty(
70-
$definitions,
71-
$schemaPrefix,
72-
$property['$ref'],
73-
);
88+
yield $this->stripSchemaPrefix($schemaPrefix, $property['$ref']);
89+
90+
continue;
91+
}
7492

75-
break;
93+
if (isset($property['items']['$ref'])) {
94+
yield $this->stripSchemaPrefix($schemaPrefix, $property['items']['$ref']);
95+
96+
continue;
7697
}
7798

7899
foreach (self::SCHEMA_LOGICAL_OPERATORS as $operator) {
79-
if (!isset($property[$operator])) {
80-
continue;
81-
}
100+
if (isset($property[$operator])) {
101+
foreach ($property[$operator] as $subschema) {
102+
if (!isset($subschema['$ref'])) {
103+
continue;
104+
}
82105

83-
foreach ($property[$operator] as $subschema) {
84-
if (!isset($subschema['$ref'])) {
85-
continue;
106+
yield $this->stripSchemaPrefix($schemaPrefix, $subschema['$ref']);
86107
}
108+
}
87109

88-
$this->addJsonldInputProperty(
89-
$definitions,
90-
$schemaPrefix,
91-
$subschema['$ref'],
92-
);
110+
if (isset($property['items'][$operator])) {
111+
foreach ($property['items'][$operator] as $subschema) {
112+
if (!isset($subschema['$ref'])) {
113+
continue;
114+
}
115+
116+
yield $this->stripSchemaPrefix($schemaPrefix, $subschema['$ref']);
117+
}
93118
}
94119
}
95120
}
96121
}
97122

98123
/** @param ArrayObject<string, mixed> $definitions */
99124
private function addJsonldInputProperty(
125+
string $definitionKey,
100126
ArrayObject $definitions,
101-
string $schemaPrefix,
102-
string $ref,
103127
): void {
104-
$definitionKey = str_replace($schemaPrefix, '', $ref);
105-
106128
$definitions[$definitionKey]['properties'][self::JSONLD_INPUT_OBJECT_PROPERTY_NAME]
107129
??= self::JSONLD_INPUT_OBJECT_PROPERTY;
108130
}
131+
132+
private function stripSchemaPrefix(string $schemaPrefix, string $reference): string
133+
{
134+
return str_replace($schemaPrefix, '', $reference);
135+
}
109136
}

src/Service/IriTemplatesService.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Symfony\Component\Routing\Route;
1414
use Symfony\Component\Routing\RouterInterface;
1515

16+
use function is_string;
1617
use function preg_replace;
1718

1819
final class IriTemplatesService
@@ -52,12 +53,16 @@ public function getIriTemplatesData(): array
5253
/** @var string $operationName */
5354
$operationName = $operation->getName();
5455
$route = $routeCollection->get($operationName);
56+
$shortName = $resourceMetadata->getShortName();
5557

56-
if (!$route instanceof Route) {
58+
if (
59+
!$route instanceof Route
60+
|| !is_string($shortName)
61+
) {
5762
continue;
5863
}
5964

60-
$iriTemplates[$resourceMetadata->getShortName()] = $this->sanitizePath($route->getPath());
65+
$iriTemplates[$shortName] = $this->sanitizePath($route->getPath());
6166

6267
break;
6368
}

0 commit comments

Comments
 (0)