Skip to content

[JSON Schema] Add base Hydra properties to collection items definition #3803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Hydra/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ final class SchemaFactory implements SchemaFactoryInterface
'type' => 'string',
];
private const BASE_PROPS = [
'@context' => self::BASE_PROP,
'@id' => self::BASE_PROP,
'@type' => self::BASE_PROP,
];
private const BASE_ROOT_PROPS = [
'@context' => self::BASE_PROP,
] + self::BASE_PROPS;

private $schemaFactory;

Expand All @@ -59,10 +61,13 @@ public function buildSchema(string $className, string $format = 'jsonld', string

$definitions = $schema->getDefinitions();
if ($key = $schema->getRootDefinitionKey()) {
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
$definitions[$key]['properties'] = self::BASE_ROOT_PROPS + ($definitions[$key]['properties'] ?? []);

return $schema;
}
if ($key = $schema->getItemsDefinitionKey()) {
$definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
}

if (($schema['type'] ?? '') === 'array') {
// hydra:collection
Expand Down
26 changes: 22 additions & 4 deletions src/JsonSchema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,20 @@ public function getRootDefinitionKey(): ?string
return null;
}

// strlen('#/definitions/') = 14
// strlen('#/components/schemas/') = 21
$prefix = self::VERSION_OPENAPI === $this->version ? 21 : 14;
return $this->removeDefinitionKeyPrefix($this['$ref']);
}

return substr($this['$ref'], $prefix);
/**
* Returns the name of the items definition, if defined.
*/
public function getItemsDefinitionKey(): ?string
{
$ref = $this['items']['$ref'] ?? null;
if (null === $ref) {
return null;
}

return $this->removeDefinitionKeyPrefix($ref);
}

/**
Expand All @@ -114,4 +123,13 @@ public function isDefined(): bool
{
return isset($this['$ref']) || isset($this['type']);
}

private function removeDefinitionKeyPrefix(string $definitionKey): string
{
// strlen('#/definitions/') = 14
// strlen('#/components/schemas/') = 21
$prefix = self::VERSION_OPENAPI === $this->version ? 21 : 14;

return substr($definitionKey, $prefix);
}
}
12 changes: 12 additions & 0 deletions tests/Hydra/JsonSchema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function testHasRootDefinitionKeyBuildSchema(): void
$this->assertEquals(Dummy::class.':jsonld', $rootDefinitionKey);
$this->assertArrayHasKey($rootDefinitionKey, $definitions);
$this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]);
$properties = $resultSchema['definitions'][$rootDefinitionKey]['properties'];
$this->assertArrayHasKey('@context', $properties);
$this->assertArrayHasKey('@type', $properties);
$this->assertArrayHasKey('@id', $properties);
}

public function testSchemaTypeBuildSchema(): void
Expand All @@ -89,6 +93,10 @@ public function testSchemaTypeBuildSchema(): void
$this->assertArrayHasKey('hydra:totalItems', $resultSchema['properties']);
$this->assertArrayHasKey('hydra:view', $resultSchema['properties']);
$this->assertArrayHasKey('hydra:search', $resultSchema['properties']);
$properties = $resultSchema['definitions'][Dummy::class.':jsonld']['properties'];
$this->assertArrayNotHasKey('@context', $properties);
$this->assertArrayHasKey('@type', $properties);
$this->assertArrayHasKey('@id', $properties);

$resultSchema = $this->schemaFactory->buildSchema(Dummy::class, 'jsonld', Schema::TYPE_OUTPUT, null, null, null, null, true);

Expand All @@ -98,5 +106,9 @@ public function testSchemaTypeBuildSchema(): void
$this->assertArrayHasKey('hydra:totalItems', $resultSchema['properties']);
$this->assertArrayHasKey('hydra:view', $resultSchema['properties']);
$this->assertArrayHasKey('hydra:search', $resultSchema['properties']);
$properties = $resultSchema['definitions'][Dummy::class.':jsonld']['properties'];
$this->assertArrayNotHasKey('@context', $properties);
$this->assertArrayHasKey('@type', $properties);
$this->assertArrayHasKey('@id', $properties);
}
}
13 changes: 13 additions & 0 deletions tests/JsonSchema/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public function testJsonSchemaVersion(string $version, string $ref): void
$this->assertSame('Foo', $schema->getRootDefinitionKey());
}

/**
* @dataProvider versionProvider
*/
public function testCollectionJsonSchemaVersion(string $version, string $ref): void
{
$schema = new Schema($version);
$schema['items']['$ref'] = $ref;

$this->assertInstanceOf(\ArrayObject::class, $schema);
$this->assertSame($version, $schema->getVersion());
$this->assertSame('Foo', $schema->getItemsDefinitionKey());
}

public function versionProvider(): iterable
{
yield [Schema::VERSION_JSON_SCHEMA, '#/definitions/Foo'];
Expand Down