Skip to content

Commit

Permalink
Apidoc: merge endpoint parameters specification if OpenApi annotation…
Browse files Browse the repository at this point in the history
… is provided (#199)

Co-authored-by: Petr Besir Horáček <[email protected]>
  • Loading branch information
besir and Petr Besir Horáček authored Jul 4, 2023
1 parent 8d66303 commit 596ac1e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
46 changes: 45 additions & 1 deletion src/OpenApi/Schema/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Apitte\OpenApi\Schema;

use Apitte\OpenApi\Utils\Helpers;

class Operation
{

Expand Down Expand Up @@ -65,7 +67,12 @@ public static function fromArray(array $data): Operation
continue;
}

$operation->addParameter(Parameter::fromArray($parameterData));
$parameter = Parameter::fromArray($parameterData);
if ($operation->hasParameter($parameter)) {
$operation->mergeParameter($parameter);
} else {
$operation->addParameter(Parameter::fromArray($parameterData));
}
}

if (isset($data['requestBody'])) {
Expand Down Expand Up @@ -128,9 +135,46 @@ public function setExternalDocs(?ExternalDocumentation $externalDocs): void
*/
public function addParameter($parameter): void
{
if ($parameter instanceof Parameter) {
$this->parameters[$this->getParameterKey($parameter)] = $parameter;

return;
}

$this->parameters[] = $parameter;
}

/**
* @param Parameter $parameter
* @return bool
*/
public function hasParameter(Parameter $parameter): bool
{
return array_key_exists($this->getParameterKey($parameter), $this->parameters);
}

/**
* @param Parameter $parameter
*/
public function mergeParameter(Parameter $parameter): void
{
$originalParameter = $this->parameters[$this->getParameterKey($parameter)];

$merged = Helpers::merge($parameter->toArray(), $originalParameter->toArray());
$parameter = Parameter::fromArray($merged);

$this->parameters[$this->getParameterKey($parameter)] = $parameter;
}

/**
* @param Parameter $parameter
* @return string
*/
private function getParameterKey(Parameter $parameter): string
{
return $parameter->getIn() . '-' . $parameter->getName();
}

/**
* @param RequestBody|Reference|null $requestBody
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/Cases/OpenApi/Schema/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function testOptional(): void
$operation->setExternalDocs($externalDocs);

$parameters = [];
$parameters[] = $p1 = new Parameter('p1', Parameter::IN_PATH);
$parameters['path-p1'] = $p1 = new Parameter('p1', Parameter::IN_PATH);
$operation->addParameter($p1);
$parameters[] = $p2 = new Parameter('p2', Parameter::IN_COOKIE);
$parameters['cookie-p2'] = $p2 = new Parameter('p2', Parameter::IN_COOKIE);
$operation->addParameter($p2);
$parameters[] = $p3 = new Reference('r1');
$operation->addParameter($p3);
Expand Down

0 comments on commit 596ac1e

Please sign in to comment.