Skip to content

Commit ef04849

Browse files
committed
fix: Bug when trying to deserialize a type into itself for Lambda default behavior. We can just return the type itself. Relevant for simple String and InputStream handlers.
1 parent 3e6a8b7 commit ef04849

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

powertools-kafka/src/main/java/software/amazon/lambda/powertools/kafka/serializers/LambdaDefaultDeserializer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,22 @@ public class LambdaDefaultDeserializer implements PowertoolsDeserializer {
2828
@SuppressWarnings("unchecked")
2929
@Override
3030
public <T> T fromJson(InputStream input, Type type) {
31-
return JacksonFactory.getInstance().getSerializer((Class<T>) type).fromJson(input);
31+
// If the target type does not require conversion, simply return the value itself
32+
if (type.equals(InputStream.class)) {
33+
return (T) input;
34+
}
35+
36+
return (T) JacksonFactory.getInstance().getSerializer(type).fromJson(input);
3237
}
3338

3439
@SuppressWarnings("unchecked")
3540
@Override
3641
public <T> T fromJson(String input, Type type) {
37-
return JacksonFactory.getInstance().getSerializer((Class<T>) type).fromJson(input);
42+
// If the target type does not require conversion, simply return the value itself
43+
if (type.equals(String.class)) {
44+
return (T) input;
45+
}
46+
47+
return (T) JacksonFactory.getInstance().getSerializer(type).fromJson(input);
3848
}
3949
}

powertools-kafka/src/test/java/software/amazon/lambda/powertools/kafka/PowertoolsSerializerTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.ByteArrayOutputStream;
2121
import java.io.IOException;
22+
import java.io.InputStream;
2223
import java.lang.reflect.Type;
2324
import java.util.Arrays;
2425
import java.util.Base64;
@@ -108,6 +109,39 @@ void shouldUseLambdaDefaultDeserializer(InputType inputType) throws JsonProcessi
108109
assertThat(result.getTags()).containsExactly("tag1", "tag2");
109110
}
110111

112+
@Test
113+
@SetEnvironmentVariable(key = "_HANDLER", value = "software.amazon.lambda.powertools.kafka.testutils.StringHandler::handleRequest")
114+
void shouldHandleStringInputType() {
115+
// When
116+
PowertoolsSerializer serializer = new PowertoolsSerializer();
117+
118+
// Then
119+
String testInput = "This is a test string";
120+
121+
// This should directly return the input string
122+
String result = serializer.fromJson(testInput, String.class);
123+
124+
assertThat(result).isEqualTo(testInput);
125+
}
126+
127+
@Test
128+
@SetEnvironmentVariable(key = "_HANDLER", value = "software.amazon.lambda.powertools.kafka.testutils.InputStreamHandler::handleRequest")
129+
void shouldHandleInputStreamType() throws IOException {
130+
// When
131+
PowertoolsSerializer serializer = new PowertoolsSerializer();
132+
133+
// Then
134+
String testInput = "This is a test string";
135+
ByteArrayInputStream inputStream = new ByteArrayInputStream(testInput.getBytes());
136+
137+
// This should return the input stream directly
138+
InputStream result = serializer.fromJson(inputStream, InputStream.class);
139+
140+
// Read the content to verify it's the same
141+
String resultString = new String(result.readAllBytes());
142+
assertThat(resultString).isEqualTo(testInput);
143+
}
144+
111145
@ParameterizedTest
112146
@MethodSource("inputTypes")
113147
@SetEnvironmentVariable(key = "_HANDLER", value = "software.amazon.lambda.powertools.kafka.testutils.JsonHandler::handleRequest")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package software.amazon.lambda.powertools.kafka.testutils;
14+
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
18+
import com.amazonaws.services.lambda.runtime.Context;
19+
import com.amazonaws.services.lambda.runtime.RequestHandler;
20+
21+
public class InputStreamHandler implements RequestHandler<InputStream, String> {
22+
@Override
23+
public String handleRequest(InputStream input, Context context) {
24+
try {
25+
return new String(input.readAllBytes());
26+
} catch (IOException e) {
27+
throw new RuntimeException("Failed to read input stream", e);
28+
}
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package software.amazon.lambda.powertools.kafka.testutils;
14+
15+
import com.amazonaws.services.lambda.runtime.Context;
16+
import com.amazonaws.services.lambda.runtime.RequestHandler;
17+
18+
public class StringHandler implements RequestHandler<String, String> {
19+
@Override
20+
public String handleRequest(String input, Context context) {
21+
return input;
22+
}
23+
}

0 commit comments

Comments
 (0)