Skip to content

Commit 4570767

Browse files
committed
fix: remove unused Platform serialization code path
1 parent e38423d commit 4570767

3 files changed

Lines changed: 42 additions & 57 deletions

File tree

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/EventHandlerLoader.java

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package com.amazonaws.services.lambda.runtime.api.client;
77

8-
import com.amazonaws.services.lambda.runtime.ClientContext;
98
import com.amazonaws.services.lambda.runtime.Context;
109
import com.amazonaws.services.lambda.runtime.LambdaLogger;
1110
import com.amazonaws.services.lambda.runtime.LambdaRuntimeInternal;
@@ -38,11 +37,9 @@
3837
import java.lang.reflect.TypeVariable;
3938
import java.util.Arrays;
4039
import java.util.Comparator;
41-
import java.util.EnumMap;
4240
import java.util.HashMap;
4341
import java.util.LinkedList;
4442
import java.util.Map;
45-
import java.util.Objects;
4643
import java.util.Optional;
4744
import static com.amazonaws.services.lambda.runtime.api.client.UserFault.filterStackTrace;
4845
import static com.amazonaws.services.lambda.runtime.api.client.UserFault.makeUserFault;
@@ -51,16 +48,10 @@
5148
public final class EventHandlerLoader {
5249
private static final byte[] _JsonNull = new byte[]{'n', 'u', 'l', 'l'};
5350

54-
private enum Platform {
55-
ANDROID,
56-
IOS,
57-
UNKNOWN
58-
}
59-
6051
private static volatile ThreadLocal<PojoSerializer<LambdaClientContext>> contextSerializer = new ThreadLocal<>();
6152
private static volatile ThreadLocal<PojoSerializer<LambdaCognitoIdentity>> cognitoSerializer = new ThreadLocal<>();
6253

63-
private static final ThreadLocal<EnumMap<Platform, Map<Type, PojoSerializer<Object>>>> typeCache = ThreadLocal.withInitial(() -> new EnumMap<>(Platform.class));
54+
private static final ThreadLocal<Map<Type, PojoSerializer<Object>>> typeCache = ThreadLocal.withInitial(HashMap::new);
6455

6556
private static final Comparator<Method> methodPriority = new Comparator<Method>() {
6657
public int compare(Method lhs, Method rhs) {
@@ -97,16 +88,14 @@ private EventHandlerLoader() {
9788
}
9889

9990
/**
100-
* returns the appropriate serializer for the class based on platform and whether the class is a supported event
91+
* returns the appropriate serializer for the class based on whether the class is a supported event
10192
*
102-
* @param platform enum platform
103-
* @param type Type of object used
93+
* @param type Type of object used
10494
* @return PojoSerializer
105-
* @see Platform for which platforms are used
10695
* @see LambdaEventSerializers for how mixins and modules are added to the serializer
10796
*/
10897
@SuppressWarnings({"unchecked", "rawtypes"})
109-
private static PojoSerializer<Object> getSerializer(Platform platform, Type type) {
98+
private static PojoSerializer<Object> getSerializer(Type type) {
11099
PojoSerializer<Object> customSerializer = PojoSerializerLoader.getCustomerSerializer(type);
111100
if (customSerializer != null) {
112101
return customSerializer;
@@ -119,24 +108,15 @@ private static PojoSerializer<Object> getSerializer(Platform platform, Type type
119108
return LambdaEventSerializers.serializerFor(clazz, AWSLambda.getCustomerClassLoader());
120109
}
121110
}
122-
// else platform dependent (Android uses GSON but all other platforms use Jackson)
123-
if (Objects.requireNonNull(platform) == Platform.ANDROID) {
124-
return GsonFactory.getInstance().getSerializer(type);
125-
}
126111
return JacksonFactory.getInstance().getSerializer(type);
127112
}
128113

129-
private static PojoSerializer<Object> getSerializerCached(Platform platform, Type type) {
130-
EnumMap<Platform, Map<Type, PojoSerializer<Object>>> threadTypeCache = typeCache.get();
131-
Map<Type, PojoSerializer<Object>> cache = threadTypeCache.get(platform);
132-
if (cache == null) {
133-
cache = new HashMap<>();
134-
threadTypeCache.put(platform, cache);
135-
}
114+
private static PojoSerializer<Object> getSerializerCached(Type type) {
115+
Map<Type, PojoSerializer<Object>> cache = typeCache.get();
136116

137117
PojoSerializer<Object> serializer = cache.get(type);
138118
if (serializer == null) {
139-
serializer = getSerializer(platform, type);
119+
serializer = getSerializer(type);
140120
cache.put(type, serializer);
141121
}
142122

@@ -158,31 +138,6 @@ private static PojoSerializer<LambdaCognitoIdentity> getCognitoSerializer() {
158138
}
159139

160140

161-
private static Platform getPlatform(Context context) {
162-
ClientContext cc = context.getClientContext();
163-
if (cc == null) {
164-
return Platform.UNKNOWN;
165-
}
166-
167-
Map<String, String> env = cc.getEnvironment();
168-
if (env == null) {
169-
return Platform.UNKNOWN;
170-
}
171-
172-
String platform = env.get("platform");
173-
if (platform == null) {
174-
return Platform.UNKNOWN;
175-
}
176-
177-
if ("Android".equalsIgnoreCase(platform)) {
178-
return Platform.ANDROID;
179-
} else if ("iPhoneOS".equalsIgnoreCase(platform)) {
180-
return Platform.IOS;
181-
} else {
182-
return Platform.UNKNOWN;
183-
}
184-
}
185-
186141
private static boolean isVoid(Type type) {
187142
return Void.TYPE.equals(type) || (type instanceof Class) && Void.class.isAssignableFrom((Class<?>) type);
188143
}
@@ -629,11 +584,11 @@ public PojoHandlerAsStreamHandler(
629584

630585

631586
if (inputType.isPresent()) {
632-
getSerializerCached(Platform.UNKNOWN, inputType.get());
587+
getSerializerCached(inputType.get());
633588
}
634589

635590
if (outputType.isPresent()) {
636-
getSerializerCached(Platform.UNKNOWN, outputType.get());
591+
getSerializerCached(outputType.get());
637592
}
638593
}
639594

@@ -642,10 +597,9 @@ public PojoHandlerAsStreamHandler(
642597
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
643598
throws IOException {
644599
final Object input;
645-
final Platform platform = getPlatform(context);
646600
try {
647601
if (inputType.isPresent()) {
648-
input = getSerializerCached(platform, inputType.get()).fromJson(inputStream);
602+
input = getSerializerCached(inputType.get()).fromJson(inputStream);
649603
} else {
650604
input = null;
651605
}
@@ -662,7 +616,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
662616

663617
try {
664618
if (outputType.isPresent()) {
665-
PojoSerializer<Object> serializer = getSerializerCached(platform, outputType.get());
619+
PojoSerializer<Object> serializer = getSerializerCached(outputType.get());
666620
serializer.toJson(output, outputStream);
667621
} else {
668622
outputStream.write(_JsonNull);

aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/EventHandlerLoaderTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.concurrent.TimeUnit;
1414

1515
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertFalse;
1617
import static org.junit.jupiter.api.Assertions.assertTrue;
1718

1819
class EventHandlerLoaderTest {
@@ -59,6 +60,22 @@ void PojoHandlerTest_twoParams() throws Exception {
5960
assertSuccessfulInvocation(lambdaRequestHandler);
6061
}
6162

63+
@Test
64+
void PojoHandler_outputSerializer_ignoresClientContextPlatform() throws Exception {
65+
LambdaRequestHandler handler =
66+
getLambdaRequestHandler("test.lambda.handlers.POJOHanlderImpl::pojoOutputHandler");
67+
68+
InvocationRequest request = getTestInvocationRequest();
69+
request.setClientContext("{\"env\":{\"platform\":\"Android\"}}");
70+
71+
String result = handler.call(request).toString();
72+
73+
assertTrue(result.contains("beanProperty"),
74+
"expected property-based (Jackson) serialization, got: " + result);
75+
assertFalse(result.contains("internalField"),
76+
"caller ClientContext must not switch serialization to field-based (Gson), got: " + result);
77+
}
78+
6279
private LambdaRequestHandler getLambdaRequestHandler(String handler) throws ClassNotFoundException {
6380
ClassLoader cl = this.getClass().getClassLoader();
6481
HandlerInfo handlerInfo = HandlerInfo.fromString(handler, cl);

aws-lambda-java-runtime-interface-client/src/test/java/test/lambda/handlers/POJOHanlderImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@ public String oneParamHandler_context(Context context) {
2323
public String twoParamsHandler(String event, Context context) {
2424
return "success";
2525
}
26+
27+
@SuppressWarnings("unused")
28+
public PojoOutput pojoOutputHandler(String event) {
29+
return new PojoOutput();
30+
}
31+
32+
@SuppressWarnings("unused")
33+
public static class PojoOutput {
34+
private final String internalField = "field-based-value";
35+
36+
public String getBeanProperty() {
37+
return "property-based-value";
38+
}
39+
}
2640
}

0 commit comments

Comments
 (0)