Skip to content

Commit

Permalink
Update blob defaults for protocol tests (#2467)
Browse files Browse the repository at this point in the history
Updates blob defaults in protocol tests to use valid Base64 strings. Also updates the associated default documentation to make this requirement clear.
  • Loading branch information
hpmellema authored Nov 15, 2024
1 parent 06750df commit f71baff
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/source-2.0/spec/type-refinement-traits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ numeric types MUST be numbers that fit within the targeted type and match any

The following shapes have restrictions on their default values:

* blob: can be set to any valid base64-encoded string.
* enum: can be set to any valid string *value* of the enum.
* intEnum: can be set to any valid integer *value* of the enum.
* document: can be set to ``null``, ```true``, ``false``, string, numbers,
Expand Down
4 changes: 2 additions & 2 deletions smithy-aws-protocol-tests/model/awsJson1_0/required.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ apply OperationWithRequiredMembersWithDefaults @httpResponseTests([
requiredBoolean: true
requiredList: []
requiredTimestamp: 1
requiredBlob: "{}"
requiredBlob: "blob"
requiredByte: 1
requiredShort: 1
requiredInteger: 10
Expand Down Expand Up @@ -127,7 +127,7 @@ structure RequiredMembersWithDefaultsMixin {
requiredTimestamp: Timestamp = 1

@required
requiredBlob: Blob = "{}"
requiredBlob: Blob = "YmxvYg=="

@required
requiredByte: Byte = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

package software.amazon.smithy.model.validation.validators;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.NeighborProviderIndex;
Expand Down Expand Up @@ -109,6 +111,15 @@ private NodeValidationVisitor validateShapeValue(
events.addAll(shape.accept(visitor));

switch (shapeTarget.getType()) {
case BLOB:
try {
value.asStringNode().ifPresent(val -> {
Base64.getDecoder().decode(val.getValue().getBytes(StandardCharsets.UTF_8));
});
} catch (IllegalArgumentException exc) {
events.add(warning(shape, trait, "The @default value of a blob should be a valid base64 string."));
}
break;
case MAP:
value.asObjectNode().ifPresent(obj -> {
if (!obj.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[WARNING] smithy.example#Foo$invalidBlob: The @default value of a blob should be a valid base64 string. | DefaultTrait
[WARNING] smithy.example#InvalidBlob: The @default value of a blob should be a valid base64 string. | DefaultTrait
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$version: "2.0"

namespace smithy.example

structure Foo {
invalidBlob: Blob = "{}"
}

@default("{}") // invalid default value
blob InvalidBlob
2 changes: 1 addition & 1 deletion smithy-protocol-tests/model/rpcv2Cbor/defaults.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ structure DefaultsMixin {
defaultBoolean: Boolean = true
defaultList: TestStringList = []
defaultTimestamp: Timestamp = 0
defaultBlob: Blob = "abc"
defaultBlob: Blob = "YWJj"
defaultByte: Byte = 1
defaultShort: Short = 1
defaultInteger: Integer = 10
Expand Down

0 comments on commit f71baff

Please sign in to comment.