Skip to content

Commit

Permalink
Update serializing empty lists for query protocols
Browse files Browse the repository at this point in the history
Update serializing empty lists for query protocols (see references)
to pass protocol tests.

Previous Behavior:

`{ "ListArg": [] }` serializes to ``.

Current Behavior:

`{ "ListArg": [] }` serializes to `?ListArg=`.

References:

- smithy-lang/smithy#1444
- [AWS query
  protocol](https://awslabs.github.io/smithy/2.0/aws/protocols/aws-query-protocol.html)
- [AWS EC2 query
  protocol](https://awslabs.github.io/smithy/2.0/aws/protocols/aws-ec2-query-protocol.html)
  • Loading branch information
Steven Yuan committed Nov 11, 2022
1 parent 04dbe8b commit 96308bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
17 changes: 17 additions & 0 deletions aws/protocol/query/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@ func (a *Array) Value() Value {
// Lists can't have flat members
return newValue(a.values, fmt.Sprintf("%s.%d", prefix, a.size), false)
}

// Empty allows empty Query Array URI encoding, e.g.
//
// "ListArg": [] -> ?ListArg=
//
// If this method is not called on an empty Query Array, it will
// not be serialized when encoded.
//
// The method should ONLY be used if the Array is known to be empty.
func (a *Array) Empty() {
// If the Array is not empty, return to avoid wrong encoding
if a.size != 0 {
return
}
// Set value of prefix to empty string (no text)
newValue(a.values, a.prefix, false).String("")
}
16 changes: 16 additions & 0 deletions aws/protocol/query/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func TestEncode(t *testing.T) {
},
Expect: []byte(`list.spam.1=spam&list.spam.2=eggs`),
},
"empty list": {
Encode: func(e *Encoder) error {
list := e.Object().Key("ListArg").Array("member")
list.Empty()
return e.Encode()
},
Expect: []byte(`ListArg=`),
},
"flat list": {
Encode: func(e *Encoder) error {
list := e.Object().FlatKey("list").Array("spam")
Expand All @@ -44,6 +52,14 @@ func TestEncode(t *testing.T) {
},
Expect: []byte(`list.1=spam&list.2=eggs`),
},
"empty flat list": {
Encode: func(e *Encoder) error {
list := e.Object().FlatKey("ListArg").Array("member")
list.Empty()
return e.Encode()
},
Expect: []byte(`ListArg=`),
},
"map": {
Encode: func(e *Encoder) error {
mapValue := e.Object().Key("map").Map("key", "value")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.smithy.model.shapes.MapShape;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.EnumTrait;
Expand Down Expand Up @@ -75,10 +76,16 @@ protected void serializeCollection(GenerationContext context, CollectionShape sh
MemberShape member = shape.getMember();
Shape target = context.getModel().expectShape(member.getTarget());

// If the list is empty, exit early to avoid extra effort.
writer.write("if len(v) == 0 { return nil }");
String arrayName = getSerializedLocationName(member, "member");

// If the list is empty, serialize an empty query value.
writer.write("""
if len(v) == 0 {
value.Array($S).Empty()
return nil
}""", arrayName);

writer.write("array := value.Array($S)", getSerializedLocationName(member, "member"));
writer.write("array := value.Array($S)", arrayName);
writer.write("");

writer.openBlock("for i := range v {", "}", () -> {
Expand Down

0 comments on commit 96308bf

Please sign in to comment.