Skip to content

Commit d14ba44

Browse files
author
Guy Elsmore-Paddock
committed
[#166] Default 'httpMethod' to 'GET' to Ensure PSR-7 1.6.x Compatibility
PSR-7 requires a method to be set on Request objects at the time they are created, but the Operation object previously defaulted the method to an empty string. Now we default it to 'GET', which should be a safe choice. In addition, if a request is misconfigured to force the request method to an empty string, there is now a validation for that. Closes #166.
1 parent 9e3abf2 commit d14ba44

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Operation.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(array $config = [], DescriptionInterface $descriptio
5353
{
5454
static $defaults = [
5555
'name' => '',
56-
'httpMethod' => '',
56+
'httpMethod' => 'GET',
5757
'uri' => '',
5858
'responseModel' => null,
5959
'notes' => '',
@@ -72,6 +72,10 @@ public function __construct(array $config = [], DescriptionInterface $descriptio
7272
$config = $this->resolveExtends($config['extends'], $config);
7373
}
7474

75+
if (array_key_exists('httpMethod', $config) && empty($config['httpMethod'])) {
76+
throw new \InvalidArgumentException('httpMethod must be a non-empty string');
77+
}
78+
7579
$this->config = $config + $defaults;
7680

7781
// Account for the old style of using responseClass

tests/OperationTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,27 @@ public function testHasData()
122122
$this->assertEquals(['foo' => 'baz', 'bar' => 123], $o->getData());
123123
}
124124

125+
public function testDefaultsHttpMethodToGET()
126+
{
127+
$o = new Operation();
128+
$this->assertEquals('GET', $o->getHttpMethod());
129+
}
130+
131+
public function testCanProvideAlternateHttpMethod()
132+
{
133+
$o = new Operation(['httpMethod' => 'POST']);
134+
$this->assertEquals('POST', $o->getHttpMethod());
135+
}
136+
137+
/**
138+
* @expectedException \InvalidArgumentException
139+
* @expectedExceptionMesssage httpMethod must be a non-empty string
140+
*/
141+
public function testEnsuresHttpMethodIsNotEmptyString()
142+
{
143+
new Operation(['httpMethod' => '']);
144+
}
145+
125146
/**
126147
* @expectedException \InvalidArgumentException
127148
* @expectedExceptionMesssage Parameters must be arrays
@@ -196,6 +217,7 @@ public function testCanExtendFromOtherOperations()
196217
'summary' => 'foo'
197218
],
198219
'B' => [
220+
'httpMethod' => 'POST',
199221
'extends' => 'A',
200222
'summary' => 'Bar'
201223
],
@@ -210,17 +232,20 @@ public function testCanExtendFromOtherOperations()
210232
]);
211233

212234
$a = $d->getOperation('A');
235+
$this->assertEquals('GET', $a->getHttpMethod());
213236
$this->assertEquals('foo', $a->getSummary());
214237
$this->assertTrue($a->hasParam('A'));
215238
$this->assertEquals('string', $a->getParam('B')->getType());
216239

217240
$b = $d->getOperation('B');
218241
$this->assertTrue($a->hasParam('A'));
242+
$this->assertEquals('POST', $b->getHttpMethod());
219243
$this->assertEquals('Bar', $b->getSummary());
220244
$this->assertEquals('string', $a->getParam('B')->getType());
221245

222246
$c = $d->getOperation('C');
223247
$this->assertTrue($a->hasParam('A'));
248+
$this->assertEquals('POST', $c->getHttpMethod());
224249
$this->assertEquals('Bar', $c->getSummary());
225250
$this->assertEquals('number', $c->getParam('B')->getType());
226251
}

0 commit comments

Comments
 (0)