Skip to content

bugfix: possible NullPointerException #71

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 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -30,7 +30,6 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
var requestMetaData = metaDataFactory.buildRequestMetaData(request);
request.setAttribute(ATTRIBUTE_REQUEST_META_DATA, requestMetaData);
if (!validator.isReady() || !trafficSelector.shouldRequestBeValidated(requestMetaData)) {
request.setAttribute(ATTRIBUTE_SKIP_VALIDATION, true);
request.setAttribute(ATTRIBUTE_SKIP_VALIDATION, true);
filterChain.doFilter(request, response);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void afterCompletion(
}

private boolean shouldSkipValidation(HttpServletRequest request) {
return request.getAttribute(OpenApiValidationFilter.ATTRIBUTE_SKIP_VALIDATION) != null;
return request == null || request.getAttribute(OpenApiValidationFilter.ATTRIBUTE_SKIP_VALIDATION) != null;
}

private void validateResponse(HttpServletRequest request, HttpServletResponse response) {
Expand All @@ -106,11 +106,10 @@ private void validateResponse(
) {
var requestMetaData = getRequestMetaData(request);
var responseMetaData = metaDataFactory.buildResponseMetaData(response, exception);
var requestToUse = contentCachingWrapperFactory.getCachingRequest(request);
var responseToUse = contentCachingWrapperFactory.getCachingResponse(response);
if (responseToUse != null) {
var violations = validateResponse(
requestToUse,
request,
responseToUse,
requestMetaData,
responseMetaData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ public void whenTestOptionsCallThenShouldNotValidate() throws Exception {
assertEquals(0, openApiViolationLogger.getViolations().size());
}

@Test
public void whenTestNoBodyThenShouldReturnSuccessAndNoViolation() throws Exception {
mockMvc.perform(post("/test/no-body"))
.andExpectAll(
status().isNoContent(),
content().string(Matchers.blankOrNullString())
);
Thread.sleep(100);

assertEquals(0, openApiViolationLogger.getViolations().size());
}

@Nullable
private OpenApiViolation getViolationByRule(List<OpenApiViolation> violations, String rule) {
return violations.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public ResponseEntity<TestResponse> postTest(PostTestRequest postTestRequest) {
}
return ResponseEntity.noContent().build();
}

@Override
public ResponseEntity<TestResponse> postTestNoBody() {
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ public void whenTestOptionsCallThenShouldNotValidate() throws Exception {
assertEquals(0, openApiViolationLogger.getViolations().size());
}

@Test
public void whenTestNoBodyThenShouldReturnSuccessAndNoViolation() throws Exception {
webTestClient
.post().uri("/test/no-body")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isNoContent()
.expectBody().isEmpty();
Thread.sleep(100);

assertEquals(0, openApiViolationLogger.getViolations().size());
}

@Nullable
private OpenApiViolation getViolationByRule(List<OpenApiViolation> violations, String rule) {
return violations.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public Mono<ResponseEntity<TestResponse>> postTest(
return Mono.just(ResponseEntity.noContent().build());
});
}

@Override
public Mono<ResponseEntity<TestResponse>> postTestNoBody(ServerWebExchange exchange) {
return Mono.just(ResponseEntity.noContent().build());
}
}
13 changes: 13 additions & 0 deletions test/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ paths:
"$ref": "#/components/schemas/TestResponse"
'204':
description: Successful response without content
"/test/no-body":
post:
description: Post test without request/response body
operationId: postTestNoBody
responses:
'200':
description: Successful response
content:
application/json:
schema:
"$ref": "#/components/schemas/TestResponse"
'204':
description: Successful response without content
components:
schemas:
TestResponse:
Expand Down