diff --git a/src/OpenApi/Model/Parameter.php b/src/OpenApi/Model/Parameter.php index 414bb356c67..ab8af9208cb 100644 --- a/src/OpenApi/Model/Parameter.php +++ b/src/OpenApi/Model/Parameter.php @@ -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) { @@ -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 diff --git a/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php b/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php index d8773f8c7a6..c956e108082 100644 --- a/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php +++ b/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php @@ -968,7 +968,7 @@ 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'], @@ -976,7 +976,7 @@ public function testInvoke(): void new Parameter('order[name]', 'query', '', false, false, null, [ 'type' => 'string', 'enum' => ['asc', 'desc'], - ]), + ], 'form', false), ], ), $filteredPath->getGet()); diff --git a/src/OpenApi/Tests/Model/ParameterTest.php b/src/OpenApi/Tests/Model/ParameterTest.php new file mode 100644 index 00000000000..e3e3218397a --- /dev/null +++ b/src/OpenApi/Tests/Model/ParameterTest.php @@ -0,0 +1,74 @@ + + * + * 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()); + } +} diff --git a/tests/Functional/Parameters/DoctrineTest.php b/tests/Functional/Parameters/DoctrineTest.php index 8fe301f5237..9631e084399 100644 --- a/tests/Functional/Parameters/DoctrineTest.php +++ b/tests/Functional/Parameters/DoctrineTest.php @@ -410,7 +410,7 @@ public static function openApiParameterDocumentationProvider(): array 'parameterName' => 'exactBrand', 'shouldHaveArrayNotation' => false, 'expectedStyle' => 'form', - 'expectedExplode' => false, + 'expectedExplode' => true, 'expectedDescription' => '', 'expectedSchema' => ['type' => 'string'], ], @@ -418,7 +418,7 @@ public static function openApiParameterDocumentationProvider(): array 'parameterName' => 'exactCategory', 'shouldHaveArrayNotation' => false, 'expectedStyle' => 'form', - 'expectedExplode' => false, + 'expectedExplode' => true, 'expectedDescription' => '', 'expectedSchema' => ['type' => 'string'], ],