Skip to content

Commit 91084f6

Browse files
committed
fixed array.* syntax in query param
1 parent a4167cb commit 91084f6

File tree

6 files changed

+101
-5
lines changed

6 files changed

+101
-5
lines changed

src/FormatterManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function setFormat($format)
2626

2727
protected function getFormatter($format)
2828
{
29-
switch($format) {
29+
switch ($format) {
3030
case 'json':
3131
return new Formatters\JsonFormatter($this->docs);
3232
case 'yaml':

src/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected function getFormRules()
150150

151151
protected function getParameterGenerator($rules)
152152
{
153-
switch($this->method) {
153+
switch ($this->method) {
154154
case 'post':
155155
case 'put':
156156
case 'patch':

src/Parameters/BodyParameterGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function getParameters()
2727
],
2828
];
2929

30-
foreach ($this->rules as $param => $rule) {
30+
foreach ($this->rules as $param => $rule) {
3131
$paramRules = $this->splitRules($rule);
3232
$enums = $this->getEnumValues($paramRules);
3333

src/Parameters/Concerns/GeneratesFromRules.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ protected function isParamRequired(array $paramRules)
3434
return in_array('required', $paramRules);
3535
}
3636

37+
protected function isArrayParameter($param)
38+
{
39+
return str_contains($param, '*');
40+
}
41+
42+
protected function getArrayKey($param)
43+
{
44+
return current(explode('.', $param));
45+
}
46+
3747
protected function getEnumValues(array $paramRules)
3848
{
3949
$in = $this->getInParameter($paramRules);

src/Parameters/QueryParameterGenerator.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ public function __construct($rules)
1616
public function getParameters()
1717
{
1818
$params = [];
19+
$arrayTypes = [];
1920

2021
foreach ($this->rules as $param => $rule) {
2122
$paramRules = $this->splitRules($rule);
2223
$enums = $this->getEnumValues($paramRules);
24+
$type = $this->getParamType($paramRules);
25+
26+
if ($this->isArrayParameter($param)) {
27+
$arrayKey = $this->getArrayKey($param);
28+
$arrayTypes[$arrayKey] = $type;
29+
continue;
30+
}
2331

2432
$paramObj = [
2533
'in' => $this->getParamLocation(),
2634
'name' => $param,
27-
'type' => $this->getParamType($paramRules),
35+
'type' => $type,
2836
'required' => $this->isParamRequired($paramRules),
2937
'description' => '',
3038
];
@@ -33,7 +41,36 @@ public function getParameters()
3341
$paramObj['enum'] = $enums;
3442
}
3543

36-
$params[] = $paramObj;
44+
if ($type === 'array') {
45+
$paramObj['items'] = ['type' => 'string'];
46+
}
47+
48+
$params[$param] = $paramObj;
49+
}
50+
51+
$params = $this->addArrayTypes($params, $arrayTypes);
52+
53+
return array_values($params);
54+
}
55+
56+
protected function addArrayTypes($params, $arrayTypes)
57+
{
58+
foreach ($arrayTypes as $arrayKey => $type) {
59+
if (!isset($params[$arrayKey])) {
60+
$params[$arrayKey] = [
61+
'in' => $this->getParamLocation(),
62+
'name' => $arrayKey,
63+
'type' => 'array',
64+
'required' => false,
65+
'description' => '',
66+
'items' => [
67+
'type' => $type,
68+
],
69+
];
70+
} else {
71+
$params[$arrayKey]['type'] = 'array';
72+
$params[$arrayKey]['items']['type'] = $type;
73+
}
3774
}
3875

3976
return $params;

tests/Parameters/QueryParameterGeneratorTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,55 @@ public function testEnumRuleObjet()
7979
], $queryParameters[0]);
8080
}
8181

82+
public function testArrayTypeDefaultsToString()
83+
{
84+
$queryParameters = $this->getQueryParameters([
85+
'values' => 'array',
86+
]);
87+
88+
$this->assertArraySubset([
89+
'name' => 'values',
90+
'type' => 'array',
91+
'required' => false,
92+
'items' => [
93+
'type' => 'string',
94+
],
95+
], $queryParameters[0]);
96+
}
97+
98+
public function testArrayValidationSyntax()
99+
{
100+
$queryParameters = $this->getQueryParameters([
101+
'values.*' => 'integer',
102+
]);
103+
104+
$this->assertArraySubset([
105+
'name' => 'values',
106+
'type' => 'array',
107+
'required' => false,
108+
'items' => [
109+
'type' => 'integer',
110+
],
111+
], $queryParameters[0]);
112+
}
113+
114+
public function testArrayValidationSyntaxWithRequiredArray()
115+
{
116+
$queryParameters = $this->getQueryParameters([
117+
'values.*' => 'integer',
118+
'values' => 'required',
119+
]);
120+
121+
$this->assertArraySubset([
122+
'name' => 'values',
123+
'type' => 'array',
124+
'required' => true,
125+
'items' => [
126+
'type' => 'integer',
127+
],
128+
], $queryParameters[0]);
129+
}
130+
82131
private function getQueryParameters(array $rules)
83132
{
84133
return (new QueryParameterGenerator($rules))->getParameters();

0 commit comments

Comments
 (0)