| @@ -13,10 +13,10 @@ | ||
| 13 | 13 | * @internal Formats the XML body of a REST-XML services. |
| 14 | 14 | */ |
| 15 | 15 | class XmlBody |
| 16 | 16 | { |
| 17 | - /** @var \Aws\Api\Service */ | |
| 18 | - private $api; | |
| 17 | + /** @var Service */ | |
| 18 | + private Service $api; | |
| 19 | 19 | /** |
| 20 | 20 | * @param Service $api API being used to create the XML body. |
| 21 | 21 | */ |
| 22 | 22 | public function __construct(Service $api) |
| @@ -35,9 +35,10 @@ | ||
| 35 | 35 | { |
| 36 | 36 | $xml = new XMLWriter(); |
| 37 | 37 | $xml->openMemory(); |
| 38 | 38 | $xml->startDocument('1.0', 'UTF-8'); |
| 39 | - $this->format($shape, $shape['locationName'] ?: $shape['name'], $args, $xml); | |
| 39 | + $rootElementName = $this->determineRootElementName($shape); | |
| 40 | + $this->format($shape, $rootElementName, $args, $xml); | |
| 40 | 41 | $xml->endDocument(); |
| 41 | 42 | return $xml->outputMemory(); |
| 42 | 43 | } |
| 43 | 44 | private function startElement(Shape $shape, $name, XMLWriter $xml) |
| @@ -43,9 +44,9 @@ | ||
| 43 | 44 | private function startElement(Shape $shape, $name, XMLWriter $xml) |
| 44 | 45 | { |
| 45 | 46 | $xml->startElement($name); |
| 46 | 47 | if ($ns = $shape['xmlNamespace']) { |
| 47 | - $xml->writeAttribute(isset($ns['prefix']) ? "xmlns:{$ns['prefix']}" : 'xmlns', $shape['xmlNamespace']['uri']); | |
| 48 | + $xml->writeAttribute(isset($ns['prefix']) ? "xmlns:{$ns['prefix']}" : 'xmlns', $ns['uri']); | |
| 48 | 49 | } |
| 49 | 50 | } |
| 50 | 51 | private function format(Shape $shape, $name, $value, XMLWriter $xml) |
| 51 | 52 | { |
| @@ -67,9 +68,15 @@ | ||
| 67 | 68 | private function add_structure(StructureShape $shape, $name, array $value, \XMLWriter $xml) |
| 68 | 69 | { |
| 69 | 70 | $this->startElement($shape, $name, $xml); |
| 70 | 71 | foreach ($this->getStructureMembers($shape, $value) as $k => $definition) { |
| 71 | - $this->format($definition['member'], $definition['member']['locationName'] ?: $k, $definition['value'], $xml); | |
| 72 | + // Default to member name | |
| 73 | + $elementName = $k; | |
| 74 | + // Only use locationName for non-structure members | |
| 75 | + if (!$definition['member'] instanceof StructureShape && $definition['member']['locationName']) { | |
| 76 | + $elementName = $definition['member']['locationName']; | |
| 77 | + } | |
| 78 | + $this->format($definition['member'], $elementName, $definition['value'], $xml); | |
| 72 | 79 | } |
| 73 | 80 | $xml->endElement(); |
| 74 | 81 | } |
| 75 | 82 | private function getStructureMembers(StructureShape $shape, array $value) |
| @@ -105,12 +112,14 @@ | ||
| 105 | 112 | } |
| 106 | 113 | } |
| 107 | 114 | private function add_map(MapShape $shape, $name, array $value, XMLWriter $xml) |
| 108 | 115 | { |
| 109 | - $xmlEntry = $shape['flattened'] ? $shape['locationName'] : 'entry'; | |
| 116 | + $xmlEntry = $shape['flattened'] ? $name : 'entry'; | |
| 110 | 117 | $xmlKey = $shape->getKey()['locationName'] ?: 'key'; |
| 111 | 118 | $xmlValue = $shape->getValue()['locationName'] ?: 'value'; |
| 112 | - $this->startElement($shape, $name, $xml); | |
| 119 | + if (!$shape['flattened']) { | |
| 120 | + $this->startElement($shape, $name, $xml); | |
| 121 | + } | |
| 113 | 122 | foreach ($value as $key => $v) { |
| 114 | 123 | $this->startElement($shape, $xmlEntry, $xml); |
| 115 | 124 | $this->format($shape->getKey(), $xmlKey, $key, $xml); |
| 116 | 125 | $this->format($shape->getValue(), $xmlValue, $v, $xml); |
| @@ -115,9 +124,11 @@ | ||
| 115 | 124 | $this->format($shape->getKey(), $xmlKey, $key, $xml); |
| 116 | 125 | $this->format($shape->getValue(), $xmlValue, $v, $xml); |
| 117 | 126 | $xml->endElement(); |
| 118 | 127 | } |
| 119 | - $xml->endElement(); | |
| 128 | + if (!$shape['flattened']) { | |
| 129 | + $xml->endElement(); | |
| 130 | + } | |
| 120 | 131 | } |
| 121 | 132 | private function add_blob(Shape $shape, $name, $value, XMLWriter $xml) |
| 122 | 133 | { |
| 123 | 134 | $this->startElement($shape, $name, $xml); |
| @@ -143,6 +154,21 @@ | ||
| 143 | 154 | $xml->writeAttribute($shape['locationName'] ?: $name, $value); |
| 144 | 155 | } else { |
| 145 | 156 | $this->defaultShape($shape, $name, $value, $xml); |
| 146 | 157 | } |
| 158 | + } | |
| 159 | + private function determineRootElementName(Shape $shape) : string | |
| 160 | + { | |
| 161 | + $shapeName = $shape->getName(); | |
| 162 | + // Look up the shape definition first | |
| 163 | + if ($shapeName && ($shapeMap = $shape->getShapeMap())) { | |
| 164 | + if (isset($shapeMap[$shapeName]['locationName'])) { | |
| 165 | + return $shapeMap[$shapeName]['locationName']; | |
| 166 | + } | |
| 167 | + } | |
| 168 | + // Fall back to shape's current locationName | |
| 169 | + if ($shape['locationName']) { | |
| 170 | + return $shape['locationName']; | |
| 171 | + } | |
| 172 | + return $shapeName; | |
| 147 | 173 | } |
| 148 | 174 | } |