Skip to content

Commit

Permalink
fix: make JSON-LD specific properties non-readOnly and make them requ…
Browse files Browse the repository at this point in the history
…ired only in output schema
  • Loading branch information
ttskch committed Jul 18, 2024
1 parent 016ccac commit 4a2933f
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
{
private const BASE_PROP = [
'readOnly' => true,
'type' => 'string',
];
private const BASE_PROPS = [
Expand All @@ -36,7 +35,6 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
];
private const BASE_ROOT_PROPS = [
'@context' => [
'readOnly' => true,
'oneOf' => [
['type' => 'string'],
[
Expand Down Expand Up @@ -87,28 +85,28 @@ public function buildSchema(string $className, string $format = 'jsonld', string
}
}

if ('input' === $type) {
return $schema;
}

$definitions = $schema->getDefinitions();
if ($key = $schema->getRootDefinitionKey()) {
$definitions[$key]['properties'] = self::BASE_ROOT_PROPS + ($definitions[$key]['properties'] ?? []);
foreach (array_keys(self::BASE_ROOT_PROPS) as $property) {
$definitions[$key]['required'] ??= [];
if (!\in_array($property, $definitions[$key]['required'], true)) {
$definitions[$key]['required'][] = $property;
if (Schema::TYPE_OUTPUT === $type) {
foreach (array_keys(self::BASE_ROOT_PROPS) as $property) {
$definitions[$key]['required'] ??= [];
if (!\in_array($property, $definitions[$key]['required'], true)) {
$definitions[$key]['required'][] = $property;
}
}
}

return $schema;
}
if ($key = $schema->getItemsDefinitionKey()) {
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
foreach (array_keys(self::BASE_PROPS) as $property) {
$definitions[$key]['required'] ??= [];
if (!\in_array($property, $definitions[$key]['required'], true)) {
$definitions[$key]['required'][] = $property;
if (Schema::TYPE_OUTPUT === $type) {
foreach (array_keys(self::BASE_PROPS) as $property) {
$definitions[$key]['required'] ??= [];
if (!\in_array($property, $definitions[$key]['required'], true)) {
$definitions[$key]['required'][] = $property;
}
}
}
}
Expand Down

0 comments on commit 4a2933f

Please sign in to comment.