Skip to content

Commit

Permalink
Merge fe52cc1 into 8b02b2b
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Mar 15, 2021
2 parents 8b02b2b + fe52cc1 commit 35e00b2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
47 changes: 42 additions & 5 deletions src/OpenApi/Factory/OpenApiFactory.php
Expand Up @@ -175,19 +175,42 @@ private function collectPaths(ResourceMetadata $resourceMetadata, string $resour
// Set up parameters
if (OperationType::ITEM === $operationType) {
foreach ($identifiers as $parameterName => $identifier) {
$parameters[] = new Model\Parameter(\is_string($parameterName) ? $parameterName : $identifier, 'path', 'Resource identifier', true, false, false, ['type' => 'string']);
$parameterName = \is_string($parameterName) ? $parameterName : $identifier;
$parameter = new Model\Parameter($parameterName, 'path', 'Resource identifier', true, false, false, ['type' => 'string']);
if ($this->hasParameter($parameter, $parameters)) {
continue;
}

$parameters[] = $parameter;
}
$links[$operationId] = $this->getLink($resourceClass, $operationId, $path);
} elseif (OperationType::COLLECTION === $operationType && 'GET' === $method) {
$parameters = array_merge($parameters, $this->getPaginationParameters($resourceMetadata, $operationName), $this->getFiltersParameters($resourceMetadata, $operationName, $resourceClass));
foreach (array_merge($this->getPaginationParameters($resourceMetadata, $operationName), $this->getFiltersParameters($resourceMetadata, $operationName, $resourceClass)) as $parameter) {
if ($this->hasParameter($parameter, $parameters)) {
continue;
}

$parameters[] = $parameter;
}
} elseif (OperationType::SUBRESOURCE === $operationType) {
foreach ($operation['identifiers'] as $parameterName => [$class, $property]) {
$parameters[] = new Model\Parameter($parameterName, 'path', $this->resourceMetadataFactory->create($class)->getShortName().' identifier', true, false, false, ['type' => 'string']);
$parameter = new Model\Parameter($parameterName, 'path', $this->resourceMetadataFactory->create($class)->getShortName().' identifier', true, false, false, ['type' => 'string']);
if ($this->hasParameter($parameter, $parameters)) {
continue;
}

$parameters[] = $parameter;
}

if ($operation['collection']) {
$subresourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$parameters = array_merge($parameters, $this->getPaginationParameters($resourceMetadata, $operationName), $this->getFiltersParameters($subresourceMetadata, $operationName, $resourceClass));
foreach (array_merge($this->getPaginationParameters($resourceMetadata, $operationName), $this->getFiltersParameters($subresourceMetadata, $operationName, $resourceClass)) as $parameter) {
if ($this->hasParameter($parameter, $parameters)) {
continue;
}

$parameters[] = $parameter;
}
}
}

Expand Down Expand Up @@ -396,7 +419,7 @@ private function getFiltersParameters(ResourceMetadata $resourceMetadata, string
$schema,
'array' === $schema['type'] && \in_array($data['type'],
[Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT], true) ? 'deepObject' : 'form',
'array' === $schema['type'],
$data['openapi']['explode'] ?? ('array' === $schema['type']),
$data['openapi']['allowReserved'] ?? false,
$data['openapi']['example'] ?? null,
isset($data['openapi']['examples']
Expand Down Expand Up @@ -486,4 +509,18 @@ private function getSecuritySchemes(): array

return $securitySchemes;
}

/**
* @var Model\Parameter[]
*/
private function hasParameter(Model\Parameter $parameter, array $parameters): bool
{
foreach ($parameters as $existingParameter) {
if ($existingParameter->getName() === $parameter->getName() && $existingParameter->getIn() === $parameter->getIn()) {
return true;
}
}

return false;
}
}
10 changes: 10 additions & 0 deletions src/OpenApi/Model/Encoding.php
Expand Up @@ -52,11 +52,21 @@ public function canExplode(): bool
return $this->explode;
}

public function getExplode(): bool
{
return $this->explode;
}

public function canAllowReserved(): bool
{
return $this->allowReserved;
}

public function getAllowReserved(): bool
{
return $this->allowReserved;
}

public function withContentType(string $contentType): self
{
$clone = clone $this;
Expand Down
12 changes: 11 additions & 1 deletion src/OpenApi/Model/Parameter.php
Expand Up @@ -83,7 +83,7 @@ public function getDeprecated(): bool
return $this->deprecated;
}

public function canAllowEmptyValue(): bool
public function getAllowEmptyValue(): bool
{
return $this->allowEmptyValue;
}
Expand All @@ -99,6 +99,11 @@ public function getStyle(): string
}

