Skip to content

Commit

Permalink
Fix failing integ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidh44 committed Oct 5, 2023
1 parent 97808a6 commit 6e7c20f
Showing 1 changed file with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static java.util.Collections.singletonList;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.Collections;
Expand Down Expand Up @@ -117,24 +116,7 @@ SdkPojo unmarshall(XmlUnmarshallerContext context, SdkPojo sdkPojo, XmlElement r
if (isExplicitPayloadMember(field) && field.marshallingType() == MarshallingType.SDK_BYTES) {
field.set(sdkPojo, SdkBytes.fromInputStream(context.response().content().get()));
} else if (isExplicitPayloadMember(field) && field.marshallingType() == MarshallingType.STRING) {
InputStream is = context.response().content().get();
try {
is.reset();
} catch (IOException e) {
// do nothing
}

SdkBytes sdkBytes = SdkBytes.fromInputStream(is);
String stringPayload = sdkBytes.asUtf8String();

if (isXmlString(stringPayload)) {
InputStream inputStream = new ByteArrayInputStream(sdkBytes.asByteArray());
XmlElement document = XmlDomParser.parse(inputStream);
Object unmarshalled = unmarshaller.unmarshall(context, singletonList(document), (SdkField<Object>) field);
field.set(sdkPojo, unmarshalled);
} else {
field.set(sdkPojo, stringPayload);
}
setExplicitStringPayload(unmarshaller, context, sdkPojo, element, field);
} else {
Object unmarshalled = unmarshaller.unmarshall(context, element, (SdkField<Object>) field);
field.set(sdkPojo, unmarshalled);
Expand All @@ -149,6 +131,31 @@ SdkPojo unmarshall(XmlUnmarshallerContext context, SdkPojo sdkPojo, XmlElement r
return (SdkPojo) ((Buildable) sdkPojo).build();
}

private void setExplicitStringPayload(XmlUnmarshaller<Object> unmarshaller, XmlUnmarshallerContext context,
SdkPojo sdkPojo, List<XmlElement> element, SdkField<?> field) {
SdkBytes sdkBytes = SdkBytes.fromInputStream(context.response().content().get());
String stringPayload = sdkBytes.asUtf8String();
if (isXmlString(stringPayload)) {
InputStream inputStream = new ByteArrayInputStream(sdkBytes.asByteArray());
XmlElement document = XmlDomParser.parse(inputStream);
Object unmarshalled = unmarshaller.unmarshall(context, singletonList(document), (SdkField<Object>) field);
field.set(sdkPojo, unmarshalled);
} else {
if (stringPayload.isEmpty()) {
try {
// InputStream may have already been read
Object unmarshalled = unmarshaller.unmarshall(context, element, (SdkField<Object>) field);
field.set(sdkPojo, unmarshalled);
} catch (NullPointerException e) {
// User passed in empty String
field.set(sdkPojo, stringPayload);
}
} else {
field.set(sdkPojo, stringPayload);
}
}
}

private boolean isXmlString(String payload) {
String s3XmlEnvelopePrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Policy><![CDATA[";
return payload.startsWith(s3XmlEnvelopePrefix) || XML_PATTERN.matcher(payload).matches();
Expand Down

0 comments on commit 6e7c20f

Please sign in to comment.