Skip to content

Commit

Permalink
Merge pull request #858 from aws/xmlnamespace-on-service
Browse files Browse the repository at this point in the history
codegen: Handle xml-namespace trait on service shape
  • Loading branch information
skotambkar authored Oct 29, 2020
2 parents 6ecaaba + 72580de commit 0061141
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import software.amazon.smithy.model.knowledge.HttpBindingIndex;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.MediaTypeTrait;
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.OptionalAuthTrait;
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.model.traits.XmlAttributeTrait;
import software.amazon.smithy.model.traits.XmlNamespaceTrait;

abstract class RestXmlProtocolGenerator extends HttpBindingProtocolGenerator {
/**
Expand Down Expand Up @@ -94,6 +98,16 @@ protected void writeMiddlewareDocumentSerializerDelegator(
writer.write("xmlEncoder := smithyxml.NewEncoder(bytes.NewBuffer(nil))");

generateXMLStartElement(context, inputShape, "root", "input");

// check if service shape is bound by xmlNameSpace Trait
Optional<XmlNamespaceTrait> xmlNamespaceTrait = context.getService().getTrait(XmlNamespaceTrait.class);
if (xmlNamespaceTrait.isPresent()) {
XmlNamespaceTrait namespace = xmlNamespaceTrait.get();
writer.write("root.Attr = append(root.Attr, smithyxml.NewNamespaceAttribute($S, $S))",
namespace.getPrefix().isPresent() ? namespace.getPrefix().get() : "", namespace.getUri()
);
}

writer.openBlock("if err := $L(input, xmlEncoder.RootElement(root)); err != nil {", "}",
functionName, () -> {
writer.write("return out, metadata, &smithy.SerializationError{Err: err}");
Expand Down Expand Up @@ -123,6 +137,16 @@ protected void writeMiddlewarePayloadAsDocumentSerializerDelegator(
writer.write("xmlEncoder := smithyxml.NewEncoder(bytes.NewBuffer(nil))");

generateXMLStartElement(context, payloadShape, "payloadRoot", operand);

// check if service shape is bound by xmlNameSpace Trait
Optional<XmlNamespaceTrait> xmlNamespaceTrait = context.getService().getTrait(XmlNamespaceTrait.class);
if (xmlNamespaceTrait.isPresent()) {
XmlNamespaceTrait namespace = xmlNamespaceTrait.get();
writer.write("payloadRoot.Attr = append(payloadRoot.Attr, smithyxml.NewNamespaceAttribute($S, $S))",
namespace.getPrefix().isPresent() ? namespace.getPrefix().get() : "", namespace.getUri()
);
}

writer.openBlock("if err := $L($L, xmlEncoder.RootElement(payloadRoot)); err != nil {", "}", functionName,
operand, () -> {
writer.write("return out, metadata, &smithy.SerializationError{Err: err}");
Expand Down
29 changes: 29 additions & 0 deletions service/cloudfront/serializers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0061141

Please sign in to comment.