Skip to content

Commit

Permalink
fix(metadata): convert composite uri variables w/ proper type
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed May 23, 2023
1 parent eacaf9f commit 8901d04
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/Api/UriVariablesConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function __construct(private readonly PropertyMetadataFactoryInterface $p

/**
* {@inheritdoc}
*
* To handle the composite identifiers type correctly, use an `uri_variables_map` that maps uriVariables to their uriVariablesDefinition.
* Indeed, a composite identifier will already be parsed, and their corresponding properties will be the parameterName and not the defined
* identifiers.
*/
public function convert(array $uriVariables, string $class, array $context = []): array
{
Expand All @@ -43,10 +47,15 @@ public function convert(array $uriVariables, string $class, array $context = [])
$uriVariablesDefinitions = $operation->getUriVariables() ?? [];

foreach ($uriVariables as $parameterName => $value) {
$uriVariableDefinition = $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
$uriVariableDefinition = $context['uri_variables_map'][$parameterName] ?? $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();

$identifierTypes = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $uriVariableDefinition->getIdentifiers() ?? [$parameterName]);
if (!($types = $identifierTypes[$parameterName] ?? false)) {
// When a composite identifier is used, we assume that the parameterName is the property to find our type
$properties = $uriVariableDefinition->getIdentifiers() ?? [$parameterName];
if ($uriVariableDefinition->getCompositeIdentifier()) {
$properties = [$parameterName];
}

if (!$types = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $properties)) {
continue;
}

Expand All @@ -73,7 +82,7 @@ private function getIdentifierTypes(string $resourceClass, array $properties): a
foreach ($properties as $property) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
foreach ($propertyMetadata->getBuiltinTypes() as $type) {
$types[$property][] = Type::BUILTIN_TYPE_OBJECT === ($builtinType = $type->getBuiltinType()) ? $type->getClassName() : $builtinType;
$types[] = Type::BUILTIN_TYPE_OBJECT === ($builtinType = $type->getBuiltinType()) ? $type->getClassName() : $builtinType;
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/State/UriVariablesResolverTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private function getOperationUriVariables(?HttpOperation $operation = null, arra
return $identifiers;
}

$uriVariablesMap = [];
foreach ($operation->getUriVariables() ?? [] as $parameterName => $uriVariableDefinition) {
if (!isset($parameters[$parameterName])) {
if (!isset($parameters['id'])) {
Expand All @@ -51,16 +52,18 @@ private function getOperationUriVariables(?HttpOperation $operation = null, arra

foreach ($currentIdentifiers as $key => $value) {
$identifiers[$key] = $value;
$uriVariableMap[$key] = $uriVariableDefinition;
}

continue;
}

$identifiers[$parameterName] = $parameters[$parameterName];
$uriVariableMap[$parameterName] = $uriVariableDefinition;
}

if ($this->uriVariablesConverter) {
$context = ['operation' => $operation];
$context = ['operation' => $operation, 'uri_variables_map' => $uriVariablesMap];
$identifiers = $this->uriVariablesConverter->convert($identifiers, $operation->getClass() ?? $resourceClass, $context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class CompositeKeyWithDifferentType

public static function provide(Operation $operation, array $uriVariables = [], array $context = []): array
{
if (!\is_string($uriVariables['verificationKey'])) {
throw new \RuntimeException('verificationKey should be a string.');
}

return $context;
}
}

0 comments on commit 8901d04

Please sign in to comment.