Skip to content

Commit

Permalink
Allow mapping null response values to defined response model properties
Browse files Browse the repository at this point in the history
  • Loading branch information
deshmukhsN committed Aug 19, 2015
1 parent 9392fc2 commit e9ed144
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ResponseLocation/JsonLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function recurse(Parameter $param, $value)
if ($properties = $param->getProperties()) {
foreach ($properties as $property) {
$key = $property->getWireName();
if (isset($value[$key])) {
if (array_key_exists($key, $value)) {
$result[$property->getName()] = $this->recurse(
$property,
$value[$key]
Expand Down
60 changes: 60 additions & 0 deletions tests/ResponseLocation/JsonLocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,64 @@ public function testVisitsNestedProperties($desc)

$this->assertEquals($expected, $result);
}

public function testVisitsNullResponseProperties()
{
$hclient = new Client();

$hclient->getEmitter()->on('before', function (BeforeEvent $event) {
$json = [
'data' => [
'link' => null
]
];
$response = new Response(200, [
'Content-Type' => 'application/json'
], Stream::factory(json_encode($json)));
$event->intercept($response);
});

$description = new Description(
[
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'responseModel' => 'j'
]
],
'models' => [
'j' => [
'type' => 'object',
'location' => 'json',
'properties' => [
'scalar' => ['type' => 'string'],
'data' => [
'type' => 'object',
'location' => 'json',
'properties' => [
'link' => [
'name' => 'val',
'type' => 'string',
'location' => 'json'
],
],
'additionalProperties' => false
]
]
]
]
]
);
$client = new GuzzleClient($hclient, $description);
$result = $client->foo();

$expected = [
'data' => [
'link' => null
]
];

$this->assertEquals($expected, $result);
}
}

0 comments on commit e9ed144

Please sign in to comment.