Skip to content

Commit af649e4

Browse files
authored
Merge pull request #122 from networknt/121-path-params-are-not-present-proxy-event-object-when-light-native-lambda-deployed-behind-aws-load-balancer
Added method of resolving path parameters and attaching them to the request event.
2 parents 1451e5f + 464912d commit af649e4

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/main/java/com/networknt/aws/lambda/handler/middleware/specification/OpenApiMiddleware.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public Status execute(LightLambdaExchange exchange) {
6161
final NormalisedPath openApiPathString = maybeApiPath.get();
6262
final Path path = helper.openApi3.getPath(openApiPathString.original());
6363

64+
/* populate the event with the path parameter values. */
65+
exchange.getRequest().setPathParameters(this.getPathParamsMap(requestPath, openApiPathString));
66+
6467
final String httpMethod = exchange.getRequest().getHttpMethod().toLowerCase();
6568
final Operation operation = path.getOperation(httpMethod);
6669

@@ -85,6 +88,41 @@ public Status execute(LightLambdaExchange exchange) {
8588
return successMiddlewareStatus();
8689
}
8790

91+
/**
92+
* Grabs the path parameters from the original path based on the provided specification path.
93+
*
94+
* @param original - The original path containing potential parameter values.
95+
* @param specPath - The specification path that contains potential parameter keys.
96+
* @return - returns a map containing the path parameter names and values.
97+
*/
98+
private Map<String, String> getPathParamsMap(final NormalisedPath original, final NormalisedPath specPath) {
99+
100+
if (!specPath.original().contains("{")) {
101+
LOG.debug("Provided path does not contain any path parameters.");
102+
return new HashMap<>();
103+
104+
} else if (original.parts().size() != specPath.parts().size()) {
105+
LOG.warn("Number of path parts in original does not match the specification.");
106+
return new HashMap<>();
107+
}
108+
109+
final var params = new HashMap<String, String>();
110+
final var originalParts = original.parts();
111+
final var specParts = specPath.parts();
112+
113+
for (int x = 0; x < original.parts().size(); x++) {
114+
final var specPart = specParts.get(x);
115+
116+
if (specPart.startsWith("{") && specPart.endsWith("}")) {
117+
final var paramKey = specPart.substring(1, specPart.length() - 1);
118+
final var pathParamValue = originalParts.get(x);
119+
LOG.trace("Specification path part {} with a key of {} and a value of {}", x, paramKey, pathParamValue);
120+
params.put(paramKey, pathParamValue);
121+
}
122+
}
123+
return params;
124+
}
125+
88126
/**
89127
* Validates the injectMap and openapiMap.Throws an exception if not valid.
90128
*

src/test/java/com/networknt/aws/lambda/middleware/specification/OpenApiMiddlewareTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
public class OpenApiMiddlewareTest {
2424
private static final Logger LOG = LoggerFactory.getLogger(OpenApiMiddlewareTest.class);
25+
2526
@Test
2627
public void testConstructor() {
2728
OpenApiMiddleware openApiMiddleware = new OpenApiMiddleware();
@@ -57,4 +58,37 @@ public void testOpenApiMiddleware() {
5758
Assertions.assertNotNull(openApiOperation);
5859

5960
}
61+
62+
@Test
63+
public void testOpenApiMiddlewarePathParams() {
64+
var requestEvent = TestUtils.createTestRequestEvent();
65+
requestEvent.setPath("/v1/pets/44");
66+
requestEvent.setHttpMethod("GET");
67+
68+
InvocationResponse invocation = InvocationResponse.builder()
69+
.requestId("12345")
70+
.event(requestEvent)
71+
.build();
72+
73+
Context lambdaContext = new LambdaContext(invocation.getRequestId());
74+
final var exchange = new LightLambdaExchange(lambdaContext, null);
75+
exchange.setInitialRequest(requestEvent);
76+
OpenApiMiddleware openApiMiddleware = new OpenApiMiddleware();
77+
openApiMiddleware.execute(exchange);
78+
requestEvent = exchange.getRequest();
79+
Assertions.assertNotNull(requestEvent);
80+
81+
Map<String, Object> auditInfo = (Map<String, Object>)exchange.getAttachment(AUDIT_ATTACHMENT_KEY);;
82+
Assertions.assertNotNull(auditInfo);
83+
String endpoint = (String)auditInfo.get(Constants.ENDPOINT_STRING);
84+
Assertions.assertNotNull(endpoint);
85+
Assertions.assertEquals("/pets/{petId}@get", endpoint);
86+
87+
final var pathParameter = requestEvent.getPathParameters().get("petId");
88+
Assertions.assertNotNull(pathParameter);
89+
Assertions.assertEquals("44", pathParameter);
90+
91+
Object openApiOperation = auditInfo.get(Constants.OPENAPI_OPERATION_STRING);
92+
Assertions.assertNotNull(openApiOperation);
93+
}
6094
}

0 commit comments

Comments
 (0)