Skip to content

Add expectations for default member deser #365

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

Merged
merged 1 commit into from
Jul 1, 2021
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 @@ -124,46 +124,48 @@ public String blobShape(BlobShape shape) {

@Override
public String booleanShape(BooleanShape shape) {
return deserializeUnmodified();
context.getWriter().addImport("expectBoolean", "__expectBoolean", "@aws-sdk/smithy-client");
return "__expectBoolean(" + dataSource + ")";
}

@Override
public String byteShape(ByteShape shape) {
return deserializeUnmodified();
return expectNumber();
}

@Override
public String shortShape(ShortShape shape) {
return deserializeUnmodified();
return expectNumber();
}

@Override
public String integerShape(IntegerShape shape) {
return deserializeUnmodified();
return expectNumber();
}

@Override
public String longShape(LongShape shape) {
return deserializeUnmodified();
return expectNumber();
}

@Override
public String floatShape(FloatShape shape) {
return deserializeUnmodified();
return expectNumber();
}

@Override
public String doubleShape(DoubleShape shape) {
return deserializeUnmodified();
return expectNumber();
}

@Override
public String stringShape(StringShape shape) {
return HttpProtocolGeneratorUtils.getStringOutputParam(context, shape, deserializeUnmodified());
private String expectNumber() {
context.getWriter().addImport("expectNumber", "__expectNumber", "@aws-sdk/smithy-client");
return "__expectNumber(" + dataSource + ")";
}

private String deserializeUnmodified() {
return dataSource;
@Override
public String stringShape(StringShape shape) {
return HttpProtocolGeneratorUtils.getStringOutputParam(context, shape, dataSource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,12 @@ private String getStringOutputParam(
dataSource = "Buffer.from(" + dataSource + ", 'base64').toString('ascii')";
}

return HttpProtocolGeneratorUtils.getStringOutputParam(context, target, dataSource);
return HttpProtocolGeneratorUtils.getStringOutputParam(
context, target, dataSource, !isGuaranteedString(bindingType));
}

private boolean isGuaranteedString(Location bindingType) {
return bindingType != Location.PAYLOAD && bindingType != Location.DOCUMENT;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ static String getStringInputParam(GenerationContext context, Shape shape, String
* @param shape The shape that represents the value being received.
* @param dataSource The in-code location of the data to provide an output of
* ({@code output.foo}, {@code entry}, etc.)
* @param useExpect Whether or not to wrap the string in expectString. This should
* only be false if the value is guaranteed to be a string already.
* @return Returns a value or expression of the output string.
*/
static String getStringOutputParam(GenerationContext context, Shape shape, String dataSource) {
static String getStringOutputParam(GenerationContext context, Shape shape, String dataSource, boolean useExpect) {
// Handle media type generation, defaulting to a standard String.
Optional<MediaTypeTrait> mediaTypeTrait = shape.getTrait(MediaTypeTrait.class);
if (mediaTypeTrait.isPresent()) {
Expand All @@ -173,7 +175,28 @@ static String getStringOutputParam(GenerationContext context, Shape shape, Strin
LOGGER.warning(() -> "Found unsupported mediatype " + mediaType + " on String shape: " + shape);
}
}
return dataSource;

if (!useExpect) {
return dataSource;
}
context.getWriter().addImport("expectString", "__expectString", "@aws-sdk/smithy-client");
return "__expectString(" + dataSource + ")";
}

/**
* Given a String output, determine its media type and generate an output value
* provider for it.
*
* <p>This currently only supports using the LazyJsonString for {@code "application/json"}.
*
* @param context The generation context.
* @param shape The shape that represents the value being received.
* @param dataSource The in-code location of the data to provide an output of
* ({@code output.foo}, {@code entry}, etc.)
* @return Returns a value or expression of the output string.
*/
static String getStringOutputParam(GenerationContext context, Shape shape, String dataSource) {
return getStringOutputParam(context, shape, dataSource, true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public static Collection<Object[]> validMemberTargetTypes() {
+ "(" + DATA_SOURCE + ", context)";

return ListUtils.of(new Object[][]{
{BooleanShape.builder().id(id).build(), DATA_SOURCE},
{ByteShape.builder().id(id).build(), DATA_SOURCE},
{DoubleShape.builder().id(id).build(), DATA_SOURCE},
{FloatShape.builder().id(id).build(), DATA_SOURCE},
{IntegerShape.builder().id(id).build(), DATA_SOURCE},
{LongShape.builder().id(id).build(), DATA_SOURCE},
{ShortShape.builder().id(id).build(), DATA_SOURCE},
{StringShape.builder().id(id).build(), DATA_SOURCE},
{BooleanShape.builder().id(id).build(), "__expectBoolean(" + DATA_SOURCE + ")"},
{ByteShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
{DoubleShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
{FloatShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
{IntegerShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
{LongShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
{ShortShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
{StringShape.builder().id(id).build(), "__expectString(" + DATA_SOURCE + ")"},
{
StringShape.builder().id(id).addTrait(new MediaTypeTrait("foo+json")).build(),
"new __LazyJsonString(" + DATA_SOURCE + ")"
Expand Down