Skip to content
Closed
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
30 changes: 30 additions & 0 deletions features/main/relation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ Feature: Relations support
}
"""

Scenario: Get related dummies by a path with a dynamic attribute
When I add "Content-Type" header equal to "application/ld+json"
And I send a "GET" request to "/dummy/33/related_dummies"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/RelatedDummy",
"@id": "/dummy/33/related_dummies",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/related_dummies/1",
"@type": "https://schema.org/Product",
"name": null,
"dummyDate": null,
"thirdLevel": "/third_levels/1",
"relatedToDummyFriend": [],
"dummyBoolean": null,
"id": 1,
"symfony": "symfony",
"age": null
}
],
"hydra:totalItems": 1
}
"""

Scenario: Create a friend relationship
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/related_to_dummy_friends" with body:
Expand Down
18 changes: 17 additions & 1 deletion src/Hydra/Serializer/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function normalize($object, $format = null, array $context = [])
$data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
$context = $this->initContext($resourceClass, $context);

$data['@id'] = $this->iriConverter->getIriFromResourceClass($resourceClass);
$data['@id'] = $this->getIri($resourceClass, $context);
$data['@type'] = 'hydra:Collection';

$data['hydra:member'] = [];
Expand All @@ -86,4 +86,20 @@ public function normalize($object, $format = null, array $context = [])

return $data;
}

/**
* @param string $resourceClass
* @param array $context
*
* @return string
*/
private function getIri(string $resourceClass, array $context): string
{
$prepared = [];
if (!empty($context['request_uri'])) {
$prepared = parse_url($context['request_uri']);
}

return $prepared['path'] ?? $this->iriConverter->getIriFromResourceClass($resourceClass);
}
}
47 changes: 37 additions & 10 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ private function updateGetOperation(\ArrayObject $pathOperation, array $mimeType
$responseDefinitionKey = $this->getDefinition($definitions, $collection, false, $resourceMetadata, $resourceClass, $operationName);
$pathOperation['produces'] ?? $pathOperation['produces'] = $mimeTypes;

if (!array_key_exists('parameters', $pathOperation)) {
$pathParameters = $this->getRequiredPathParameters($resourceMetadata, $operationName, $collection);
$filtersParameters = $collection ? $this->getFiltersParameters($resourceClass, $operationName, $resourceMetadata) : [];
$parameters = array_merge($pathParameters, $filtersParameters);

if ($parameters) {
$pathOperation['parameters'] = $parameters;
}
}

if ($collection) {
$pathOperation['summary'] ?? $pathOperation['summary'] = sprintf('Retrieves the collection of %s resources.', $resourceShortName);
$pathOperation['responses'] ?? $pathOperation['responses'] = [
Expand All @@ -203,20 +213,10 @@ private function updateGetOperation(\ArrayObject $pathOperation, array $mimeType
],
];

if (!isset($pathOperation['parameters']) && count($parameters = $this->getFiltersParameters($resourceClass, $operationName, $resourceMetadata)) > 0) {
$pathOperation['parameters'] = $parameters;
}

return $pathOperation;
}

$pathOperation['summary'] ?? $pathOperation['summary'] = sprintf('Retrieves a %s resource.', $resourceShortName);
$pathOperation['parameters'] ?? $pathOperation['parameters'] = [[
'name' => 'id',
'in' => 'path',
'required' => true,
'type' => 'integer',
]];
$pathOperation['responses'] ?? $pathOperation['responses'] = [
'200' => [
'description' => sprintf('%s resource response', $resourceShortName),
Expand Down Expand Up @@ -561,6 +561,33 @@ private function getFiltersParameters(string $resourceClass, string $operationNa
return $parameters;
}

/**
* @param ResourceMetadata $metadata
* @param string $operationName
* @param bool $collection
*
* @return array
*/
private function getRequiredPathParameters(ResourceMetadata $metadata, string $operationName, bool $collection): array
{
$operations = $collection ? $metadata->getCollectionOperations() : $metadata->getItemOperations();
$operation = $operations[$operationName];
$path = $this->getPath($metadata->getShortName(), $operation, $collection);
preg_match_all('#\{([^\}].+)\}#', $path, $matches);

$result = [];
foreach ($matches[1] as $requiredPathParameter) {
$result[] = [
'name' => $requiredPathParameter,
'in' => 'path',
'required' => true,
'type' => 'integer',
];
}

return $result;
}

/**
* {@inheritdoc}
*/
Expand Down
11 changes: 9 additions & 2 deletions tests/Fixtures/TestBundle/Entity/RelatedDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*
* @ApiResource(iri="https://schema.org/Product")
* @ApiResource(
* iri="https://schema.org/Product",
* collectionOperations={
* "get" = {"method"="GET"},
* "get_by_parent" = {"method"="GET", "path"="/dummy/{id}/related_dummies"},
* "post" = {"method"="POST"}
* }
* )
* @ORM\Entity
*/
class RelatedDummy extends ParentDummy
Expand Down Expand Up @@ -137,7 +144,7 @@ public function getRelatedToDummyFriend()
/**
* Set relatedToDummyFriend.
*
* @param relatedToDummyFriend the value to set
* @param RelatedToDummyFriend $relatedToDummyFriend
*/
public function setRelatedToDummyFriend(RelatedToDummyFriend $relatedToDummyFriend)
{
Expand Down