Skip to content

Commit

Permalink
Fix xml attribute serialization
Browse files Browse the repository at this point in the history
This fixes an issue where xml attributes were not being serialized
except at the root level.
  • Loading branch information
JordonPhillips committed Apr 7, 2021
1 parent f91a70b commit 453feac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,6 @@ static void generateHttpProtocolTests(GenerationContext context) {
.service(ShapeId.from("aws.protocoltests.json10#JsonRpc10"))
.operation(ShapeId.from("aws.protocoltests.json10#EmptyInputAndEmptyOutput"))
.addTestName("AwsJson10EmptyInputAndEmptyOutput")
.build(),

// Rest XML namespaced attributes. This needs to be fixed, but can be punted
// temporarily since this is only used in an output in a single service.
// TODO: fix serializing namespaced xml attributes
HttpProtocolUnitTestGenerator.SkipTest.builder()
.service(ShapeId.from("aws.protocoltests.restxml.xmlns#RestXmlWithNamespace"))
.operation(ShapeId.from("aws.protocoltests.restxml.xmlns#SimpleScalarProperties"))
.build()
));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package software.amazon.smithy.aws.go.codegen;

import java.util.Collection;
import java.util.Optional;
import software.amazon.smithy.aws.go.codegen.customization.AwsCustomGoDependency;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.aws.traits.protocols.RestXmlTrait;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
Expand All @@ -12,17 +14,15 @@
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.SyntheticClone;
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.go.codegen.integration.ProtocolUtils;
import software.amazon.smithy.go.codegen.knowledge.GoPointableIndex;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.model.traits.XmlAttributeTrait;
import software.amazon.smithy.model.traits.XmlNameTrait;
import software.amazon.smithy.model.traits.XmlNamespaceTrait;
import software.amazon.smithy.aws.traits.protocols.RestXmlTrait;

public final class XmlProtocolUtils {
private XmlProtocolUtils() {
Expand Down Expand Up @@ -74,7 +74,7 @@ public static void generatePayloadAsDocumentXMLStartElement(
String name = memberShape.getMemberName();
if (targetShape.isStructureShape()) {
if (memberShape.hasTrait(XmlNameTrait.class)) {
name = getSerializedXMLMemberName(memberShape);
name = getSerializedXMLMemberName(memberShape);
} else {
name = getSerializedXMLShapeName(context, targetShape);
}
Expand Down Expand Up @@ -112,7 +112,25 @@ private static void generateXmlNamespaceAndAttributes(
}

// Traverse member shapes to get attributes
shape.members().stream().forEach(memberShape -> {
if (shape.isMemberShape()) {
MemberShape memberShape = shape.asMemberShape().get();
Shape target = context.getModel().expectShape(memberShape.getTarget());
String memberName = context.getSymbolProvider().toMemberName(memberShape);
String operand = inputSrc + "." + memberName;
generateXmlAttributes(context, target.members(), operand, dst);
} else {
generateXmlAttributes(context, shape.members(), inputSrc, dst);
}
}

private static void generateXmlAttributes(
ProtocolGenerator.GenerationContext context,
Collection<MemberShape> members,
String inputSrc,
String dst
) {
GoWriter writer = context.getWriter();
members.forEach(memberShape -> {
if (memberShape.hasTrait(XmlAttributeTrait.class)) {
GoValueAccessUtils.writeIfNonZeroValueMember(context.getModel(), context.getSymbolProvider(),
writer, memberShape, inputSrc, true, memberShape.isRequired(), (operand) -> {
Expand All @@ -126,8 +144,6 @@ private static void generateXmlNamespaceAndAttributes(
});
}



// generates code to format xml attributes. If a shape type is timestamp, number, or boolean
// it will be formatted into a string.
private static void formatXmlAttributeValueAsString(
Expand All @@ -146,7 +162,10 @@ private static void formatXmlAttributeValueAsString(
derefSource = "*" + src;
}

if (target.isStringShape()) {
if (target.hasTrait(EnumTrait.class)) {
writer.write("$L = string($L)", dest, derefSource);
return;
} else if (target.isStringShape()) {
// create dereferenced copy of pointed to value.
writer.write("$L = $L", dest, derefSource);
return;
Expand Down Expand Up @@ -403,5 +422,3 @@ private static boolean isS3Service(ServiceShape service) {
return serviceId.equalsIgnoreCase("S3");
}
}


0 comments on commit 453feac

Please sign in to comment.