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 7a32d6a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 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
1 change: 0 additions & 1 deletion tests/Hydra/JsonSchema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function testHasRootDefinitionKeyBuildSchema(): void
$this->assertArrayHasKey('@context', $properties);
$this->assertEquals(
[
'readOnly' => true,
'oneOf' => [
['type' => 'string'],
[
Expand Down
6 changes: 3 additions & 3 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public function testExecuteWithJsonldTypeInput(): void
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_post', '--format' => 'jsonld', '--type' => 'input']);
$result = $this->tester->getDisplay();

$this->assertStringNotContainsString('@id', $result);
$this->assertStringNotContainsString('@context', $result);
$this->assertStringNotContainsString('@type', $result);
$this->assertStringContainsString('@id', $result);
$this->assertStringContainsString('@context', $result);
$this->assertStringContainsString('@type', $result);
}

/**
Expand Down

0 comments on commit 7a32d6a

Please sign in to comment.