Skip to content

Commit 7a32d6a

Browse files
committed
fix: make JSON-LD specific properties non-readOnly and make them required only in output schema
1 parent 016ccac commit 7a32d6a

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

src/Hydra/JsonSchema/SchemaFactory.php

Lines changed: 12 additions & 14 deletions
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
[
@@ -87,28 +85,28 @@ public function buildSchema(string $className, string $format = 'jsonld', string
8785
}
8886
}
8987

90-
if ('input' === $type) {
91-
return $schema;
92-
}
93-
9488
$definitions = $schema->getDefinitions();
9589
if ($key = $schema->getRootDefinitionKey()) {
9690
$definitions[$key]['properties'] = self::BASE_ROOT_PROPS + ($definitions[$key]['properties'] ?? []);
97-
foreach (array_keys(self::BASE_ROOT_PROPS) as $property) {
98-
$definitions[$key]['required'] ??= [];
99-
if (!\in_array($property, $definitions[$key]['required'], true)) {
100-
$definitions[$key]['required'][] = $property;
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+
}
10197
}
10298
}
10399

104100
return $schema;
105101
}
106102
if ($key = $schema->getItemsDefinitionKey()) {
107103
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
108-
foreach (array_keys(self::BASE_PROPS) as $property) {
109-
$definitions[$key]['required'] ??= [];
110-
if (!\in_array($property, $definitions[$key]['required'], true)) {
111-
$definitions[$key]['required'][] = $property;
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+
}
112110
}
113111
}
114112
}

tests/Hydra/JsonSchema/SchemaFactoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ public function testHasRootDefinitionKeyBuildSchema(): void
101101
$this->assertArrayHasKey('@context', $properties);
102102
$this->assertEquals(
103103
[
104-
'readOnly' => true,
105104
'oneOf' => [
106105
['type' => 'string'],
107106
[

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ public function testExecuteWithJsonldTypeInput(): void
7676
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_post', '--format' => 'jsonld', '--type' => 'input']);
7777
$result = $this->tester->getDisplay();
7878

79-
$this->assertStringNotContainsString('@id', $result);
80-
$this->assertStringNotContainsString('@context', $result);
81-
$this->assertStringNotContainsString('@type', $result);
79+
$this->assertStringContainsString('@id', $result);
80+
$this->assertStringContainsString('@context', $result);
81+
$this->assertStringContainsString('@type', $result);
8282
}
8383

8484
/**

0 commit comments

Comments
 (0)