Repository navigation
Description
The WebFlux BodyCachingServerHttpResponseDecorator converts the response body publisher into a Mono before forwarding it to Spring:
var buffer = Mono.from(body);
return super.writeWith(buffer);writeWith accepts a Publisher<DataBuffer>, which may emit multiple buffers for one HTTP response. Converting that publisher to a Mono retains only one buffer. As a result, a supported response that is emitted in multiple buffers can be truncated before it reaches the client. Validation also receives only the first buffer rather than the complete response body.
For example, a single response may be emitted as:
Buffer 1: {"name":"GetYour
Buffer 2: Guide"}
The expected client response is:
{"name":"GetYourGuide"}With the current implementation, only the first buffer is forwarded:
{"name":"GetYourImpact
This affects the WebFlux starter when validation is active and a JSON, XML, or HTML response is produced using more than one DataBuffer. Clients can receive an incomplete response, and OpenAPI validation can report incorrect violations because it sees an incomplete body.
Proposed fix
Join all response buffers before caching and forwarding the response:
var buffer = DataBufferUtils.join(body).doOnNext(dataBuffer -> {
cachedBody = dataBuffer.toString(StandardCharsets.UTF_8);
if (onBodyCachedListener != null) {
onBodyCachedListener.run();
}
});
return super.writeWith(buffer);This preserves the existing design of caching complete supported response bodies for validation. The servlet implementation already uses full-content caching, and the README limits validation to JSON, XML, and HTML to avoid caching content such as videos and streams.
Regression test
A new test sends a response as two DataBuffer values:
Flux.just(
delegate.bufferFactory().wrap("{\"name\":\"GetYour".getBytes(StandardCharsets.UTF_8)),
delegate.bufferFactory().wrap("Guide\"}".getBytes(StandardCharsets.UTF_8))
)It verifies both that:
- the client receives
{"name":"GetYourGuide"}; - the cached body used for validation is
{"name":"GetYourGuide"}.
The regression test fails with the current Mono.from(body) implementation and passes once all buffers are joined.
Validation
Run the focused regression test:
.\gradlew.bat :spring-boot-starter:spring-boot-starter-webflux:test --tests "com.getyourguide.openapi.validation.filter.decorator.BodyCachingServerHttpResponseDecoratorTest"Then run the complete WebFlux module test suite:
.\gradlew.bat :spring-boot-starter:spring-boot-starter-webflux:testTerminal output:
Build run before changes:
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat clean test
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :examples:example-spring-boot-starter-web:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-web/src/main/resources/openapi.yaml
Spec is valid.
> Task :examples:example-spring-boot-starter-web:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator. #
# We appreciate your support! Please consider donation to help us maintain this project. #
# https://opencollective.com/openapi_generator/donate #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-web\build/generated
> Task :examples:example-spring-boot-starter-web:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
> Task :examples:example-spring-boot-starter-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-webflux/src/main/resources/openapi.yaml
Spec is valid.
> Task :examples:example-spring-boot-starter-webflux:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator. #
# We appreciate your support! Please consider donation to help us maintain this project. #
# https://opencollective.com/openapi_generator/donate #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-webflux\build/generated
> Task :spring-boot-starter:spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
> Task :examples:example-spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :test:openapi-web:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-web/../openapi.yaml
Spec is valid.
> Task :test:openapi-web:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator. #
# We appreciate your support! Please consider donation to help us maintain this project. #
# https://opencollective.com/openapi_generator/donate #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-web\build/generated/openapi
> Task :test:openapi-web:compileTestFixturesJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :test:openapi-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux/../openapi.yaml
Spec is valid.
> Task :test:openapi-webflux:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator. #
# We appreciate your support! Please consider donation to help us maintain this project. #
# https://opencollective.com/openapi_generator/donate #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux\build/generated/openapi
> Task :test:openapi-webflux:compileTestFixturesJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html
Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 2m 8s
74 actionable tasks: 70 executed, 4 up-to-date
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.4.1/userguide/configuration_cache_enabling.html
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi>Test Run after adding a scneario where existing code will fail:
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat :spring-boot-starter:spring-boot-starter-webflux:test --tests "com.getyourguide.openapi.validation.filter.decorator.BodyCachingServerHttpResponseDecoratorTest"
> Task :test:openapi-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux/../openapi.yaml
Spec is valid.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :spring-boot-starter:spring-boot-starter-webflux:test
BodyCachingServerHttpResponseDecoratorTest > whenResponseContainsMultipleBuffersThenAllBuffersAreWritten() FAILED
org.opentest4j.AssertionFailedError at BodyCachingServerHttpResponseDecoratorTest.java:41
1 test completed, 1 failed
> Task :spring-boot-starter:spring-boot-starter-webflux:test FAILED
[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':spring-boot-starter:spring-boot-starter-webflux:test'.
> There were failing tests. See the report at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/spring-boot-starter/spring-boot-starter-webflux/build/reports/tests/test/index.html
* Try:
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 14s
20 actionable tasks: 3 executed, 17 up-to-date
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi>Test run after applying the fix:
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat :spring-boot-starter:spring-boot-starter-webflux:test --tests "com.getyourguide.openapi.validation.filter.decorator.BodyCachingServerHttpResponseDecoratorTest"
> Task :spring-boot-starter:spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
> Task :test:openapi-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux/../openapi.yaml
Spec is valid.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html
Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 17s
20 actionable tasks: 4 executed, 16 up-to-date
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.4.1/userguide/configuration_cache_enabling.html
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> Build run after adding the changes:
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat clean test
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :examples:example-spring-boot-starter-web:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-web/src/main/resources/openapi.yaml
Spec is valid.
> Task :examples:example-spring-boot-starter-web:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator. #
# We appreciate your support! Please consider donation to help us maintain this project. #
# https://opencollective.com/openapi_generator/donate #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-web\build/generated
> Task :examples:example-spring-boot-starter-web:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
> Task :examples:example-spring-boot-starter-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-webflux/src/main/resources/openapi.yaml
Spec is valid.
> Task :examples:example-spring-boot-starter-webflux:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator. #
# We appreciate your support! Please consider donation to help us maintain this project. #
# https://opencollective.com/openapi_generator/donate #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-webflux\build/generated
> Task :spring-boot-starter:spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
> Task :examples:example-spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :test:openapi-web:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-web/../openapi.yaml
Spec is valid.
> Task :test:openapi-web:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator. #
# We appreciate your support! Please consider donation to help us maintain this project. #
# https://opencollective.com/openapi_generator/donate #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-web\build/generated/openapi
> Task :test:openapi-web:compileTestFixturesJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :test:openapi-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux/../openapi.yaml
Spec is valid.
> Task :test:openapi-webflux:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator. #
# We appreciate your support! Please consider donation to help us maintain this project. #
# https://opencollective.com/openapi_generator/donate #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux\build/generated/openapi
> Task :test:openapi-webflux:compileTestFixturesJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html
Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 1m 18s
74 actionable tasks: 70 executed, 4 up-to-date
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.4.1/userguide/configuration_cache_enabling.html
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi>
Activity