Skip to content
Draft
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
7 changes: 4 additions & 3 deletions src/Metadata/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
use ApiPlatform\OpenApi\Model\PathItem;
use ApiPlatform\State\OptionsInterface;

/**
Expand Down Expand Up @@ -326,7 +327,7 @@ public function __construct(
protected ?array $denormalizationContext = null,
protected ?bool $collectDenormalizationErrors = null,
protected ?array $hydraContext = null,
protected bool|OpenApiOperation|null $openapi = null,
protected bool|OpenApiOperation|PathItem|null $openapi = null,
/**
* The `validationContext` option configures the context of validation for the current ApiResource.
* You can, for instance, describe the validation groups that will be used:.
Expand Down Expand Up @@ -1373,12 +1374,12 @@ public function withHydraContext(array $hydraContext): static
return $self;
}

public function getOpenapi(): bool|OpenApiOperation|null
public function getOpenapi(): bool|OpenApiOperation|PathItem|null
{
return $this->openapi;
}

public function withOpenapi(bool|OpenApiOperation $openapi): static
public function withOpenapi(bool|OpenApiOperation|PathItem $openapi): static
{
$self = clone $this;
$self->openapi = $openapi;
Expand Down
7 changes: 4 additions & 3 deletions src/Metadata/HttpOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
use ApiPlatform\OpenApi\Attributes\Webhook;
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
use ApiPlatform\OpenApi\Model\PathItem;
use ApiPlatform\State\OptionsInterface;
use Symfony\Component\WebLink\Link as WebLink;

Expand Down Expand Up @@ -164,7 +165,7 @@ public function __construct(
protected ?array $cacheHeaders = null,
protected ?array $paginationViaCursor = null,
protected ?array $hydraContext = null,
protected bool|OpenApiOperation|Webhook|null $openapi = null,
protected bool|OpenApiOperation|Webhook|PathItem|null $openapi = null,
protected ?array $exceptionToStatus = null,
protected ?array $links = null,
protected ?array $errors = null,
Expand Down Expand Up @@ -629,12 +630,12 @@ public function withHydraContext(array $hydraContext): static
return $self;
}

public function getOpenapi(): bool|OpenApiOperation|Webhook|null
public function getOpenapi(): bool|OpenApiOperation|Webhook|PathItem|null
{
return $this->openapi;
}

public function withOpenapi(bool|OpenApiOperation|Webhook $openapi): static
public function withOpenapi(bool|OpenApiOperation|Webhook|PathItem $openapi): static
{
$self = clone $this;
$self->openapi = $openapi;
Expand Down
65 changes: 64 additions & 1 deletion src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection

$openapiAttribute = $operation->getOpenapi();

// if (null === $openapiAttribute) {
// $openapiAttribute = $resource->getOpenapi();
// }

// Operation ignored from OpenApi
if (false === $openapiAttribute) {
continue;
Expand Down Expand Up @@ -225,6 +229,9 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
if ($openapiAttribute instanceof Webhook) {
$pathItem = $openapiAttribute->getPathItem() ?: new PathItem();
$openapiOperation = $pathItem->{'get'.ucfirst(strtolower($method))}() ?: new Operation();
} elseif ($openapiAttribute instanceof PathItem) {
$pathItem = $openapiAttribute;
$openapiOperation = $pathItem->{'get'.ucfirst(strtolower($method))}() ?: new Operation();
} elseif (!\is_object($openapiAttribute)) {
$openapiOperation = new Operation();
} else {
Expand All @@ -248,6 +255,50 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
extensionProperties: $openapiOperation->getExtensionProperties(),
);

if ($openapiAttribute instanceof PathItem) {
if ($globalPathSummary = $openapiAttribute->getSummary()) {
$pathItem = $pathItem->withSummary($globalPathSummary);

foreach (PathItem::$methods as $pathMethod) {
$getMethod = 'get'.ucfirst(strtolower($pathMethod));
$withMethod = 'with'.ucfirst(strtolower($pathMethod));

if (($existingOperation = $pathItem->{$getMethod}()) && $existingOperation instanceof Operation) {
$existingOperationSummary = $existingOperation->getSummary();

if ($existingOperationSummary === $this->getPathDescription($resourceShortName, $pathMethod, false) || $existingOperationSummary === $this->getPathDescription($resourceShortName, $pathMethod, true)) {
$pathItem = $pathItem->{$withMethod}($existingOperation->withSummary($globalPathSummary));
}
}
}
}

if ($globalPathDescription = $openapiAttribute->getDescription()) {
$pathItem = $pathItem->withDescription($globalPathDescription);

foreach (PathItem::$methods as $pathMethod) {
$getMethod = 'get'.ucfirst(strtolower($pathMethod));
$withMethod = 'with'.ucfirst(strtolower($pathMethod));

if (($existingOperation = $pathItem->{$getMethod}()) && $existingOperation instanceof Operation) {
$existingOperationDescription = $existingOperation->getDescription();

if ($existingOperationDescription === $this->getPathDescription($resourceShortName, $pathMethod, false) || $existingOperationDescription === $this->getPathDescription($resourceShortName, $pathMethod, true)) {
$pathItem = $pathItem->{$withMethod}($existingOperation->withDescription($globalPathDescription));
}
}
}
}
}

if ($openapiAttribute instanceof PathItem && $openapiSummary = $openapiAttribute->getSummary()) {
$openapiOperation = $openapiOperation->withSummary($openapiSummary);
}

if ($openapiAttribute instanceof PathItem && $openapiDescription = $openapiAttribute->getDescription()) {
$openapiOperation = $openapiOperation->withDescription($openapiDescription);
}

foreach ($openapiOperation->getTags() as $v) {
$tags[$v] = new Tag(name: $v, description: $resource->getDescription() ?? "Resource '$v' operations.");
}
Expand Down Expand Up @@ -492,7 +543,19 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
$openapiOperation = $existingOperation->withResponse(200, $currentResponse->withContent($currentResponseContent));
}

$paths->addPath($path, $pathItem->{'with'.ucfirst($method)}($openapiOperation));
$finalPathItem = $pathItem->{'with'.ucfirst($method)}($openapiOperation);

if (null === $operation->getOpenapi() && $resource->getOpenapi() instanceof PathItem) {
$resourcePathItem = $resource->getOpenapi();
if ($resourcePathItem->getSummary()) {
$finalPathItem = $finalPathItem->withSummary($resourcePathItem->getSummary());
}
if ($resourcePathItem->getDescription()) {
$finalPathItem = $finalPathItem->withDescription($resourcePathItem->getDescription());
}
}

$paths->addPath($path, $finalPathItem);
}
}

Expand Down
Loading