Skip to content

Commit

Permalink
fix(hydra): move owl:maxCardinality from JsonSchema to Hydra (#6136)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon authored Jan 31, 2024
1 parent ac8031e commit 05713bf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
3 changes: 0 additions & 3 deletions features/openapi/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ Feature: Documentation support
And the "playMode" property for the OpenAPI class "VideoGame" should be equal to:
"""
{
"owl:maxCardinality": 1,
"type": "string",
"format": "iri-reference",
"example": "https://example.com/"
Expand Down Expand Up @@ -310,7 +309,6 @@ Feature: Documentation support
And the "resourceRelated" property for the OpenAPI class "Resource" should be equal to:
"""
{
"owl:maxCardinality": 1,
"readOnly": true,
"anyOf": [
{
Expand Down Expand Up @@ -389,7 +387,6 @@ Feature: Documentation support
And the "data" property for the OpenAPI class "WrappedResponseEntity.CustomOutputEntityWrapperDto-read" should be equal to:
"""
{
"owl:maxCardinality": 1,
"$ref": "#\/components\/schemas\/WrappedResponseEntity-read"
}
"""
Expand Down
21 changes: 21 additions & 0 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,23 @@ private function getRange(ApiProperty $propertyMetadata): array|string|null
return 1 === \count($types) ? $types[0] : $types;
}

private function isSingleRelation(ApiProperty $propertyMetadata): bool
{
$builtInTypes = $propertyMetadata->getBuiltinTypes() ?? [];

foreach ($builtInTypes as $type) {
$className = $type->getClassName();
if (!$type->isCollection()
&& null !== $className
&& $this->resourceClassResolver->isResourceClass($className)
) {
return true;
}
}

return false;
}

/**
* Builds the classes array.
*/
Expand Down Expand Up @@ -509,6 +526,10 @@ private function getProperty(ApiProperty $propertyMetadata, string $propertyName
$property['owl:deprecated'] = true;
}

if ($this->isSingleRelation($propertyMetadata)) {
$property['owl:maxCardinality'] = true;
}

return $property;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ public function create(string $resourceClass, string $property, array $options =
$className = $valueType->getClassName();
}

if (!\array_key_exists('owl:maxCardinality', $propertySchema)
&& !$isCollection
&& null !== $className
&& $this->resourceClassResolver->isResourceClass($className)
) {
$propertySchema['owl:maxCardinality'] = 1;
}

if ($isCollection && null !== $propertyMetadata->getUriTemplate()) {
$keyType = null;
$isCollection = false;
Expand Down
1 change: 0 additions & 1 deletion src/OpenApi/Serializer/LegacyOpenApiNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function normalize(mixed $object, string $format = null, array $context =
}
unset($schemas[$name]['properties'][$property]['type']);
}
unset($schemas[$name]['properties'][$property]['owl:maxCardinality']);
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function testArraySchemaWithReference(): void
]);

$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['type'], [
'owl:maxCardinality' => 1,
'$ref' => '#/definitions/TestEntity.jsonld-write',
]);
}
Expand Down

0 comments on commit 05713bf

Please sign in to comment.