Skip to content
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

Merge endpoint parameters specification if OpenApi annotation is provided #199

Merged
merged 1 commit into from
Jul 4, 2023
Merged
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
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