Skip to content

Commit

Permalink
Merge pull request aws#697 from jeskew/fix/xml-errors
Browse files Browse the repository at this point in the history
Sync updated protocol test cases and apply necessary fixes
  • Loading branch information
jeskew committed Jul 20, 2015
2 parents 9db359f + f887300 commit 0ebae87
Show file tree
Hide file tree
Showing 9 changed files with 438 additions and 141 deletions.
34 changes: 23 additions & 11 deletions src/Api/Parser/AbstractRestParser.php
Expand Up @@ -89,18 +89,30 @@ private function extractHeader(
&$result
) {
$value = $response->getHeaderLine($shape['locationName'] ?: $name);
$type = $shape->getType();

if ($type === 'blob') {
$value = base64_decode($value);
} elseif ($type === 'timestamp') {
try {
$value = new DateTimeResult($value);
} catch (\Exception $e) {
// If the value cannot be parsed, then do not add it to the
// output structure.
return;
}
switch ($shape->getType()) {
case 'float':
case 'double':
$value = (float) $value;
break;
case 'long':
$value = (int) $value;
break;
case 'boolean':
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
break;
case 'blob':
$value = base64_decode($value);
break;
case 'timestamp':
try {
$value = new DateTimeResult($value);
break;
} catch (\Exception $e) {
// If the value cannot be parsed, then do not add it to the
// output structure.
return;
}
}

$result[$name] = $value;
Expand Down
6 changes: 5 additions & 1 deletion src/Api/Parser/XmlParser.php
Expand Up @@ -58,11 +58,15 @@ private function parse_structure(

private function memberKey(Shape $shape, $name)
{
if (null !== $shape['locationName']) {
return $shape['locationName'];
}

if ($shape instanceof ListShape && $shape['flattened']) {
return $shape->getMember()['locationName'] ?: $name;
}

return $shape['locationName'] ?: $name;
return $name;
}

private function parse_list(ListShape $shape, \SimpleXMLElement $value)
Expand Down
42 changes: 33 additions & 9 deletions src/Api/Serializer/XmlBody.php
@@ -1,11 +1,13 @@
<?php
namespace Aws\Api\Serializer;

use Aws\Api\MapShape;
use Aws\Api\Service;
use Aws\Api\Shape;
use Aws\Api\StructureShape;
use Aws\Api\ListShape;
use Aws\Api\TimestampShape;
use XMLWriter;

/**
* @internal Formats the XML body of a REST-XML services.
Expand Down Expand Up @@ -33,7 +35,7 @@ public function __construct(Service $api)
*/
public function build(Shape $shape, array $args)
{
$xml = new \XMLWriter();
$xml = new XMLWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$this->format($shape, $shape['locationName'], $args, $xml);
Expand All @@ -42,7 +44,7 @@ public function build(Shape $shape, array $args)
return $xml->outputMemory();
}

private function startElement(Shape $shape, $name, \XMLWriter $xml)
private function startElement(Shape $shape, $name, XMLWriter $xml)
{
$xml->startElement($name);

Expand All @@ -54,7 +56,7 @@ private function startElement(Shape $shape, $name, \XMLWriter $xml)
}
}

private function format(Shape $shape, $name, $value, \XMLWriter $xml)
private function format(Shape $shape, $name, $value, XMLWriter $xml)
{
// Any method mentioned here has a custom serialization handler.
static $methods = [
Expand All @@ -75,7 +77,7 @@ private function format(Shape $shape, $name, $value, \XMLWriter $xml)
}
}

private function defaultShape(Shape $shape, $name, $value, \XMLWriter $xml)
private function defaultShape(Shape $shape, $name, $value, XMLWriter $xml)
{
$this->startElement($shape, $name, $xml);
$xml->writeRaw($value);
Expand Down Expand Up @@ -129,7 +131,7 @@ private function add_list(
ListShape $shape,
$name,
array $value,
\XMLWriter $xml
XMLWriter $xml
) {
$items = $shape->getMember();

Expand All @@ -149,7 +151,29 @@ private function add_list(
}
}

private function add_blob(Shape $shape, $name, $value, \XMLWriter $xml)
private function add_map(
MapShape $shape,
$name,
array $value,
XMLWriter $xml
) {
$xmlEntry = $shape['flattened'] ? $shape['locationName'] : 'entry';
$xmlKey = $shape->getKey()['locationName'] ?: 'key';
$xmlValue = $shape->getValue()['locationName'] ?: 'value';

$this->startElement($shape, $name, $xml);

foreach ($value as $key => $v) {
$this->startElement($shape, $xmlEntry, $xml);
$this->format($shape->getKey(), $xmlKey, $key, $xml);
$this->format($shape->getValue(), $xmlValue, $v, $xml);
$xml->endElement();
}

$xml->endElement();
}

private function add_blob(Shape $shape, $name, $value, XMLWriter $xml)
{
$this->startElement($shape, $name, $xml);
$xml->writeRaw(base64_encode($value));
Expand All @@ -160,7 +184,7 @@ private function add_timestamp(
TimestampShape $shape,
$name,
$value,
\XMLWriter $xml
XMLWriter $xml
) {
$this->startElement($shape, $name, $xml);
$xml->writeRaw(TimestampShape::format($value, 'iso8601'));
Expand All @@ -171,7 +195,7 @@ private function add_boolean(
Shape $shape,
$name,
$value,
\XMLWriter $xml
XMLWriter $xml
) {
$this->startElement($shape, $name, $xml);
$xml->writeRaw($value ? 'true' : 'false');
Expand All @@ -182,7 +206,7 @@ private function add_string(
Shape $shape,
$name,
$value,
\XMLWriter $xml
XMLWriter $xml
) {
if ($shape['xmlAttribute']) {
$xml->writeAttribute($shape['locationName'] ?: $name, $value);
Expand Down

0 comments on commit 0ebae87

Please sign in to comment.