Skip to content

Commit 864f1c5

Browse files
authored
Fix problem with validating partial bodies instead of whole body (#7)
1 parent 48e39d5 commit 864f1c5

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/ValidationReportHandler.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ private OpenApiViolation buildOpenApiViolation(
8383
private boolean isViolationExcluded(OpenApiViolation openApiViolation) {
8484
return
8585
violationExclusions.isExcluded(openApiViolation)
86-
// JSON parse errors should be ignored as it can't be compared to the schema then (this also prevents logging personal data!)
87-
|| openApiViolation.getMessage().startsWith("Unable to parse JSON")
8886
// If it matches more than 1, then we don't want to log a validation error
8987
|| openApiViolation.getMessage().matches(
9088
".*\\[Path '[^']+'] Instance failed to match exactly one schema \\(matched [1-9][0-9]* out of \\d\\).*");

spring-boot-starter/spring-boot-starter-webflux-spring2.7/src/main/java/com/getyourguide/openapi/validation/filter/decorator/BodyCachingServerHttpRequestDecorator.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
1111
import org.springframework.lang.NonNull;
1212
import reactor.core.publisher.Flux;
13+
import reactor.core.publisher.SignalType;
1314

1415
public class BodyCachingServerHttpRequestDecorator extends ServerHttpRequestDecorator {
1516
private final TrafficSelector trafficSelector;
@@ -38,11 +39,17 @@ public Flux<DataBuffer> getBody() {
3839
return super.getBody();
3940
}
4041

41-
return super.getBody().doOnNext(dataBuffer -> {
42-
cachedBody = dataBuffer.toString(StandardCharsets.UTF_8);
43-
if (onBodyCachedListener != null) {
44-
onBodyCachedListener.run();
45-
}
46-
});
42+
return super.getBody()
43+
.doOnNext(dataBuffer -> {
44+
if (cachedBody == null) {
45+
cachedBody = "";
46+
}
47+
cachedBody += dataBuffer.toString(StandardCharsets.UTF_8);
48+
})
49+
.doFinally(signalType -> {
50+
if (signalType == SignalType.ON_COMPLETE && onBodyCachedListener != null) {
51+
onBodyCachedListener.run();
52+
}
53+
});
4754
}
4855
}

spring-boot-starter/spring-boot-starter-webflux/src/main/java/com/getyourguide/openapi/validation/filter/decorator/BodyCachingServerHttpRequestDecorator.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
1111
import org.springframework.lang.NonNull;
1212
import reactor.core.publisher.Flux;
13+
import reactor.core.publisher.SignalType;
1314

1415
public class BodyCachingServerHttpRequestDecorator extends ServerHttpRequestDecorator {
1516
private final TrafficSelector trafficSelector;
@@ -38,11 +39,17 @@ public Flux<DataBuffer> getBody() {
3839
return super.getBody();
3940
}
4041

41-
return super.getBody().doOnNext(dataBuffer -> {
42-
cachedBody = dataBuffer.toString(StandardCharsets.UTF_8);
43-
if (onBodyCachedListener != null) {
44-
onBodyCachedListener.run();
45-
}
46-
});
42+
return super.getBody()
43+
.doOnNext(dataBuffer -> {
44+
if (cachedBody == null) {
45+
cachedBody = "";
46+
}
47+
cachedBody += dataBuffer.toString(StandardCharsets.UTF_8);
48+
})
49+
.doFinally(signalType -> {
50+
if (signalType == SignalType.ON_COMPLETE && onBodyCachedListener != null) {
51+
onBodyCachedListener.run();
52+
}
53+
});
4754
}
4855
}

0 commit comments

Comments
 (0)