Skip to content

feat(v2): Validation failures return 400s #1489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package software.amazon.lambda.powertools.validation.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
Expand All @@ -38,15 +40,24 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {

String body = "{id";

Map<String, String> headers = new HashMap<>();
headers.put("header1", "value1,value2,value3");
Map<String, List<String>> headersList = new HashMap<>();
List<String> headerValues = new ArrayList<>();
headerValues.add("value1");
headerValues.add("value2");
headerValues.add("value3");
headersList.put("header1", headerValues);

final APIGatewayProxyResponseEvent apiGWProxyResponseEvent = new APIGatewayProxyResponseEvent()
.withBody(body)
.withHeaders(Map.of("header1", "value1,value2,value3"))
.withMultiValueHeaders(Map.of("header1", List.of("value1", "value2", "value3")));
.withHeaders(headers)
.withMultiValueHeaders(headersList);

APIGatewayV2HTTPResponse apiGWV2HTTPResponse = new APIGatewayV2HTTPResponse();
apiGWV2HTTPResponse.setBody(body);
apiGWV2HTTPResponse.setHeaders(Map.of("header1", "value1,value2,value3"));
apiGWV2HTTPResponse.setMultiValueHeaders(Map.of("header1", List.of("value1", "value2", "value3")));
apiGWV2HTTPResponse.setHeaders(headers);
apiGWV2HTTPResponse.setMultiValueHeaders(headersList);

return Stream.of(apiGWProxyResponseEvent, apiGWV2HTTPResponse).map(Arguments::of);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
Expand Down Expand Up @@ -137,21 +139,27 @@ void testValidateOutboundJsonSchemaWithHandledExceptions(Object object) throws T

Object response = validationAspect.around(pjp, validation);
assertThat(response).isInstanceOfAny(APIGatewayProxyResponseEvent.class, APIGatewayV2HTTPResponse.class);

List<String> headerValues = new ArrayList<>();
headerValues.add("value1");
headerValues.add("value2");
headerValues.add("value3");

if (response instanceof APIGatewayProxyResponseEvent) {
assertThat(response).isInstanceOfSatisfying(APIGatewayProxyResponseEvent.class, t -> {
assertThat(t.getStatusCode()).isEqualTo(400);
assertThat(t.getBody()).isNotBlank();
assertThat(t.getIsBase64Encoded()).isFalse();
assertThat(t.getHeaders()).containsEntry("header1", "value1,value2,value3");
assertThat(t.getMultiValueHeaders()).containsEntry("header1", List.of("value1", "value2", "value3"));
assertThat(t.getMultiValueHeaders()).containsEntry("header1", headerValues);
});
} else if (response instanceof APIGatewayV2HTTPResponse) {
assertThat(response).isInstanceOfSatisfying(APIGatewayV2HTTPResponse.class, t -> {
assertThat(t.getStatusCode()).isEqualTo(400);
assertThat(t.getBody()).isNotBlank();
assertThat(t.getIsBase64Encoded()).isFalse();
assertThat(t.getHeaders()).containsEntry("header1", "value1,value2,value3");
assertThat(t.getMultiValueHeaders()).containsEntry("header1", List.of("value1", "value2", "value3"));
assertThat(t.getMultiValueHeaders()).containsEntry("header1", headerValues);
});
} else {
fail();
Expand Down Expand Up @@ -198,21 +206,29 @@ public void validate_inputOK_schemaInClasspath_shouldValidate() {
@Test
public void validate_inputKO_schemaInClasspath_shouldThrowValidationException() {
GenericSchemaV7APIGatewayProxyRequestEventHandler handler = new GenericSchemaV7APIGatewayProxyRequestEventHandler();
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setBody("{" +
" \"id\": 1," +
" \"name\": \"Lampshade\"," +
" \"price\": -2" +
"}");
event.setHeaders(Map.of("header1", "value1"));
event.setMultiValueHeaders(Map.of("header1", List.of("value1")));

// price is negative
APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
assertThat(response.getBody()).isNotBlank();
assertThat(response.getStatusCode()).isEqualTo(400);
assertThat(response.getHeaders()).isEmpty();
assertThat(response.getMultiValueHeaders()).isEmpty();
Map<String, String> headers = new HashMap<>();
headers.put("header1", "value1");
Map<String, List<String>> headersList = new HashMap<>();
List<String> headerValues = new ArrayList<>();
headerValues.add("value1");
headersList.put("header1", headerValues);

APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setBody("{" +
" \"id\": 1," +
" \"name\": \"Lampshade\"," +
" \"price\": -2" +
"}");
event.setHeaders(headers);
event.setMultiValueHeaders(headersList);

// price is negative
APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
assertThat(response.getBody()).isNotBlank();
assertThat(response.getStatusCode()).isEqualTo(400);
assertThat(response.getHeaders()).isEmpty();
assertThat(response.getMultiValueHeaders()).isEmpty();
}

@Test
Expand All @@ -234,18 +250,18 @@ public void validate_inputOK_schemaInString_shouldValidate() {
@Test
public void validate_inputKO_schemaInString_shouldThrowValidationException() {
ValidationInboundAPIGatewayV2HTTPEventHandler handler = new ValidationInboundAPIGatewayV2HTTPEventHandler();
APIGatewayV2HTTPEvent event = new APIGatewayV2HTTPEvent();
event.setBody("{" +
" \"id\": 1," +
" \"name\": \"Lampshade\"" +
"}");
event.setHeaders(Map.of("header1", "value1"));
APIGatewayV2HTTPResponse response = handler.handleRequest(event, context);
assertThat(response.getBody()).isNotBlank();
assertThat(response.getStatusCode()).isEqualTo(400);
assertThat(response.getHeaders()).isEmpty();
assertThat(response.getMultiValueHeaders()).isEmpty();
APIGatewayV2HTTPEvent event = new APIGatewayV2HTTPEvent();
event.setBody("{" +
" \"id\": 1," +
" \"name\": \"Lampshade\"" +
"}");
event.setHeaders(Map.of("header1", "value1"));

APIGatewayV2HTTPResponse response = handler.handleRequest(event, context);
assertThat(response.getBody()).isNotBlank();
assertThat(response.getStatusCode()).isEqualTo(400);
assertThat(response.getHeaders()).isEmpty();
assertThat(response.getMultiValueHeaders()).isEmpty();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
APIGatewayV2HTTPEvent event = new APIGatewayV2HTTPEvent();
event.setBody("{" +
" \"id\": 1," +
" \"name\": \"Lampshade\"" +
"}");
event.setHeaders(Map.of("header1", "value1"));
APIGatewayV2HTTPResponse response = handler.handleRequest(event, context);
assertThat(response.getBody()).isNotBlank();
assertThat(response.getStatusCode()).isEqualTo(400);
assertThat(response.getHeaders()).isEmpty();
assertThat(response.getMultiValueHeaders()).isEmpty();
APIGatewayV2HTTPEvent event = new APIGatewayV2HTTPEvent();
event.setBody("{" +
" \"id\": 1," +
" \"name\": \"Lampshade\"" +
"}");
event.setHeaders(Map.of("header1", "value1"));
APIGatewayV2HTTPResponse response = handler.handleRequest(event, context);
assertThat(response.getBody()).isNotBlank();
assertThat(response.getStatusCode()).isEqualTo(400);
assertThat(response.getHeaders()).isEmpty();
assertThat(response.getMultiValueHeaders()).isEmpty();

}

@Test
Expand Down