public function canExplode(): bool
{
return $this->allowReserved;
}

public function getExplode(): bool
{
return $this->explode;
}
Expand All @@ -108,6 +113,11 @@ public function canAllowReserved(): bool
return $this->allowReserved;
}

public function getAllowReserved(): bool
{
return $this->allowReserved;
}

public function getExample()
{
return $this->example;
Expand Down
17 changes: 11 additions & 6 deletions tests/OpenApi/Factory/OpenApiFactoryTest.php
Expand Up @@ -77,7 +77,8 @@ public function testInvoke(): void
'custom' => ['method' => 'HEAD', 'path' => '/foo/{id}', 'openapi_context' => [
'description' => 'Custom description',
'parameters' => [
['description' => 'Test parameter', 'name' => 'param', 'in' => 'path', 'type' => 'string', 'required' => true, 'default' => 'BOTH'],
['description' => 'Test parameter', 'name' => 'param', 'in' => 'path', 'required' => true],
['description' => 'Replace parameter', 'name' => 'id', 'in' => 'path', 'required' => true, 'schema' => ['type' => 'string', 'format' => 'uuid']],
],
'tags' => ['Dummy', 'Profile'],
'responses' => [
Expand Down Expand Up @@ -117,7 +118,11 @@ public function testInvoke(): void
'formats' => ['method' => 'PUT', 'path' => '/formatted/{id}', 'output_formats' => ['json' => ['application/json'], 'csv' => ['text/csv']], 'input_formats' => ['json' => ['application/json'], 'csv' => ['text/csv']]],
],
[
'get' => ['method' => 'GET'] + self::OPERATION_FORMATS,
'get' => ['method' => 'GET', 'openapi_context' => [
'parameters' => [
['description' => 'Test modified collection page number', 'name' => 'page', 'in' => 'query', 'required' => false, 'schema' => ['type' => 'integer', 'default' => 1], 'allowEmptyValue' => true],
],
]] + self::OPERATION_FORMATS,
'post' => ['method' => 'POST'] + self::OPERATION_FORMATS,
'filtered' => ['method' => 'GET', 'filters' => ['f1', 'f2', 'f3', 'f4', 'f5'], 'path' => '/filtered'] + self::OPERATION_FORMATS,
'paginated' => ['method' => 'GET', 'pagination_client_enabled' => true, 'pagination_client_items_per_page' => true, 'pagination_items_per_page' => 20, 'pagination_maximum_items_per_page' => 80, 'path' => '/paginated'] + self::OPERATION_FORMATS,
Expand Down Expand Up @@ -153,7 +158,7 @@ public function testInvoke(): void
'type' => 'string',
'required' => true,
'strategy' => 'exact',
'openapi' => ['example' => 'bar', 'deprecated' => true, 'allowEmptyValue' => true, 'allowReserved' => true],
'openapi' => ['example' => 'bar', 'deprecated' => true, 'allowEmptyValue' => true, 'allowReserved' => true, 'explode' => true],
]]),
'f2' => new DummyFilter(['ha' => [
'property' => 'foo',
Expand Down Expand Up @@ -306,7 +311,7 @@ public function testInvoke(): void
'Retrieves the collection of Dummy resources.',
null,
[
new Model\Parameter('page', 'query', 'The collection page number', false, false, true, [
new Model\Parameter('page', 'query', 'Test modified collection page number', false, false, true, [
'type' => 'integer',
'default' => 1,
]),
Expand Down Expand Up @@ -434,7 +439,7 @@ public function testInvoke(): void
'Dummy',
'Custom description',
null,
[new Model\Parameter('param', 'path', 'Test parameter', true), new Model\Parameter('id', 'path', 'Resource identifier', true, false, false, ['type' => 'string'])],
[new Model\Parameter('param', 'path', 'Test parameter', true), new Model\Parameter('id', 'path', 'Replace parameter', true, false, false, ['type' => 'string', 'format' => 'uuid'])],
new Model\RequestBody('Custom request body', new \ArrayObject([
'multipart/form-data' => [
'schema' => [
Expand Down Expand Up @@ -512,7 +517,7 @@ public function testInvoke(): void
]),
new Model\Parameter('name', 'query', '', true, true, true, [
'type' => 'string',
], 'form', false, true, 'bar'),
], 'form', true, true, 'bar'),
new Model\Parameter('ha', 'query', '', false, false, true, [
'type' => 'integer',
]),
Expand Down

0 comments on commit 35e00b2

Please sign in to comment.