Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 6446fb8

Browse files
authored
Fix request parameters builder (#22)
1 parent 4ab2b29 commit 6446fb8

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

src/Schema.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ private static function fetch($schema, ...$paths)
310310

311311
/**
312312
* A list of parameters that are applicable for this operation.
313+
*
313314
* If a parameter is already defined at the Path Item,
314315
* the new definition will override it, but can never remove it.
315316
*
@@ -323,10 +324,34 @@ private function getRequestParameters($template, $method, $location)
323324
{
324325
$path = $this->fetch($this->schema, 'paths', $template);
325326

326-
$parameters = $this->fetch($path, $method, 'parameters') ?: $this->fetch($path, 'parameters');
327+
$operationParameters = $this->normalizeRequestParameters(
328+
(array) $this->fetch($path, $method, 'parameters'),
329+
$location
330+
);
331+
332+
$pathParameters = $this->normalizeRequestParameters(
333+
(array) $this->fetch($path, 'parameters'),
334+
$location
335+
);
336+
337+
return $operationParameters + $pathParameters;
338+
}
339+
340+
/**
341+
* Normalizes parameters definitions.
342+
*
343+
* Filter parameters by location, and use name as list index.
344+
*
345+
* @param array $parameters
346+
* @param string $location
347+
*
348+
* @return object[]
349+
*/
350+
private function normalizeRequestParameters(array $parameters, $location)
351+
{
327352
$schemas = [];
328353

329-
foreach ((array) $parameters as $parameter) {
354+
foreach ($parameters as $parameter) {
330355
if ($parameter->in !== $location) {
331356
continue;
332357
}

tests/SchemaTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,57 @@ public function howToUseSwagger(Schema $spec)
167167
$this->assertNull($responseBody);
168168
}
169169

170+
/**
171+
* @test
172+
*/
173+
public function shouldGetUnionOfParameters()
174+
{
175+
$spec = new Schema(
176+
$this->createObject(
177+
[
178+
'swagger' => '2.0',
179+
'host' => 'localhost',
180+
'paths' => [
181+
'/posts' => [
182+
'parameters' => [
183+
[
184+
'name' => 'foo',
185+
'in' => 'header',
186+
'required' => false,
187+
],
188+
[
189+
'name' => 'bar',
190+
'in' => 'header',
191+
],
192+
],
193+
'get' => [
194+
'parameters' => [
195+
[
196+
'name' => 'baz',
197+
'in' => 'header',
198+
],
199+
[
200+
'name' => 'foo',
201+
'in' => 'header',
202+
'required' => true,
203+
],
204+
],
205+
],
206+
],
207+
],
208+
]
209+
)
210+
);
211+
212+
$parameters = $spec->getRequestHeaderSchemas('/posts', 'get');
213+
214+
$this->assertCount(3, $parameters);
215+
$this->assertArrayHasKey('foo', $parameters);
216+
$this->assertArrayHasKey('bar', $parameters);
217+
$this->assertArrayHasKey('baz', $parameters);
218+
$this->assertSame(true, $parameters['foo']->required);
219+
}
220+
170221
/**
171222
* @test
172223
*/

0 commit comments

Comments
 (0)