Skip to content

Commit 5ba49d7

Browse files
authored
Merge pull request #190 from networknt/fix-shared-lambda-runtime-mapper
Adopt the shared Lambda mapper and qualify native request validation
2 parents af992cb + 13585d5 commit 5ba49d7

7 files changed

Lines changed: 124 additions & 31 deletions

File tree

‎README.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ python3 tests/test_native_image_script.py
3333
```
3434

3535
These tests stub Maven and Docker to check packaging and failure handling.
36-
They are not run by Maven or the current CI workflow.
36+
They are not run by Maven. The source-build CI workflow runs them separately from the real native smoke.
3737

3838
### Test with Main
3939

@@ -127,7 +127,7 @@ software.amazon.awssdk.services.lambda.model.ResourceNotFoundException: Function
127127

128128
This source line uses Jackson 3.2.3 and json-schema-validator 3.0.8. Build the Jackson 3 light-4j, http-client, REST and AWS Lambda prerequisites first; older snapshot binaries are not compatible. Jackson annotations retain their com.fasterxml.jackson.annotation package.
129129

130-
The runtime mapper uses Jackson2Defaults with its existing permissive unknown-property override. Mapper instances are immutable. Java time and Optional support are built in. Classic SnakeYAML compatibility pins are retained until the stack-wide cleanup.
130+
The runtime uses the event-only `LambdaRuntimeMappers.mapper()` API from custom-runtime, built from `JacksonDefaults` with its permissive unknown-property override. Mapper instances are immutable. Java time and Optional support are built in. Classic SnakeYAML compatibility pins are retained until the stack-wide cleanup.
131131

132132
Local qualification includes the full JVM suite, sanitized event/validation parity against the Jackson 2 baseline, and an offline GraalVM 25 native-image build. A native-image build does not establish deployed AWS behavior or complete reflection coverage for every middleware/configuration. The separate conformance worktree is deferred.
133133

@@ -138,7 +138,11 @@ MAVEN_REPO_LOCAL=/path/to/isolated/m2 bash native-image-linux.sh
138138
python3 tests/native_runtime_smoke.py target/custom-runtime/lambda-native
139139
```
140140

141-
The smoke check binds an API Gateway event, runs the local health handler, and verifies success and builder-based error serialization. It uses no AWS endpoint or credentials. `-H:+AddAllCharsets` is required by SnakeYAML Engine's UTF-32 input detection. The check does not qualify every middleware chain or deployed AWS behavior.
141+
The smoke check binds API Gateway events with an unknown future field and runs specification, validator and health handlers. It checks valid referenced query/header/body schemas, invalid query/header/body values, an unknown path, a method mismatch, and builder-based error serialization. The OpenAPI document and handler/validator settings are controlled fixtures. The status catalog and injection settings are copied from the packaged application's `config/status.yml` and `config/config.yml`; their contents are not duplicated in the fixture. By default the script requires exactly one shaded `target/lambda-native-*.jar` (excluding sources and Javadoc JARs); use `--application-jar /path/to/runtime.jar` to select one explicitly.
142+
143+
It uses no AWS endpoint or credentials. `-H:+AddAllCharsets` is required by SnakeYAML Engine's UTF-32 input detection. The check does not qualify every middleware chain, all default-resource packaging, or deployed AWS behavior.
144+
145+
The native reflection file currently includes version-specific JSON overlay sideband accessors (`MapOverlay$1.getOverlay()` and `ListOverlay$1.getOverlay()`) and the OpenAPI path cache's Caffeine factories (`SSMS.FACTORY` and `PSMS()`), covered by this smoke. These remain application registrations until their owning libraries ship equivalent native metadata. Migrate native consumers together and remove these entries only after updated source pins and native smoke results establish equivalent coverage; moving them is separate from this mapper change.
142146

143147
### Reproducible Jackson 3 source build
144148

