-
Notifications
You must be signed in to change notification settings - Fork 100
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
jeromevdl
merged 21 commits into
aws-powertools:v2
from
skal111:feat/1298-validation-400
Dec 1, 2023
Merged
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f311722
#1298 first implem - api gateway events validation will be catched an…
ddbb2a1
#1298 updated tests in pt-examples-validation + sonar qaulity fixes
eda478d
#1298 code smell
4808b8f
1298 introduced E2E tests for validation
ecf056e
#1298 updated documentation to match new behavior of @Validation
3f64d55
#1298 implemented suggested changes
46f7f4c
#1298 added new E2E tests for ALB event, which should trigger a valid…
3a48656
#1298 original headers are returned as part of a 400 validation faile…
80ceb05
#1298 javadoc
5387e49
#1298 first implem - api gateway events validation will be catched an…
d72ee64
Rebased upstream
4ef6abf
1298 introduced E2E tests for validation
200cbd1
#1298 updated documentation to match new behavior of @Validation
22e069a
#1298 implemented suggested changes
a7d02c4
#1298 added new E2E tests for ALB event, which should trigger a valid…
dd13481
#1298 original headers are returned as part of a 400 validation faile…
a414353
#1298 javadoc
3e336c4
tests are compatible with java8
36318b2
fixed java8 compatibility
111d79a
indentation
jeromevdl c9ca742
Merge branch 'v2' into feat/1298-validation-400
jeromevdl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.