Skip to content

Set explicit payload members to null if the input is empty #6111

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 4 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-e2de7f3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "bugfix",
"description": "Fix a regression for the JSON REST protocol for which an structure explicit payload member was set to the empty object instead of null"
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.core.SdkBytes;
Expand Down Expand Up @@ -266,6 +267,11 @@ private <T extends SdkPojo> T unmarshallFromJson(SdkPojo sdkPojo, InputStream in
return (T) unmarshallingParser.parse(sdkPojo, inputStream);
}

@SuppressWarnings("unchecked")
private <T extends SdkPojo> T unmarshallMemberFromJson(Supplier<SdkPojo> constructor, InputStream inputStream) {
return (T) unmarshallingParser.parseMember(constructor, inputStream);
}

private <TypeT extends SdkPojo> TypeT unmarshallResponse(SdkPojo sdkPojo,
SdkHttpFullResponse response) throws IOException {
JsonUnmarshallerContext context = JsonUnmarshallerContext.builder()
Expand All @@ -290,7 +296,7 @@ private <TypeT extends SdkPojo> TypeT unmarshallResponse(SdkPojo sdkPojo,
} else if (isExplicitPayloadMember(field) && field.marshallingType() == MarshallingType.SDK_POJO) {
Optional<AbortableInputStream> responseContent = context.response().content();
if (responseContent.isPresent()) {
field.set(sdkPojo, unmarshallFromJson(field.constructor().get(), responseContent.get()));
field.set(sdkPojo, unmarshallMemberFromJson(field.constructor(), responseContent.get()));
} else {
field.set(sdkPojo, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.core.SdkBytes;
Expand Down Expand Up @@ -72,6 +73,33 @@ public static Builder builder() {
return new Builder();
}

/**
* Parse the provided {@link InputStream} and return the deserialized {@link SdkPojo}. Unlike
* {@link #parse(SdkPojo, InputStream)} this method returns null if the input stream is empty. This is used to unmarshall
* payload members that can be null unlike top-level response pojos.
*/
public SdkPojo parseMember(Supplier<SdkPojo> constructor, InputStream content) {
return invokeSafely(() -> {
try (JsonParser parser = jsonFactory.createParser(content)
.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false)) {

JsonUnmarshallerContext c = JsonUnmarshallerContext.builder().build();
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_OBJECT) {
throw new JsonParseException("expecting start object, got instead: " + token);
}
SdkPojo pojo = constructor.get();
return parseSdkPojo(c, pojo, parser);
}
});
}

/**
* Parse the provided {@link InputStream} and return the deserialized {@link SdkPojo}.
*/
Expand All @@ -95,7 +123,6 @@ public SdkPojo parse(SdkPojo pojo, InputStream content) {
}
});
}

/**
* Parses an sdk pojo and fills its fields. The given SdkPojo instance is expected to be a {@link Buildable} instance. This
* method expects that the START_OBJECT token has been already consumed, so the next token should be either a field name or an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
}
}
},
{
"description": "Operation with explicit payload structure, with emtpy output is unmarshalled as null value",
"given": {
"response": {
"status_code": 200,
"body": ""
}
},
"when": {
"action": "unmarshall",
"operation": "OperationWithExplicitPayloadStructure"
},
"then": {
"deserializedAs": {
"PayloadMember": null
}
}
},
{
"description": "Operation with streaming payload in output is unmarshalled correctly",
"given": {
Expand Down
Loading