Skip to content

Commit 05713bf

Browse files
fix(hydra): move owl:maxCardinality from JsonSchema to Hydra (#6136)
1 parent ac8031e commit 05713bf

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

features/openapi/docs.feature

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ Feature: Documentation support
9898
And the "playMode" property for the OpenAPI class "VideoGame" should be equal to:
9999
"""
100100
{
101-
"owl:maxCardinality": 1,
102101
"type": "string",
103102
"format": "iri-reference",
104103
"example": "https://example.com/"
@@ -310,7 +309,6 @@ Feature: Documentation support
310309
And the "resourceRelated" property for the OpenAPI class "Resource" should be equal to:
311310
"""
312311
{
313-
"owl:maxCardinality": 1,
314312
"readOnly": true,
315313
"anyOf": [
316314
{
@@ -389,7 +387,6 @@ Feature: Documentation support
389387
And the "data" property for the OpenAPI class "WrappedResponseEntity.CustomOutputEntityWrapperDto-read" should be equal to:
390388
"""
391389
{
392-
"owl:maxCardinality": 1,
393390
"$ref": "#\/components\/schemas\/WrappedResponseEntity-read"
394391
}
395392
"""

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,23 @@ private function getRange(ApiProperty $propertyMetadata): array|string|null
386386
return 1 === \count($types) ? $types[0] : $types;
387387
}
388388

389+
private function isSingleRelation(ApiProperty $propertyMetadata): bool
390+
{
391+
$builtInTypes = $propertyMetadata->getBuiltinTypes() ?? [];
392+
393+
foreach ($builtInTypes as $type) {
394+
$className = $type->getClassName();
395+
if (!$type->isCollection()
396+
&& null !== $className
397+
&& $this->resourceClassResolver->isResourceClass($className)
398+
) {
399+
return true;
400+
}
401+
}
402+
403+
return false;
404+
}
405+
389406
/**
390407
* Builds the classes array.
391408
*/
@@ -509,6 +526,10 @@ private function getProperty(ApiProperty $propertyMetadata, string $propertyName
509526
$property['owl:deprecated'] = true;
510527
}
511528

529+
if ($this->isSingleRelation($propertyMetadata)) {
530+
$property['owl:maxCardinality'] = true;
531+
}
532+
512533
return $property;
513534
}
514535

src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,6 @@ public function create(string $resourceClass, string $property, array $options =
125125
$className = $valueType->getClassName();
126126
}
127127

128-
if (!\array_key_exists('owl:maxCardinality', $propertySchema)
129-
&& !$isCollection
130-
&& null !== $className
131-
&& $this->resourceClassResolver->isResourceClass($className)
132-
) {
133-
$propertySchema['owl:maxCardinality'] = 1;
134-
}
135-
136128
if ($isCollection && null !== $propertyMetadata->getUriTemplate()) {
137129
$keyType = null;
138130
$isCollection = false;

src/OpenApi/Serializer/LegacyOpenApiNormalizer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function normalize(mixed $object, string $format = null, array $context =
4545
}
4646
unset($schemas[$name]['properties'][$property]['type']);
4747
}
48-
unset($schemas[$name]['properties'][$property]['owl:maxCardinality']);
4948
}
5049
}
5150

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public function testArraySchemaWithReference(): void
120120
]);
121121

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

0 commit comments

Comments
 (0)