Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen: Handle xml-namespace trait on service shape #858

Merged
merged 3 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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