Skip to content

Commit 4c89914

Browse files
committed
Improvements and tests for the me endpoint
1 parent 74ef5ed commit 4c89914

1 file changed

Lines changed: 30 additions & 29 deletions

File tree

src/ApiPlatform/Api/IriConverter.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,41 @@ public function getResourceFromIri(string $iri, array $context = [], ?Operation
3737
return $this->decorated->getResourceFromIri($iri, $context, $operation);
3838
}
3939

40-
// We want relations when they are found, to use the IRI with the path
40+
private function getUserGetOperation(Operation $operation = null): Operation {
41+
// we do not want to return the /me IRI if a Get endpoint is configured. The IRI should be canonical to the user's ID etc.
42+
43+
// get the API metadata of the class
44+
// find the Get operation - ApiPlatform\Metadata\Get
45+
// use this uriTemplate instead, overwrite $operation and the operation in context
46+
$resourceIterator = $this->resourceMetadataCollectionFactory->create($operation->getClass())->getIterator();
47+
while($resourceIterator->valid()) {
48+
$current = $resourceIterator->current();
49+
/**
50+
* @var Operations $resourceOperations
51+
*/
52+
$resourceOperations = $current->getOperations();
53+
$operationIterator = $resourceOperations->getIterator();
54+
while($operationIterator->valid()) {
55+
$checkOperation = $operationIterator->current();
56+
if ($checkOperation instanceof Get) {
57+
return $checkOperation;
58+
}
59+
$operationIterator->next();
60+
}
61+
$resourceIterator->next();
62+
}
63+
return $operation;
64+
}
4165

42-
/**
43-
* @throws ResourceClassNotFoundException
44-
* @throws \Exception
45-
*/
66+
// We want relations when they are found, to use the IRI with the path
4667
public function getIriFromResource($resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null, array $context = []): ?string
4768
{
4869
if ($operation?->getName() === 'me') {
49-
// we do not want to return the /me IRI if a Get endpoint is configured. The IRI should be canonical to the user's ID etc.
50-
51-
// get the API metadata of the class
52-
// find the Get operation - ApiPlatform\Metadata\Get
53-
// use this uriTemplate instead, overwrite $operation and the operation in context
54-
$resourceIterator = $this->resourceMetadataCollectionFactory->create($operation->getClass())->getIterator();
55-
while($resourceIterator->valid()) {
56-
$current = $resourceIterator->current();
57-
/**
58-
* @var Operations $resourceOperations
59-
*/
60-
$resourceOperations = $current->getOperations();
61-
$operationIterator = $resourceOperations->getIterator();
62-
while($operationIterator->valid()) {
63-
$checkOperation = $operationIterator->current();
64-
if ($checkOperation instanceof Get) {
65-
$operation = $checkOperation;
66-
$context['operation'] = $checkOperation;
67-
break 2;
68-
}
69-
$operationIterator->next();
70-
}
71-
$resourceIterator->next();
72-
}
70+
$checkOperation = $this->getUserGetOperation($operation);
71+
$operation = $checkOperation;
72+
$context['operation'] = $checkOperation;
7373
}
74+
7475
$originalIri = $this->decorated->getIriFromResource($resource, $referenceType, $operation, $context);
7576

7677
if (!$resource instanceof Route || !($path = $resource->getPath())) {

0 commit comments

Comments
 (0)