forked from loafle/openapi-generator-original
[Php] Exclude query params when they're not required (#12120)
* exclude query params when they are not required * fix check on empty * update samples * fix * add tests * update test * Fix style I guess PHPCodeSniffer would find PSR12 violated but we use CS-Fixer instead. Anyway, conditions should contain spaces between logical operators for readability. * Apply CS-Fixer changes to templates * Refresh samples * Add required param to docblock Co-authored-by: Yuriy Belenko <yura-bely@mail.ru>
This commit is contained in:
@@ -141,9 +141,10 @@ class ObjectSerializerTest extends TestCase
|
||||
string $openApiType,
|
||||
string $style,
|
||||
bool $explode,
|
||||
bool $required,
|
||||
$expected
|
||||
): void {
|
||||
$value = ObjectSerializer::toQueryValue($data, $paramName, $openApiType, $style, $explode);
|
||||
$value = ObjectSerializer::toQueryValue($data, $paramName, $openApiType, $style, $explode, $required);
|
||||
$query = ObjectSerializer::buildQuery($value);
|
||||
$this->assertEquals($expected, $query);
|
||||
}
|
||||
@@ -155,124 +156,137 @@ class ObjectSerializerTest extends TestCase
|
||||
|
||||
return [
|
||||
// style form
|
||||
// color=
|
||||
'form empty, explode on' => [
|
||||
'', 'color', 'string', 'form', true, 'color=',
|
||||
// skipValidation
|
||||
'form boolean, explode on, required false' => [
|
||||
false, 'skipValidation', 'boolean', 'form', true, false, 'skipValidation=0',
|
||||
],
|
||||
'form empty boolean, explode on, required false' => [
|
||||
null, 'skipValidation', 'boolean', 'form', true, false, '',
|
||||
],
|
||||
'form empty boolean, explode on, required true' => [
|
||||
null, 'skipValidation', 'boolean', 'form', true, true, 'skipValidation=',
|
||||
],
|
||||
// color=
|
||||
'form empty, explode off' => [
|
||||
'', 'color', 'string', 'form', false, 'color=',
|
||||
'form empty, explode on, required true' => [
|
||||
'', 'color', 'string', 'form', true, true, 'color=',
|
||||
],
|
||||
'form empty, explode on, required false' => [
|
||||
'', 'color', 'string', 'form', true, false, '',
|
||||
],
|
||||
// color=
|
||||
'form empty, explode off, required true' => [
|
||||
'', 'color', 'string', 'form', false, true, 'color=',
|
||||
],
|
||||
// color=blue
|
||||
'form string, explode on' => [
|
||||
'blue', 'color', 'string', 'form', true, 'color=blue',
|
||||
'form string, explode on, required true' => [
|
||||
'blue', 'color', 'string', 'form', true, true, 'color=blue',
|
||||
],
|
||||
// color=blue
|
||||
'form string, explode off' => [
|
||||
'blue', 'color', 'string', 'form', false, 'color=blue',
|
||||
'form string, explode off, required true' => [
|
||||
'blue', 'color', 'string', 'form', false, true, 'color=blue',
|
||||
],
|
||||
// color=blue&color=black&color=brown
|
||||
'form array, explode on' => [
|
||||
$array, 'color', 'array', 'form', true, 'color=blue&color=black&color=brown',
|
||||
'form array, explode on, required true' => [
|
||||
$array, 'color', 'array', 'form', true, true, 'color=blue&color=black&color=brown',
|
||||
],
|
||||
// color=blue&color=black&color=brown
|
||||
'form nested array, explode on' => [
|
||||
['foobar' => $array], 'color', 'array', 'form', true, 'color=blue&color=black&color=brown',
|
||||
'form nested array, explode on, required true' => [
|
||||
['foobar' => $array], 'color', 'array', 'form', true, true, 'color=blue&color=black&color=brown',
|
||||
],
|
||||
// color=blue,black,brown
|
||||
'form array, explode off' => [
|
||||
$array, 'color', 'array', 'form', false, 'color=blue%2Cblack%2Cbrown',
|
||||
'form array, explode off, required true' => [
|
||||
$array, 'color', 'array', 'form', false, true, 'color=blue%2Cblack%2Cbrown',
|
||||
],
|
||||
// color=blue,black,brown
|
||||
'form nested array, explode off' => [
|
||||
['foobar' => $array], 'color', 'array', 'form', false, 'color=blue%2Cblack%2Cbrown',
|
||||
'form nested array, explode off, required true' => [
|
||||
['foobar' => $array], 'color', 'array', 'form', false, true, 'color=blue%2Cblack%2Cbrown',
|
||||
],
|
||||
// R=100&G=200&B=150
|
||||
'form object, explode on' => [
|
||||
$object, 'color', 'object', 'form', true, 'R=100&G=200&B=150',
|
||||
'form object, explode on, required true' => [
|
||||
$object, 'color', 'object', 'form', true, true, 'R=100&G=200&B=150',
|
||||
],
|
||||
// color=R,100,G,200,B,150
|
||||
'form object, explode off' => [
|
||||
$object, 'color', 'object', 'form', false, 'color=R%2C100%2CG%2C200%2CB%2C150',
|
||||
'form object, explode off, required true' => [
|
||||
$object, 'color', 'object', 'form', false, true, 'color=R%2C100%2CG%2C200%2CB%2C150',
|
||||
],
|
||||
|
||||
// SPACE DELIMITED
|
||||
// color=blue
|
||||
'spaceDelimited primitive, explode off' => [
|
||||
'blue', 'color', 'string', 'spaceDelimited', false, 'color=blue',
|
||||
'spaceDelimited primitive, explode off, required true' => [
|
||||
'blue', 'color', 'string', 'spaceDelimited', false, true, 'color=blue',
|
||||
],
|
||||
// color=blue
|
||||
'spaceDelimited primitive, explode on' => [
|
||||
'blue', 'color', 'string', 'spaceDelimited', true, 'color=blue',
|
||||
'spaceDelimited primitive, explode on, required true' => [
|
||||
'blue', 'color', 'string', 'spaceDelimited', true, true, 'color=blue',
|
||||
],
|
||||
// color=blue%20black%20brown
|
||||
'spaceDelimited array, explode off' => [
|
||||
$array, 'color', 'array', 'spaceDelimited', false, 'color=blue%20black%20brown',
|
||||
'spaceDelimited array, explode off, required true' => [
|
||||
$array, 'color', 'array', 'spaceDelimited', false, true, 'color=blue%20black%20brown',
|
||||
],
|
||||
// color=blue&color=black&color=brown
|
||||
'spaceDelimited array, explode on' => [
|
||||
$array, 'color', 'array', 'spaceDelimited', true, 'color=blue&color=black&color=brown',
|
||||
'spaceDelimited array, explode on, required true' => [
|
||||
$array, 'color', 'array', 'spaceDelimited', true, true, 'color=blue&color=black&color=brown',
|
||||
],
|
||||
// color=R%20100%20G%20200%20B%20150
|
||||
// swagger editor gives color=R,100,G,200,B,150
|
||||
'spaceDelimited object, explode off' => [
|
||||
$object, 'color', 'object', 'spaceDelimited', false, 'color=R%20100%20G%20200%20B%20150',
|
||||
'spaceDelimited object, explode off, required true' => [
|
||||
$object, 'color', 'object', 'spaceDelimited', false, true, 'color=R%20100%20G%20200%20B%20150',
|
||||
],
|
||||
// R=100&G=200&B=150
|
||||
'spaceDelimited object, explode on' => [
|
||||
$object, 'color', 'object', 'spaceDelimited', true, 'R=100&G=200&B=150',
|
||||
'spaceDelimited object, explode on, required true' => [
|
||||
$object, 'color', 'object', 'spaceDelimited', true, true, 'R=100&G=200&B=150',
|
||||
],
|
||||
|
||||
// PIPE DELIMITED
|
||||
// color=blue
|
||||
'pipeDelimited primitive, explode off' => [
|
||||
'blue', 'color', 'string', 'pipeDelimited', false, 'color=blue',
|
||||
'pipeDelimited primitive, explode off, required true' => [
|
||||
'blue', 'color', 'string', 'pipeDelimited', false, true, 'color=blue',
|
||||
],
|
||||
// color=blue
|
||||
'pipeDelimited primitive, explode on' => [
|
||||
'blue', 'color', 'string', 'pipeDelimited', true, 'color=blue',
|
||||
'pipeDelimited primitive, explode on, required true' => [
|
||||
'blue', 'color', 'string', 'pipeDelimited', true, true, 'color=blue',
|
||||
],
|
||||
// color=blue|black|brown
|
||||
'pipeDelimited array, explode off' => [
|
||||
$array, 'color', 'array', 'pipeDelimited', false, 'color=blue%7Cblack%7Cbrown',
|
||||
'pipeDelimited array, explode off, required true' => [
|
||||
$array, 'color', 'array', 'pipeDelimited', false, true, 'color=blue%7Cblack%7Cbrown',
|
||||
],
|
||||
// color=blue&color=black&color=brown
|
||||
'pipeDelimited array, explode on' => [
|
||||
$array, 'color', 'array', 'pipeDelimited', true, 'color=blue&color=black&color=brown',
|
||||
'pipeDelimited array, explode on, required true' => [
|
||||
$array, 'color', 'array', 'pipeDelimited', true, true, 'color=blue&color=black&color=brown',
|
||||
],
|
||||
// color=R|100|G|200|B|150
|
||||
// swagger editor gives color=R,100,G,200,B,150
|
||||
'pipeDelimited object, explode off' => [
|
||||
$object, 'color', 'object', 'pipeDelimited', false, 'color=R%7C100%7CG%7C200%7CB%7C150',
|
||||
'pipeDelimited object, explode off, required true' => [
|
||||
$object, 'color', 'object', 'pipeDelimited', false, true, 'color=R%7C100%7CG%7C200%7CB%7C150',
|
||||
],
|
||||
// R=100&G=200&B=150
|
||||
'pipeDelimited object, explode on' => [
|
||||
$object, 'color', 'object', 'pipeDelimited', true, 'R=100&G=200&B=150',
|
||||
'pipeDelimited object, explode on, required true' => [
|
||||
$object, 'color', 'object', 'pipeDelimited', true, true, 'R=100&G=200&B=150',
|
||||
],
|
||||
|
||||
// DEEP OBJECT
|
||||
// color=blue
|
||||
'deepObject primitive, explode off' => [
|
||||
'blue', 'color', 'string', 'deepObject', false, 'color=blue',
|
||||
'deepObject primitive, explode off, required true' => [
|
||||
'blue', 'color', 'string', 'deepObject', false, true, 'color=blue',
|
||||
],
|
||||
'deepObject primitive, explode on' => [
|
||||
'blue', 'color', 'string', 'deepObject', true, 'color=blue',
|
||||
'deepObject primitive, explode on, required true' => [
|
||||
'blue', 'color', 'string', 'deepObject', true, true, 'color=blue',
|
||||
],
|
||||
// color=blue,black,brown
|
||||
'deepObject array, explode off' => [
|
||||
$array, 'color', 'array', 'deepObject', false, 'color=blue%2Cblack%2Cbrown',
|
||||
'deepObject array, explode off, required true' => [
|
||||
$array, 'color', 'array', 'deepObject', false, true, 'color=blue%2Cblack%2Cbrown',
|
||||
],
|
||||
// color=blue&color=black&color=brown
|
||||
'deepObject array, explode on' => [
|
||||
$array, 'color', 'array', 'deepObject', true, 'color=blue&color=black&color=brown',
|
||||
'deepObject array, explode on, required true' => [
|
||||
$array, 'color', 'array', 'deepObject', true, true, 'color=blue&color=black&color=brown',
|
||||
],
|
||||
// color[R]=100&color[G]=200&color[B]=150
|
||||
'deepObject object, explode off' => [
|
||||
$object, 'color', 'object', 'deepObject', false, 'color%5BR%5D=100&color%5BG%5D=200&color%5BB%5D=150',
|
||||
'deepObject object, explode off, required true' => [
|
||||
$object, 'color', 'object', 'deepObject', false, true, 'color%5BR%5D=100&color%5BG%5D=200&color%5BB%5D=150',
|
||||
],
|
||||
// color[R]=100&color[G]=200&color[B]=150
|
||||
'deepObject object, explode on' => [
|
||||
$object, 'color', 'object', 'deepObject', true, 'color%5BR%5D=100&color%5BG%5D=200&color%5BB%5D=150',
|
||||
'deepObject object, explode on, required true' => [
|
||||
$object, 'color', 'object', 'deepObject', true, true, 'color%5BR%5D=100&color%5BG%5D=200&color%5BB%5D=150',
|
||||
],
|
||||
// filter[or][0][name]=John&filter[or][1][email]=john@doe.com
|
||||
'example from @nadar' => [
|
||||
@@ -287,6 +301,7 @@ class ObjectSerializerTest extends TestCase
|
||||
'object',
|
||||
'deepObject',
|
||||
true,
|
||||
true,
|
||||
'filter%5Bor%5D%5B0%5D%5Bname%5D=John&filter%5Bor%5D%5B1%5D%5Bemail%5D=john%40doe.com'
|
||||
],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user