Skip to content

Commit

Permalink
fix-2022: Correctly describe array parameter (#2203)
Browse files Browse the repository at this point in the history
* fix-2022: Correctly describe array parameter

* style fix
  • Loading branch information
DjordyKoert committed Jan 29, 2024
1 parent b96c263 commit 19d628a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Processors/MapQueryStringProcessor.php
Expand Up @@ -65,7 +65,11 @@ private function addQueryParameters(Analysis $analysis, OA\Operation $operation,
$isModelOptional = $argumentMetaData->hasDefaultValue() || $argumentMetaData->isNullable();

foreach ($schemaModel->properties as $property) {
$operationParameter = Util::getOperationParameter($operation, $property->property, 'query');
$name = 'array' === $property->type
? $property->property.'[]'
: $property->property;

$operationParameter = Util::getOperationParameter($operation, $name, 'query');

// Remove incompatible properties
$propertyVars = get_object_vars($property);
Expand Down
Expand Up @@ -22,7 +22,12 @@ public function describe(ArgumentMetadata $argumentMetadata, OA\Operation $opera
return;
}

$operationParameter = Util::getOperationParameter($operation, $attribute->name ?? $argumentMetadata->getName(), 'query');
$name = $attribute->name ?? $argumentMetadata->getName();
$name = 'array' === $argumentMetadata->getType()
? $name.'[]'
: $name;

$operationParameter = Util::getOperationParameter($operation, $name, 'query');

Util::modifyAnnotationValue($operationParameter, 'required', !($argumentMetadata->hasDefaultValue() || $argumentMetadata->isNullable()));

Expand Down
3 changes: 3 additions & 0 deletions Tests/Functional/Controller/ApiController81.php
Expand Up @@ -26,6 +26,7 @@
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithNullableSchemaSet;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithObjectType;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithRef;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\ArrayQueryModel;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\FilterQueryModel;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\PaginationQueryModel;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel\SortQueryModel;
Expand Down Expand Up @@ -553,6 +554,7 @@ public function fetchArticleWithManyParameters(
#[MapQueryString] FilterQueryModel $filterQuery,
#[MapQueryString] PaginationQueryModel $paginationQuery,
#[MapQueryString] SortQueryModel $sortQuery,
#[MapQueryString] ArrayQueryModel $arrayQuery,
) {
}

Expand All @@ -562,6 +564,7 @@ public function fetchArticleWithManyOptionalParameters(
#[MapQueryString] ?FilterQueryModel $filterQuery,
#[MapQueryString] ?PaginationQueryModel $paginationQuery,
#[MapQueryString] ?SortQueryModel $sortQuery,
#[MapQueryString] ?ArrayQueryModel $arrayQuery,
) {
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/Functional/Entity/QueryModel/ArrayQueryModel.php
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Nelmio\ApiDocBundle\Tests\Functional\Entity\QueryModel;

use OpenApi\Attributes as OA;

class ArrayQueryModel
{
public array $ids;
#[OA\Property(description: 'List of product ids', type: 'array', items: new OA\Items(type: 'integer'))]
private ?array $productIds = null;

public function getProductIds(): ?array
{
return $this->productIds;
}
}
46 changes: 45 additions & 1 deletion Tests/Functional/SymfonyFunctionalTest.php
Expand Up @@ -301,7 +301,7 @@ public function testMapQueryParameter(): void
],
],
[
'name' => 'someArray',
'name' => 'someArray[]',
'in' => 'query',
'required' => true,
'schema' => [
Expand Down Expand Up @@ -629,6 +629,28 @@ public function testMapQueryStringHandlesManyParameters(): void
'$ref' => '#/components/schemas/SortEnum',
],
],
[
'name' => 'ids[]',
'in' => 'query',
'required' => true,
'schema' => [
'type' => 'array',
'items' => [],
],
],
[
'name' => 'productIds[]',
'in' => 'query',
'required' => false,
'description' => 'List of product ids',
'schema' => [
'type' => 'array',
'description' => 'List of product ids',
'items' => [
'type' => 'integer',
],
],
],
], json_decode($this->getOperation('/api/article_map_query_string_many_parameters', 'get')->toJson(), true)['parameters']);
}

Expand Down Expand Up @@ -692,6 +714,28 @@ public function testMapQueryStringHandlesManyParametersOptional(): void
'$ref' => '#/components/schemas/SortEnum',
],
],
[
'name' => 'ids[]',
'in' => 'query',
'required' => false,
'schema' => [
'type' => 'array',
'items' => [],
],
],
[
'name' => 'productIds[]',
'in' => 'query',
'required' => false,
'description' => 'List of product ids',
'schema' => [
'type' => 'array',
'description' => 'List of product ids',
'items' => [
'type' => 'integer',
],
],
],
], json_decode($this->getOperation('/api/article_map_query_string_many_parameters_optional', 'get')->toJson(), true)['parameters']);
}

Expand Down

0 comments on commit 19d628a

Please sign in to comment.