Skip to content

Commit 16c298f

Browse files
committed
object syntax body param
1 parent 157361b commit 16c298f

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed

src/Parameters/BodyParameterGenerator.php

+29-10
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,23 @@ protected function addToProperties(&$properties, $nameTokens, $rules)
6666
$type = $this->getParamType($rules);
6767
}
6868

69-
$propObj = [
70-
'type' => $type
71-
];
72-
73-
if ($enums = $this->getEnumValues($rules)) {
74-
$propObj['enum'] = $enums;
75-
}
76-
7769
if ($name === '*') {
7870
$name = 0;
7971
}
8072

81-
$properties[$name] = $propObj;
73+
if (!isset($properties[$name])) {
74+
$propObj = $this->getNewPropObj($type, $rules);
75+
76+
$properties[$name] = $propObj;
77+
} else {
78+
//overwrite previous type in case it wasn't given before
79+
$properties[$name]['type'] = $type;
80+
}
8281

8382
if ($type === 'array') {
84-
$properties[$name]['items'] = [];
8583
$this->addToProperties($properties[$name]['items'], $nameTokens, $rules);
84+
} else if ($type === 'object') {
85+
$this->addToProperties($properties[$name]['properties'], $nameTokens, $rules);
8686
}
8787
}
8888

@@ -94,4 +94,23 @@ protected function getNestedParamType($nameTokens)
9494
return 'object';
9595
}
9696
}
97+
98+
protected function getNewPropObj($type, $rules)
99+
{
100+
$propObj = [
101+
'type' => $type
102+
];
103+
104+
if ($enums = $this->getEnumValues($rules)) {
105+
$propObj['enum'] = $enums;
106+
}
107+
108+
if ($type === 'array') {
109+
$propObj['items'] = [];
110+
} else if ($type === 'object') {
111+
$propObj['properties'] = [];
112+
}
113+
114+
return $propObj;
115+
}
97116
}

tests/Parameters/BodyParameterGeneratorTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,57 @@ public function testArraySyntax()
101101
], $bodyParameters['schema']['properties']);
102102
}
103103

104+
public function testObjectInArraySyntax()
105+
{
106+
$bodyParameters = $this->getBodyParameters([
107+
'points' => 'array',
108+
'points.*.x' => 'numeric',
109+
'points.*.y' => 'numeric',
110+
]);
111+
112+
$this->assertEquals([
113+
'points' => [
114+
'type' => 'array',
115+
'items' => [
116+
[
117+
'type' => 'object',
118+
'properties' => [
119+
'x' => [
120+
'type' => 'number'
121+
],
122+
'y' => [
123+
'type' => 'number'
124+
],
125+
]
126+
]
127+
]
128+
]
129+
], $bodyParameters['schema']['properties']);
130+
}
131+
132+
public function testSingleObjectSyntax()
133+
{
134+
$bodyParameters = $this->getBodyParameters([
135+
'point' => '',
136+
'point.x' => 'numeric',
137+
'point.y' => 'numeric',
138+
]);
139+
140+
$this->assertEquals([
141+
'point' => [
142+
'type' => 'object',
143+
'properties' => [
144+
'x' => [
145+
'type' => 'number'
146+
],
147+
'y' => [
148+
'type' => 'number'
149+
],
150+
]
151+
]
152+
], $bodyParameters['schema']['properties']);
153+
}
154+
104155
private function getBodyParameters(array $rules)
105156
{
106157
$bodyParameters = (new BodyParameterGenerator($rules))->getParameters();

0 commit comments

Comments
 (0)