Skip to content

Commit e424240

Browse files
committed
Add jackson 3.x support for springboot 4
1 parent a39062c commit e424240

File tree

28 files changed

+99
-139
lines changed

28 files changed

+99
-139
lines changed

aws-serverless-java-container-core/pom.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
<name>AWS Serverless Java container support - Core</name>
77
<description>Allows Java applications written for a servlet container to run in AWS Lambda</description>
88
<url>https://aws.amazon.com/lambda</url>
9-
<version>2.1.5-SNAPSHOT</version>
9+
<version>3.0.0-SNAPSHOT</version>
1010

1111
<parent>
1212
<groupId>com.amazonaws.serverless</groupId>
1313
<artifactId>aws-serverless-java-container</artifactId>
14-
<version>2.1.5-SNAPSHOT</version>
14+
<version>3.0.0-SNAPSHOT</version>
1515
<relativePath>..</relativePath>
1616
</parent>
1717

1818
<properties>
1919
<jaxrs.version>3.1.0</jaxrs.version>
2020
<servlet.version>6.0.0</servlet.version>
21+
<jackson.version>3.0.2</jackson.version>
2122
</properties>
2223

2324
<dependencies>
@@ -40,13 +41,13 @@
4041
</dependency>
4142

4243
<dependency>
43-
<groupId>com.fasterxml.jackson.core</groupId>
44+
<groupId>tools.jackson.core</groupId>
4445
<artifactId>jackson-databind</artifactId>
4546
<version>${jackson.version}</version>
4647
</dependency>
4748

4849
<dependency>
49-
<groupId>com.fasterxml.jackson.module</groupId>
50+
<groupId>tools.jackson.module</groupId>
5051
<artifactId>jackson-module-afterburner</artifactId>
5152
<version>${jackson.version}</version>
5253
</dependency>

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AwsProxyExceptionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.amazonaws.serverless.proxy.model.ErrorModel;
1919
import com.amazonaws.serverless.proxy.model.Headers;
2020

21-
import com.fasterxml.jackson.core.JsonProcessingException;
21+
import tools.jackson.core.JacksonException;
2222
import jakarta.ws.rs.core.Response;
2323
import org.slf4j.Logger;
2424
import org.slf4j.LoggerFactory;
@@ -103,7 +103,7 @@ protected String getErrorJson(String message) {
103103

104104
try {
105105
return LambdaContainerHandler.getObjectMapper().writeValueAsString(new ErrorModel(message));
106-
} catch (JsonProcessingException e) {
106+
} catch (JacksonException e) {
107107
log.error("Could not produce error JSON", e);
108108
return "{ \"message\": \"" + message + "\" }";
109109
}

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/LambdaContainerHandler.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
import com.amazonaws.serverless.proxy.model.ContainerConfig;
2020
import com.amazonaws.services.lambda.runtime.Context;
2121

22-
import com.fasterxml.jackson.core.JsonParseException;
23-
import com.fasterxml.jackson.databind.JsonMappingException;
24-
import com.fasterxml.jackson.databind.ObjectMapper;
25-
import com.fasterxml.jackson.databind.ObjectReader;
26-
import com.fasterxml.jackson.databind.ObjectWriter;
27-
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
22+
import tools.jackson.core.JacksonException;
23+
import tools.jackson.databind.ObjectMapper;
24+
import tools.jackson.databind.ObjectReader;
25+
import tools.jackson.databind.ObjectWriter;
2826
import org.slf4j.Logger;
2927
import org.slf4j.LoggerFactory;
3028

@@ -86,10 +84,9 @@ public abstract class LambdaContainerHandler<RequestType, ResponseType, Containe
8684
}
8785

8886
private static void registerAfterBurner() {
89-
objectMapper.registerModule(new AfterburnerModule());
87+
// AfterburnerModule is built-in in Jackson 3, no need to register
9088
}
9189

92-
9390
//-------------------------------------------------------------
9491
// Constructors
9592
//-------------------------------------------------------------
@@ -258,12 +255,9 @@ public void proxyStream(InputStream input, OutputStream output, Context context)
258255
ResponseType resp = proxy(request, context);
259256

