-
-
Notifications
You must be signed in to change notification settings - Fork 921
Add Subresource to swagger docs #1188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Metadata\Resource; | ||
|
||
final class SubResourceMetadata | ||
{ | ||
private $parent; | ||
private $property; | ||
private $isCollection; | ||
private $parentResourceClass; | ||
|
||
public function __construct(ResourceMetadata $parent, string $property, bool $isCollection, string $parentResourceClass) | ||
{ | ||
$this->parent = $parent; | ||
$this->property = $property; | ||
$this->isCollection = $isCollection; | ||
$this->parentResourceClass = $parentResourceClass; | ||
} | ||
|
||
/** | ||
* @return ResourceMetadata | ||
*/ | ||
public function getParent(): ResourceMetadata | ||
{ | ||
return $this->parent; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getProperty(): string | ||
{ | ||
return $this->property; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isCollection(): bool | ||
{ | ||
return $this->isCollection; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getParentResourceClass(): string | ||
{ | ||
return $this->parentResourceClass; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; | ||
use ApiPlatform\Core\Metadata\Resource\SubResourceMetadata; | ||
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
@@ -98,6 +99,8 @@ public function normalize($object, $format = null, array $context = []) | |
|
||
$this->addPaths($paths, $definitions, $resourceClass, $resourceShortName, $resourceMetadata, $mimeTypes, OperationType::COLLECTION); | ||
$this->addPaths($paths, $definitions, $resourceClass, $resourceShortName, $resourceMetadata, $mimeTypes, OperationType::ITEM); | ||
|
||
$this->processSubResources($paths, $definitions, $resourceClass, $resourceMetadata, $mimeTypes); | ||
} | ||
|
||
$definitions->ksort(); | ||
|
@@ -109,25 +112,34 @@ public function normalize($object, $format = null, array $context = []) | |
/** | ||
* Updates the list of entries in the paths collection. | ||
* | ||
* @param \ArrayObject $paths | ||
* @param \ArrayObject $definitions | ||
* @param string $resourceClass | ||
* @param string $resourceShortName | ||
* @param ResourceMetadata $resourceMetadata | ||
* @param array $mimeTypes | ||
* @param string $operationType | ||
* @param \ArrayObject $paths | ||
* @param \ArrayObject $definitions | ||
* @param string $resourceClass | ||
* @param string $resourceShortName | ||
* @param ResourceMetadata $resourceMetadata | ||
* @param array $mimeTypes | ||
* @param string $operationType | ||
* @param SubResourceMetadata $subResource | ||
*/ | ||
private function addPaths(\ArrayObject $paths, \ArrayObject $definitions, string $resourceClass, string $resourceShortName, ResourceMetadata $resourceMetadata, array $mimeTypes, string $operationType) | ||
private function addPaths(\ArrayObject $paths, \ArrayObject $definitions, string $resourceClass, string $resourceShortName, ResourceMetadata $resourceMetadata, array $mimeTypes, string $operationType, SubResourceMetadata $subResource = null) | ||
{ | ||
if (null === $operations = $operationType === OperationType::COLLECTION ? $resourceMetadata->getCollectionOperations() : $resourceMetadata->getItemOperations()) { | ||
if ($subResource) { | ||
$operations = $resourceMetadata->getCollectionOperations(); | ||
} else if (null === $operations = $operationType === OperationType::ITEM ? $resourceMetadata->getItemOperations() : $resourceMetadata->getCollectionOperations()) { | ||
return; | ||
} | ||
|
||
foreach ($operations as $operationName => $operation) { | ||
if ($subResource) { | ||
$operation['identifiers'] = [['id', $subResource->getParentResourceClass()]]; | ||
$operation['property'] = $subResource->getProperty(); | ||
$operation['collection'] = $subResource->isCollection(); | ||
} | ||
|
||
$path = $this->getPath($resourceShortName, $operation, $operationType); | ||
$method = $operationType === OperationType::ITEM ? $this->operationMethodResolver->getItemOperationMethod($resourceClass, $operationName) : $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName); | ||
|
||
$paths[$path][strtolower($method)] = $this->getPathOperation($operationName, $operation, $method, $operationType, $resourceClass, $resourceMetadata, $mimeTypes, $definitions); | ||
$paths[$path][strtolower($method)] = $this->getPathOperation($operationName, $operation, $method, $operationType, $resourceClass, $resourceMetadata, $mimeTypes, $definitions, $subResource); | ||
} | ||
} | ||
|
||
|
@@ -160,22 +172,23 @@ private function getPath(string $resourceShortName, array $operation, string $op | |
* | ||
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operation-object | ||
* | ||
* @param string $operationName | ||
* @param array $operation | ||
* @param string $method | ||
* @param string $operationType | ||
* @param string $resourceClass | ||
* @param ResourceMetadata $resourceMetadata | ||
* @param string[] $mimeTypes | ||
* @param \ArrayObject $definitions | ||
* @param string $operationName | ||
* @param array $operation | ||
* @param string $method | ||
* @param string $operationType | ||
* @param string $resourceClass | ||
* @param ResourceMetadata $resourceMetadata | ||
* @param string[] $mimeTypes | ||
* @param \ArrayObject $definitions | ||
* @param SubResourceMetadata $subResource | ||
* | ||
* @return \ArrayObject | ||
*/ | ||
private function getPathOperation(string $operationName, array $operation, string $method, string $operationType, string $resourceClass, ResourceMetadata $resourceMetadata, array $mimeTypes, \ArrayObject $definitions): \ArrayObject | ||
private function getPathOperation(string $operationName, array $operation, string $method, string $operationType, string $resourceClass, ResourceMetadata $resourceMetadata, array $mimeTypes, \ArrayObject $definitions, SubResourceMetadata $subResource = null): \ArrayObject | ||
{ | ||
$pathOperation = new \ArrayObject($operation['swagger_context'] ?? []); | ||
$resourceShortName = $resourceMetadata->getShortName(); | ||
$pathOperation['tags'] ?? $pathOperation['tags'] = [$resourceShortName]; | ||
$pathOperation['tags'] ?? $pathOperation['tags'] = [$subResource ? $subResource->getParent()->getShortName() : $resourceShortName]; | ||
$pathOperation['operationId'] ?? $pathOperation['operationId'] = lcfirst($operationName).ucfirst($resourceShortName).ucfirst($operationType); | ||
|
||
switch ($method) { | ||
|
@@ -636,4 +649,29 @@ private function getSerializerContext(string $operationType, bool $denormalizati | |
|
||
return $resourceMetadata->getItemOperationAttribute($operationName, $contextKey, null, true); | ||
} | ||
|
||
/** | ||
* @param \ArrayObject $paths | ||
* @param \ArrayObject $definitions | ||
* @param string $parentResourceClass | ||
* @param ResourceMetadata $parentResourceMetadata | ||
* @param array $mimeTypes | ||
*/ | ||
private function processSubResources(\ArrayObject $paths, \ArrayObject $definitions, string $parentResourceClass, ResourceMetadata $parentResourceMetadata, array $mimeTypes) | ||
{ | ||
$serializerContext = $this->getSerializerContext(OperationType::SUBRESOURCE, false, $parentResourceMetadata, 'get'); | ||
$options = isset($serializerContext['groups']) ? ['serializer_groups' => $serializerContext['groups']] : []; | ||
foreach ($this->propertyNameCollectionFactory->create($parentResourceClass, $options) as $property) { | ||
$propertyMetadata = $this->propertyMetadataFactory->create($parentResourceClass, $property); | ||
if ($propertyMetadata->hasSubresource()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use a guard clause to make the code easier to read? if (!$propertyMetadata->hasSubresource()) {
continue;
} |
||
$isCollection = $propertyMetadata->getType()->isCollection(); | ||
$resourceClass = $isCollection ? $propertyMetadata->getType()->getCollectionValueType()->getClassName() : $propertyMetadata->getType()->getClassName(); | ||
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); | ||
|
||
$subResourceMetadata = new SubResourceMetadata($parentResourceMetadata, $property, $isCollection, $parentResourceClass); | ||
|
||
$this->addPaths($paths, $definitions, $resourceClass, $parentResourceMetadata->getShortName(), $resourceMetadata, $mimeTypes, OperationType::SUBRESOURCE, $subResourceMetadata); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm this is a good idea. I didn't wanted to introduce a new Metadata class when I worked on subresources but this will give more power to subresource declarations (ping @teohhanhui ^^).
WDYT about going a bit further, so that
PropertyMetadataFactory
will create theSubResourceMetadata
when it's declared as such? This is what the outcome may be in the code below:With something like this we can avoid repeating the following code (for example also in
ApiLoader
):ping @api-platform/core-team
IMO we need to agree on this change first because it may involve many changes ^^. Anyway, if we introduce this new metadata class, it'd be a shame not to use it everywhere we can :).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's worth it to introduce yet another metadata class.
In fact, I'd argue for the removal of
PropertyMetadata::$subresource
property. Just use an attribute.