Skip to content

Commit 4ad2302

Browse files
authored
fix(openapi): default explode to true for form and cookie style param… (#7891)
1 parent 3a6220c commit 4ad2302

4 files changed

Lines changed: 83 additions & 5 deletions

File tree

src/OpenApi/Model/Parameter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class Parameter
1717
{
1818
use ExtensionTrait;
1919

20-
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)
20+
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)
2121
{
2222
if (null === $style) {
2323
if ('query' === $in || 'cookie' === $in) {
@@ -26,6 +26,10 @@ public function __construct(private string $name, private string $in, private st
2626
$this->style = 'simple';
2727
}
2828
}
29+
30+
if (null === $explode) {
31+
$this->explode = \in_array($this->style, ['form', 'cookie'], true);
32+
}
2933
}
3034

3135
public function getName(): string

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,15 +968,15 @@ public function testInvoke(): void
968968
], 'form', true, true, 'bar'),
969969
new Parameter('ha', 'query', '', false, false, null, [
970970
'type' => 'integer',
971-
]),
971+
], 'form', false),
972972
new Parameter('toto', 'query', '', true, false, null, [
973973
'type' => 'array',
974974
'items' => ['type' => 'string'],
975975
], 'deepObject', true),
976976
new Parameter('order[name]', 'query', '', false, false, null, [
977977
'type' => 'string',
978978
'enum' => ['asc', 'desc'],
979-
]),
979+
], 'form', false),
980980
],
981981
), $filteredPath->getGet());
982982

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\OpenApi\Tests\Model;
15+
16+
use ApiPlatform\OpenApi\Model\Parameter;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ParameterTest extends TestCase
20+
{
21+
public function testExplodeDefaultsTrueForFormStyle(): void
22+
{
23+
$parameter = new Parameter('test', 'query');
24+
$this->assertSame('form', $parameter->getStyle());
25+
$this->assertTrue($parameter->getExplode());
26+
}
27+
28+
public function testExplodeDefaultsTrueForCookieStyle(): void
29+
{
30+
$parameter = new Parameter('test', 'cookie');
31+
$this->assertSame('form', $parameter->getStyle());
32+
$this->assertTrue($parameter->getExplode());
33+
}
34+
35+
public function testExplodeDefaultsFalseForPathStyle(): void
36+
{
37+
$parameter = new Parameter('test', 'path');
38+
$this->assertSame('simple', $parameter->getStyle());
39+
$this->assertFalse($parameter->getExplode());
40+
}
41+
42+
public function testExplodeDefaultsFalseForHeaderStyle(): void
43+
{
44+
$parameter = new Parameter('test', 'header');
45+
$this->assertSame('simple', $parameter->getStyle());
46+
$this->assertFalse($parameter->getExplode());
47+
}
48+
49+
public function testExplicitExplodeFalseOverridesDefault(): void
50+
{
51+
$parameter = new Parameter('test', 'query', explode: false);
52+
$this->assertSame('form', $parameter->getStyle());
53+
$this->assertFalse($parameter->getExplode());
54+
}
55+
56+
public function testExplicitExplodeTrueOnSimpleStyle(): void
57+
{
58+
$parameter = new Parameter('test', 'path', explode: true);
59+
$this->assertSame('simple', $parameter->getStyle());
60+
$this->assertTrue($parameter->getExplode());
61+
}
62+
63+
public function testExplodeDefaultsTrueForExplicitFormStyle(): void
64+
{
65+
$parameter = new Parameter('test', 'path', style: 'form');
66+
$this->assertTrue($parameter->getExplode());
67+
}
68+
69+
public function testExplodeDefaultsFalseForExplicitDeepObjectStyle(): void
70+
{
71+
$parameter = new Parameter('test', 'query', style: 'deepObject');
72+
$this->assertFalse($parameter->getExplode());
73+
}
74+
}

tests/Functional/Parameters/DoctrineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,15 @@ public static function openApiParameterDocumentationProvider(): array
410410
'parameterName' => 'exactBrand',
411411
'shouldHaveArrayNotation' => false,
412412
'expectedStyle' => 'form',
413-
'expectedExplode' => false,
413+
'expectedExplode' => true,
414414
'expectedDescription' => '',
415415
'expectedSchema' => ['type' => 'string'],
416416
],
417417
'castToArray false should not use array notation' => [
418418
'parameterName' => 'exactCategory',
419419
'shouldHaveArrayNotation' => false,
420420
'expectedStyle' => 'form',
421-
'expectedExplode' => false,
421+
'expectedExplode' => true,
422422
'expectedDescription' => '',
423423
'expectedSchema' => ['type' => 'string'],
424424
],

0 commit comments

Comments
 (0)