Skip to content
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
6 changes: 5 additions & 1 deletion src/OpenApi/Model/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Parameter
{
use ExtensionTrait;

public function __construct(private string $name, private string $in, private string $description = '', private bool $required = false, private bool $deprecated = false, private ?bool $allowEmptyValue = null, private array $schema = [], private ?string $style = null, private bool $explode = false, private ?bool $allowReserved = null, private mixed $example = null, private ?\ArrayObject $examples = null, private ?\ArrayObject $content = null)
public function __construct(private string $name, private string $in, private string $description = '', private bool $required = false, private bool $deprecated = false, private ?bool $allowEmptyValue = null, private array $schema = [], private ?string $style = null, private ?bool $explode = null, private ?bool $allowReserved = null, private mixed $example = null, private ?\ArrayObject $examples = null, private ?\ArrayObject $content = null)
{
if (null === $style) {
if ('query' === $in || 'cookie' === $in) {
Expand All @@ -26,6 +26,10 @@ public function __construct(private string $name, private string $in, private st
$this->style = 'simple';
}
}

if (null === $explode) {
$this->explode = \in_array($this->style, ['form', 'cookie'], true);
}
}

public function getName(): string
Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi/Tests/Factory/OpenApiFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,15 +968,15 @@ public function testInvoke(): void
], 'form', true, true, 'bar'),
new Parameter('ha', 'query', '', false, false, null, [
'type' => 'integer',
]),
], 'form', false),
new Parameter('toto', 'query', '', true, false, null, [
'type' => 'array',
'items' => ['type' => 'string'],
], 'deepObject', true),
new Parameter('order[name]', 'query', '', false, false, null, [
'type' => 'string',
'enum' => ['asc', 'desc'],
]),
], 'form', false),
],
), $filteredPath->getGet());

Expand Down
74 changes: 74 additions & 0 deletions src/OpenApi/Tests/Model/ParameterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\OpenApi\Tests\Model;

use ApiPlatform\OpenApi\Model\Parameter;
use PHPUnit\Framework\TestCase;

class ParameterTest extends TestCase
{
public function testExplodeDefaultsTrueForFormStyle(): void
{
$parameter = new Parameter('test', 'query');
$this->assertSame('form', $parameter->getStyle());
$this->assertTrue($parameter->getExplode());
}

public function testExplodeDefaultsTrueForCookieStyle(): void
{
$parameter = new Parameter('test', 'cookie');
$this->assertSame('form', $parameter->getStyle());
$this->assertTrue($parameter->getExplode());
}

public function testExplodeDefaultsFalseForPathStyle(): void
{
$parameter = new Parameter('test', 'path');
$this->assertSame('simple', $parameter->getStyle());
$this->assertFalse($parameter->getExplode());
}

public function testExplodeDefaultsFalseForHeaderStyle(): void
{
$parameter = new Parameter('test', 'header');
$this->assertSame('simple', $parameter->getStyle());
$this->assertFalse($parameter->getExplode());
}

public function testExplicitExplodeFalseOverridesDefault(): void
{
$parameter = new Parameter('test', 'query', explode: false);
$this->assertSame('form', $parameter->getStyle());
$this->assertFalse($parameter->getExplode());
}

public function testExplicitExplodeTrueOnSimpleStyle(): void
{
$parameter = new Parameter('test', 'path', explode: true);
$this->assertSame('simple', $parameter->getStyle());
$this->assertTrue($parameter->getExplode());
}

public function testExplodeDefaultsTrueForExplicitFormStyle(): void
{
$parameter = new Parameter('test', 'path', style: 'form');
$this->assertTrue($parameter->getExplode());
}

public function testExplodeDefaultsFalseForExplicitDeepObjectStyle(): void
{
$parameter = new Parameter('test', 'query', style: 'deepObject');
$this->assertFalse($parameter->getExplode());
}
}
4 changes: 2 additions & 2 deletions tests/Functional/Parameters/DoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,15 @@ public static function openApiParameterDocumentationProvider(): array
'parameterName' => 'exactBrand',
'shouldHaveArrayNotation' => false,
'expectedStyle' => 'form',
'expectedExplode' => false,
'expectedExplode' => true,
'expectedDescription' => '',
'expectedSchema' => ['type' => 'string'],
],
'castToArray false should not use array notation' => [
'parameterName' => 'exactCategory',
'shouldHaveArrayNotation' => false,
'expectedStyle' => 'form',
'expectedExplode' => false,
'expectedExplode' => true,
'expectedDescription' => '',
'expectedSchema' => ['type' => 'string'],
],
Expand Down
Loading