‎ci/jackson3-source-refs.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"http-client": "5ecee29dbb08d813512f5379c2e1b902d553c5d4",
33
"json-overlay": "e309fd63f95317067c9b13a4bed7ac16727bfea5",
44
"light-4j": "97b2359b92ed3e1d05cac04f2da8760aa954e49d",
5-
"light-aws-lambda": "dddbbc9e7f6f690f8688ceac024728b6a84f4808",
5+
"light-aws-lambda": "5913c7c25b01b6d384fd9c39014dc91d63dfc35e",
66
"light-rest-4j": "a31955945036d9108b7e9bb72167415cc9cfc287",
77
"openapi-parser": "9ae849056e49f7c796fe28bfe6093fbe4847cc5c",
88
"yaml-rule": "c237cc3a25720f43e57bf571e25d5fa06687aa3b"

‎reflect.json‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
[
2+
{
3+
"name": "com.github.benmanes.caffeine.cache.SSMS",
4+
"fields": [{ "name": "FACTORY" }]
5+
},
6+
{
7+
"name": "com.github.benmanes.caffeine.cache.PSMS",
8+
"methods": [{ "name": "<init>", "parameterTypes": [] }]
9+
},
10+
{
11+
"name": "com.networknt.jsonoverlay.MapOverlay$1",
12+
"methods": [{ "name": "getOverlay", "parameterTypes": [] }]
13+
},
14+
{
15+
"name": "com.networknt.jsonoverlay.ListOverlay$1",
16+
"methods": [{ "name": "getOverlay", "parameterTypes": [] }]
17+
},
218
{
319
"name": "com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent",
420
"methods": [

‎src/main/java/com/networknt/aws/lambda/Main.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
44
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
5-
import tools.jackson.databind.DeserializationFeature;
65
import tools.jackson.databind.ObjectMapper;
7-
import com.networknt.jackson.JacksonDefaults;
86
import com.networknt.aws.lambda.app.LambdaApp;
97

108
import java.io.IOException;
119

1210

1311
public class Main {
14-
private static final ObjectMapper OBJECT_MAPPER = JacksonDefaults.builder()
15-
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build();
12+
private static final ObjectMapper OBJECT_MAPPER = LambdaRuntimeMappers.mapper();
1613

1714
public static void main(String[] args) throws IOException {
1815

‎src/test/java/com/networknt/aws/lambda/Jackson3NativeCompatibilityTest.java‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@
55
import org.junit.jupiter.api.Test;
66
import tools.jackson.databind.ObjectMapper;
77

8-
import java.lang.reflect.Field;
9-
10-
import static org.junit.jupiter.api.Assertions.*;
8+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
1110

1211
class Jackson3NativeCompatibilityTest {
13-
private ObjectMapper mainMapper() throws Exception {
14-
Field field = Main.class.getDeclaredField("OBJECT_MAPPER");
15-
field.setAccessible(true);
16-
return (ObjectMapper) field.get(null);
17-
}
1812

1913
@Test
2014
void runtimeMapperKeepsPermissiveEventBinding() throws Exception {
21-
APIGatewayProxyRequestEvent event = mainMapper().readValue(
15+
APIGatewayProxyRequestEvent event = LambdaRuntimeMappers.mapper().readValue(
2216
"{\"path\":\"/v1/pets\",\"httpMethod\":\"POST\",\"futureField\":true} trailing",
2317
APIGatewayProxyRequestEvent.class);
2418
assertEquals("/v1/pets", event.getPath());
@@ -27,7 +21,7 @@ void runtimeMapperKeepsPermissiveEventBinding() throws Exception {
2721

2822
@Test
2923
void responseRetainsJackson2ShapeAndRoundTrips() throws Exception {
30-
ObjectMapper mapper = mainMapper();
24+
ObjectMapper mapper = LambdaRuntimeMappers.mapper();
3125
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
3226
.withStatusCode(200).withBody("{\"ok\":true}").withIsBase64Encoded(false);
3327
String json = mapper.writeValueAsString(response);
@@ -39,6 +33,6 @@ void responseRetainsJackson2ShapeAndRoundTrips() throws Exception {
3933
@Test
4034
void runtimeMapperRetainsJackson2StreamDepth() throws Exception {
4135
String nested = "[".repeat(600) + "0" + "]".repeat(600);
42-
assertDoesNotThrow(() -> mainMapper().readTree(nested));
36+
assertDoesNotThrow(() -> LambdaRuntimeMappers.mapper().readTree(nested));
4337
}
4438
}

‎src/test/java/com/networknt/aws/lambda/TestUtils.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
44
import tools.jackson.core.JacksonException;
5-
import tools.jackson.databind.DeserializationFeature;
65
import tools.jackson.databind.ObjectMapper;
7-
import com.networknt.jackson.JacksonDefaults;
86

97
public class TestUtils {
108

@@ -133,8 +131,7 @@ public class TestUtils {
133131
" }\n" +
134132
"}";
135133

136-
private static final ObjectMapper OBJECT_MAPPER = JacksonDefaults.builder()
137-
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build();
134+
private static final ObjectMapper OBJECT_MAPPER = LambdaRuntimeMappers.mapper();
138135

139136
public static APIGatewayProxyRequestEvent createTestRequestEvent() {
140137
APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent;

‎tests/native_runtime_smoke.py‎

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
import subprocess
1010
import tempfile
1111
import threading
12+
import zipfile
1213

1314

14-
def invoke(binary, handler, expected_endpoint):
15+
def invoke(binary, status_catalog, handler, expected_endpoint, event, *, config_catalog):
1516
received = queue.Queue()
16-
event = {"path": "/health", "httpMethod": "GET", "body": None,
17-
"isBase64Encoded": False, "headers": {}, "multiValueHeaders": {},
18-
"requestContext": {"requestId": "native-smoke", "identity": {"sourceIp": "127.0.0.1"}}}
1917

2018
class RuntimeApi(http.server.BaseHTTPRequestHandler):
2119
def log_message(self, *args):
@@ -39,19 +37,73 @@ def do_POST(self):
3937

4038
with tempfile.TemporaryDirectory(prefix="native-runtime-smoke-") as directory:
4139
config = Path(directory)
40+
(config / "config.yml").write_bytes(config_catalog)
4241
(config / "handler.yml").write_text("""enabled: true
4342
handlers:
43+
- com.networknt.aws.lambda.handler.middleware.specification.OpenApiMiddleware@spec
44+
- com.networknt.aws.lambda.handler.middleware.validator.ValidatorMiddleware@validator
4445
- com.networknt.aws.lambda.handler.health.HealthCheckHandler@health
4546
chains: {}
4647
paths:
4748
- path: /health
4849
method: get
49-
exec: [health]
50-
defaultHandlers: [health]
50+
exec: [spec, validator, health]
51+
defaultHandlers: [spec, validator, health]
5152
""")
5253
(config / "health.yml").write_text("enabled: true\ndownstreamEnabled: false\n")
5354
(config / "lambda-app.yml").write_text("lambdaAppId: native-smoke\nencodeBase64Request: false\nencodeBase64Response: false\n")
5455
(config / "values.yml").write_text("{}\n")
56+
(config / "service.yml").write_text("singletons: []\n")
57+
(config / "status.yml").write_bytes(status_catalog)
58+
# Controlled fixture settings exercise validation; this is not default-config qualification.
59+
(config / "openapi-inject.yml").write_text("{}\n")
60+
(config / "openapi-handler.yml").write_text("enabled: true\nmultipleSpec: false\n")
61+
(config / "openapi-validator.yml").write_text("enabled: true\nskipPathPrefixes: []\n")
62+
(config / "openapi.yaml").write_text("""openapi: 3.0.3
63+
info:
64+
title: Native validation smoke
65+
version: '1'
66+
servers:
67+
- url: /
68+
paths:
69+
/health:
70+
get:
71+
parameters:
72+
- name: count
73+
in: query
74+
required: true
75+
schema:
76+
$ref: '#/components/schemas/Count'
77+
- name: x-count
78+
in: header
79+
required: true
80+
schema:
81+
$ref: '#/components/schemas/Count'
82+
responses:
83+
'200':
84+
description: OK
85+
/body:
86+
post:
87+
requestBody:
88+
required: true
89+
content:
90+
application/json:
91+
schema:
92+
$ref: '#/components/schemas/Payload'
93+
responses:
94+
'200':
95+
description: OK
96+
components:
97+
schemas:
98+
Count:
99+
type: integer
100+
Payload:
101+
type: object
102+
required: [value]
103+
properties:
104+
value:
105+
type: integer
106+
""")
55107
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), RuntimeApi)
56108
thread = threading.Thread(target=server.serve_forever, daemon=True)
57109
thread.start()
@@ -85,15 +137,48 @@ def do_POST(self):
85137
def main():
86138
parser = argparse.ArgumentParser()
87139
parser.add_argument("binary", type=Path)
140+
parser.add_argument("--application-jar", type=Path,
141+
help="Shaded runtime JAR providing framework config and status catalog (default: target/lambda-native-*.jar)")
88142
args = parser.parse_args()
89143
binary = args.binary.resolve(strict=True)
90-
response = invoke(binary, "com.networknt.aws.lambda.app.LambdaApp", "response")
144+
jars = [args.application_jar] if args.application_jar else [
145+
jar for jar in (Path(__file__).resolve().parents[1] / "target").glob("lambda-native-*.jar")
146+
if not jar.name.endswith(("-sources.jar", "-javadoc.jar"))]
147+
if len(jars) != 1:
148+
parser.error("Expected one shaded runtime JAR; specify --application-jar explicitly")
149+
with zipfile.ZipFile(jars[0]) as application_jar:
150+
status_catalog = application_jar.read("config/status.yml")
151+
config_catalog = application_jar.read("config/config.yml")
152+
event = {"path": "/health", "httpMethod": "GET", "body": None,
153+
"isBase64Encoded": False, "headers": {"x-count": "7"}, "multiValueHeaders": {},
154+
"queryStringParameters": {"count": "7"}, "futureEventField": {"nested": True},
155+
"requestContext": {"requestId": "native-smoke", "identity": {"sourceIp": "127.0.0.1"}}}
156+
handler = "com.networknt.aws.lambda.app.LambdaApp"
157+
response = invoke(binary, status_catalog, handler, "response", event, config_catalog=config_catalog)
91158
assert response["statusCode"] == 200, response
92159
assert response["body"] == "OK", response
93160
assert response["isBase64Encoded"] is False, response
94-
error = invoke(binary, "native.smoke.MissingHandler", "error")
161+
body_event = event | {"path": "/body", "httpMethod": "POST", "body": '{"value":7}',
162+
"headers": {"content-type": "application/json"}, "queryStringParameters": {}}
163+
body_response = invoke(binary, status_catalog, handler, "response", body_event, config_catalog=config_catalog)
164+
assert body_response["statusCode"] == 200, body_response
165+
assert body_response["body"] == "OK", body_response
166+
cases = [
167+
("query", event | {"queryStringParameters": {"count": "invalid"}}, 400, "ERR11004"),
168+
("header", event | {"headers": {"x-count": "invalid"}}, 400, "ERR11004"),
169+
("body", body_event | {"body": '{"value":"invalid"}'}, 400, "ERR11004"),
170+
("unknown path", event | {"path": "/missing"}, 404, "ERR10007"),
171+
("method mismatch", event | {"httpMethod": "DELETE"}, 405, "ERR10008"),
172+
]
173+
for name, invalid_event, status, code in cases:
174+
invalid = invoke(binary, status_catalog, handler, "response", invalid_event, config_catalog=config_catalog)
175+
assert invalid["statusCode"] == status, (name, invalid)
176+
assert json.loads(invalid["body"])["code"] == code, (name, invalid)
177+
error = invoke(binary, status_catalog, "native.smoke.MissingHandler", "error", event, config_catalog=config_catalog)
95178
assert "native.smoke.MissingHandler" in error["message"], error
96179
print("Native Runtime smoke passed: API Gateway binding, health response and builder error serialization")
180+
print("Native Runtime validation passed: unknown event fields and referenced query-schema rejection")
181+
print("Native Runtime validation passed: body and header schemas, unknown path and method mismatch")
97182

98183

99184
if __name__ == "__main__":

0 commit comments

Comments
 (0)