Skip to content

Commit 4d1c2b8

Browse files
committed
feat: separate the JSON-LD schema for input/output and fix the types for JSON-LD specific properties
1 parent 953ec19 commit 4d1c2b8

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/Hydra/JsonSchema/SchemaFactory.php

+27-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
2828
{
2929
private const BASE_PROP = [
30-
'readOnly' => true,
3130
'type' => 'string',
3231
];
3332
private const BASE_PROPS = [
@@ -36,7 +35,6 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
3635
];
3736
private const BASE_ROOT_PROPS = [
3837
'@context' => [
39-
'readOnly' => true,
4038
'oneOf' => [
4139
['type' => 'string'],
4240
[
@@ -74,18 +72,43 @@ public function buildSchema(string $className, string $format = 'jsonld', string
7472
return $schema;
7573
}
7674

77-
if ('input' === $type) {
78-
return $schema;
75+
if (($key = $schema->getRootDefinitionKey() ?? $schema->getItemsDefinitionKey()) !== null) {
76+
$postfix = '.'.$type;
77+
$definitions = $schema->getDefinitions();
78+
$definitions[$key.$postfix] = $definitions[$key];
79+
unset($definitions[$key]);
80+
81+
if (($schema['type'] ?? '') === 'array') {
82+
$schema['items']['$ref'] .= $postfix;
83+
} else {
84+
$schema['$ref'] .= $postfix;
85+
}
7986
}
8087

8188
$definitions = $schema->getDefinitions();
8289
if ($key = $schema->getRootDefinitionKey()) {
8390
$definitions[$key]['properties'] = self::BASE_ROOT_PROPS + ($definitions[$key]['properties'] ?? []);
91+
if (Schema::TYPE_OUTPUT === $type) {
92+
foreach (array_keys(self::BASE_ROOT_PROPS) as $property) {
93+
$definitions[$key]['required'] ??= [];
94+
if (!\in_array($property, $definitions[$key]['required'], true)) {
95+
$definitions[$key]['required'][] = $property;
96+
}
97+
}
98+
}
8499

85100
return $schema;
86101
}
87102
if ($key = $schema->getItemsDefinitionKey()) {
88103
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
104+
if (Schema::TYPE_OUTPUT === $type) {
105+
foreach (array_keys(self::BASE_PROPS) as $property) {
106+
$definitions[$key]['required'] ??= [];
107+
if (!\in_array($property, $definitions[$key]['required'], true)) {
108+
$definitions[$key]['required'][] = $property;
109+
}
110+
}
111+
}
89112
}
90113

91114
if (($schema['type'] ?? '') === 'array') {

0 commit comments

Comments
 (0)