260257
objectWriter.writeValue(output, resp);
261-
} catch (JsonParseException e) {
258+
} catch (JacksonException e) {
262259
log.error("Error while parsing request object stream", e);
263260
getObjectMapper().writeValue(output, exceptionHandler.handle(e));
264-
} catch (JsonMappingException e) {
265-
log.error("Error while mapping object to RequestType class", e);
266-
getObjectMapper().writeValue(output, exceptionHandler.handle(e));
267261
} finally {
268262
output.flush();
269263
output.close();

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/jaxrs/AwsHttpApiV2SecurityContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import com.amazonaws.serverless.proxy.internal.SecurityUtils;
1717
import com.amazonaws.serverless.proxy.model.HttpApiV2ProxyRequest;
1818
import com.amazonaws.services.lambda.runtime.Context;
19-
import com.fasterxml.jackson.core.JsonProcessingException;
20-
import com.fasterxml.jackson.databind.JsonNode;
19+
import tools.jackson.core.JacksonException;
20+
import tools.jackson.databind.JsonNode;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323

@@ -66,7 +66,7 @@ public Principal getUserPrincipal() {
6666
return (() -> {
6767
return subject;
6868
});
69-
} catch (JsonProcessingException e) {
69+
} catch (JacksonException e) {
7070
log.error("Error while attempting to parse JWT body for requestId: " + SecurityUtils.crlf(event.getRequestContext().getRequestId()), e);
7171
return null;
7272
}

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/model/HttpApiV2AuthorizerMap.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313
package com.amazonaws.serverless.proxy.model;
1414

1515
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
16-
import com.fasterxml.jackson.core.JsonGenerator;
17-
import com.fasterxml.jackson.core.JsonParser;
18-
import com.fasterxml.jackson.core.JsonProcessingException;
19-
import com.fasterxml.jackson.databind.DeserializationContext;
20-
import com.fasterxml.jackson.databind.JsonNode;
21-
import com.fasterxml.jackson.databind.SerializerProvider;
22-
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
23-
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
24-
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
25-
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
26-
import com.fasterxml.jackson.databind.type.TypeFactory;
16+
import tools.jackson.core.JsonGenerator;
17+
import tools.jackson.core.JsonParser;
18+
import tools.jackson.databind.DeserializationContext;
19+
import tools.jackson.databind.JsonNode;
20+
import tools.jackson.databind.SerializationContext;
21+
import tools.jackson.databind.annotation.JsonDeserialize;
22+
import tools.jackson.databind.annotation.JsonSerialize;
23+
import tools.jackson.databind.deser.std.StdDeserializer;
24+
import tools.jackson.databind.ser.std.StdSerializer;
25+
import tools.jackson.databind.type.TypeFactory;
2726

2827
import java.io.IOException;
2928
import java.util.HashMap;
@@ -77,18 +76,17 @@ public HttpApiV2AuthorizerDeserializer() {
7776
}
7877

7978
@Override
80-
public HttpApiV2AuthorizerMap deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
81-
throws IOException, JsonProcessingException {
79+
public HttpApiV2AuthorizerMap deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) {
8280
HttpApiV2AuthorizerMap map = new HttpApiV2AuthorizerMap();
83-
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
81+
JsonNode node = deserializationContext.readTree(jsonParser);
8482
if (node.has(JWT_KEY)) {
8583
HttpApiV2JwtAuthorizer authorizer = LambdaContainerHandler.getObjectMapper()
8684
.treeToValue(node.get(JWT_KEY), HttpApiV2JwtAuthorizer.class);
8785
map.putJwtAuthorizer(authorizer);
8886
}
8987
if (node.has(LAMBDA_KEY)) {
9088
Map<String, Object> context = LambdaContainerHandler.getObjectMapper().treeToValue(node.get(LAMBDA_KEY),
91-
TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, Object.class));
89+
LambdaContainerHandler.getObjectMapper().getTypeFactory().constructMapType(HashMap.class, String.class, Object.class));
9290
map.put(LAMBDA_KEY, context);
9391
}
9492
if (node.has(IAM_KEY)) {
@@ -110,16 +108,19 @@ public HttpApiV2AuthorizerSerializer() {
110108

111109
@Override
112110
public void serialize(HttpApiV2AuthorizerMap httpApiV2AuthorizerMap, JsonGenerator jsonGenerator,
113-
SerializerProvider serializerProvider) throws IOException {
111+
SerializationContext serializationContext) {
114112
jsonGenerator.writeStartObject();
115113
if (httpApiV2AuthorizerMap.isJwt()) {
116-
jsonGenerator.writeObjectField(JWT_KEY, httpApiV2AuthorizerMap.getJwtAuthorizer());
114+
jsonGenerator.writeName(JWT_KEY);
115+
jsonGenerator.writePOJO(httpApiV2AuthorizerMap.getJwtAuthorizer());
117116
}
118117
if (httpApiV2AuthorizerMap.isLambda()) {
119-
jsonGenerator.writeObjectField(LAMBDA_KEY, httpApiV2AuthorizerMap.getLambdaAuthorizerContext());
118+
jsonGenerator.writeName(LAMBDA_KEY);
119+
jsonGenerator.writePOJO(httpApiV2AuthorizerMap.getLambdaAuthorizerContext());
120120
}
121121
if (httpApiV2AuthorizerMap.isIam()) {
122-
jsonGenerator.writeObjectField(IAM_KEY, httpApiV2AuthorizerMap.get(IAM_KEY));
122+
jsonGenerator.writeName(IAM_KEY);
123+
jsonGenerator.writePOJO(httpApiV2AuthorizerMap.get(IAM_KEY));
123124
}
124125
jsonGenerator.writeEndObject();
125126
}

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/AwsProxyExceptionHandlerTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import com.amazonaws.serverless.exceptions.InvalidResponseObjectException;
66
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
77
import com.amazonaws.serverless.proxy.model.ErrorModel;
8-
import com.fasterxml.jackson.core.JsonProcessingException;
9-
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import tools.jackson.core.JacksonException;
9+
import tools.jackson.databind.ObjectMapper;
1010

1111
import static org.junit.jupiter.api.Assertions.*;
1212
import static org.mockito.ArgumentMatchers.any;
@@ -47,7 +47,7 @@ void typedHandle_InvalidRequestEventException_500State() {
4747

4848
@Test
4949
void typedHandle_InvalidRequestEventException_responseString()
50-
throws JsonProcessingException {
50+
throws JacksonException {
5151
AwsProxyResponse resp = exceptionHandler.handle(new InvalidRequestEventException(INVALID_REQUEST_MESSAGE, null));
5252

5353
assertNotNull(resp);
@@ -74,7 +74,7 @@ void typedHandle_InvalidResponseObjectException_502State() {
7474

7575
@Test
7676
void typedHandle_InvalidResponseObjectException_responseString()
77-
throws JsonProcessingException {
77+
throws JacksonException {
7878
AwsProxyResponse resp = exceptionHandler.handle(new InvalidResponseObjectException(INVALID_RESPONSE_MESSAGE, null));
7979

8080
assertNotNull(resp);
@@ -106,7 +106,7 @@ void typedHandle_InternalServerErrorException_500State() {
106106

107107
@Test
108108
void typedHandle_InternalServerErrorException_responseString()
109-
throws JsonProcessingException {
109+
throws JacksonException {
110110
InternalServerErrorException mockInternalServerErrorException = Mockito.mock(InternalServerErrorException.class);
111111
Mockito.when(mockInternalServerErrorException.getMessage()).thenReturn(INTERNAL_SERVER_ERROR_MESSAGE);
112112

@@ -131,7 +131,7 @@ void typedHandle_InternalServerErrorException_jsonContentTypeHeader() {
131131

132132
@Test
133133
void typedHandle_NullPointerException_responseObject()
134-
throws JsonProcessingException {
134+
throws JacksonException {
135135
AwsProxyResponse resp = exceptionHandler.handle(new NullPointerException());
136136

137137
assertNotNull(resp);
@@ -248,7 +248,7 @@ void getErrorJson_ErrorModel_validJson()
248248
void getErrorJson_JsonParsinException_validJson()
249249
throws IOException {
250250
ObjectMapper mockMapper = mock(ObjectMapper.class);
251-
JsonProcessingException exception = mock(JsonProcessingException.class);
251+
JacksonException exception = mock(JacksonException.class);
252252
when(mockMapper.writeValueAsString(any(Object.class))).thenThrow(exception);
253253

254254
String output = exceptionHandler.getErrorJson(INVALID_RESPONSE_MESSAGE);

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/testutils/AwsProxyRequestBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
1616
import com.amazonaws.serverless.proxy.model.*;
1717

18-
import com.fasterxml.jackson.core.JsonProcessingException;
19-
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import tools.jackson.core.JacksonException;
19+
import tools.jackson.databind.ObjectMapper;
2020
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2121
import org.apache.commons.io.IOUtils;
2222
import org.apache.hc.core5.http.ContentType;
@@ -106,7 +106,7 @@ public AwsProxyRequestBuilder alb() {
106106
try {
107107
String json = objectMapper.writeValueAsString(this.request);
108108
albRequest = objectMapper.readValue(json, AwsProxyRequest.class);
109-
} catch (JsonProcessingException jpe) {
109+
} catch (JacksonException jpe) {
110110
throw new RuntimeException(jpe);
111111
}
112112

@@ -265,7 +265,7 @@ public AwsProxyRequestBuilder body(Object body) {
265265
if (request.getMultiValueHeaders() != null && request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE).startsWith(MediaType.APPLICATION_JSON)) {
266266
try {
267267
return body(LambdaContainerHandler.getObjectMapper().writeValueAsString(body));
268-
} catch (JsonProcessingException e) {
268+
} catch (JacksonException e) {
269269
throw new UnsupportedOperationException("Could not serialize object: " + e.getMessage());
270270
}
271271
} else {
@@ -438,7 +438,7 @@ public InputStream buildStream() {
438438
try {
439439
String requestJson = LambdaContainerHandler.getObjectMapper().writeValueAsString(request);
440440
return new ByteArrayInputStream(requestJson.getBytes(StandardCharsets.UTF_8));
441-
} catch (JsonProcessingException e) {
441+
} catch (JacksonException e) {
442442
return null;
443443
}
444444
}
@@ -448,7 +448,7 @@ public InputStream toHttpApiV2RequestStream() {
448448
try {
449449
String requestJson = LambdaContainerHandler.getObjectMapper().writeValueAsString(req);
450450
return new ByteArrayInputStream(requestJson.getBytes(StandardCharsets.UTF_8));
451-
} catch (JsonProcessingException e) {
451+
} catch (JacksonException e) {
452452
return null;
453453
}
454454
}

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/model/AwsProxyRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.io.IOException;
88
import org.junit.jupiter.api.Test;
99
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
10-
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import tools.jackson.databind.ObjectMapper;
1111

1212
public class AwsProxyRequestTest {
1313
private static final String CUSTOM_HEADER_KEY_LOWER_CASE = "custom-header";

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/model/HttpApiV2ProxyRequestTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.amazonaws.serverless.proxy.model;
22

33
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
4-
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import tools.jackson.core.JacksonException;
55
import org.junit.jupiter.api.Test;
66

77
import java.util.ArrayList;
@@ -181,7 +181,7 @@ void deserialize_fromJsonString_authorizerPopulatedCorrectly() {
181181
assertTrue(req.getRequestContext().getAuthorizer().getJwtAuthorizer().getClaims().containsKey("claim1"));
182182
assertEquals(2, req.getRequestContext().getAuthorizer().getJwtAuthorizer().getScopes().size());
183183
assertEquals(RequestSource.API_GATEWAY, req.getRequestSource());
184-
} catch (JsonProcessingException e) {
184+
} catch (JacksonException e) {
185185
e.printStackTrace();
186186
fail("Exception while parsing request" + e.getMessage());
187187
}
@@ -196,7 +196,7 @@ void deserialize_fromJsonString_authorizerEmptyMap() {
196196
assertFalse(req.getRequestContext().getAuthorizer().isJwt());
197197
assertFalse(req.getRequestContext().getAuthorizer().isLambda());
198198
assertFalse(req.getRequestContext().getAuthorizer().isIam());
199-
} catch (JsonProcessingException e) {
199+
} catch (JacksonException e) {
200200
e.printStackTrace();
201201
fail("Exception while parsing request" + e.getMessage());
202202
}
@@ -212,7 +212,7 @@ void deserialize_fromJsonString_lambdaAuthorizer() {
212212
assertTrue(req.getRequestContext().getAuthorizer().isLambda());
213213
assertEquals(5, req.getRequestContext().getAuthorizer().getLambdaAuthorizerContext().size());
214214
assertEquals(1, req.getRequestContext().getAuthorizer().getLambdaAuthorizerContext().get("numberKey"));
215-
} catch (JsonProcessingException e) {
215+
} catch (JacksonException e) {
216216
e.printStackTrace();
217217
fail("Exception while parsing request" + e.getMessage());
218218
}
@@ -239,7 +239,7 @@ void deserialize_fromJsonString_iamAuthorizer() {
239239
req.getRequestContext().getAuthorizer().getIamAuthorizer().getUserArn());
240240
assertEquals("AIDACOSFODNN7EXAMPLE2",
241241
req.getRequestContext().getAuthorizer().getIamAuthorizer().getUserId());
242-
} catch (JsonProcessingException e) {
242+
} catch (JacksonException e) {
243243
e.printStackTrace();
244244
fail("Exception while parsing request" + e.getMessage());
245245
}
@@ -254,7 +254,7 @@ void deserialize_fromJsonString_isBase64EncodedPopulates() {
254254
req = LambdaContainerHandler.getObjectMapper().readValue(NO_AUTH_PROXY, HttpApiV2ProxyRequest.class);
255255
assertTrue(req.isBase64Encoded());
256256
assertEquals(RequestSource.API_GATEWAY, req.getRequestSource());
257-
} catch (JsonProcessingException e) {
257+
} catch (JacksonException e) {
258258
e.printStackTrace();
259259
fail("Exception while parsing request" + e.getMessage());
260260
}
@@ -277,7 +277,7 @@ void serialize_toJsonString_authorizerPopulatesCorrectly() {
277277
assertTrue(reqString.contains("\"scopes\":[\"first\",\"second\"]"));
278278
assertTrue(reqString.contains("\"authorizer\":{\"jwt\":{"));
279279
assertTrue(reqString.contains("\"isBase64Encoded\":false"));
280-
} catch (JsonProcessingException e) {
280+
} catch (JacksonException e) {
281281
e.printStackTrace();
282282
fail("Exception while serializing request" + e.getMessage());
283283
}

0 commit comments

Comments
 (0)