Skip to content

[BUG][Java][spring][webclient] webclient metrics contains actual query parameters, resulting in OOM #7899

Closed
@cdmatta

Description

@cdmatta

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Generate the spring webclient implementation for an open api endpoint which is using query parameters.
Use the generated ApiClient to make calls to the endpoint with different query parameters in a spring webflux application (with micrometer metrics).

Check the uri tag for the metric http_client_requests.
This uri tag contains the query parameter. This will continue to grow as different values are used.
Eventually, leading to an OutOfMemory error.

openapi-generator version

openapi-generator version: 4.3.1

This is not a regression.

OpenAPI declaration file content or url

This can be reproduced with the sample petstore fake endpoints yaml file in the project

/fake/body-with-query-params

Steps to reproduce
  1. Generate the spring webclient implementation for the yaml
  2. Use it in a Spring boot project with actuator and metrics
  3. Call the endpoint with query parameters

Actual

  • Query parameter values are present in the http_client_requests metric
    Screenshot from 2020-11-08 13-17-35

Expected

  • Query parameters values must not be present in the metrics.
Suggest a fix

Solution for this is available in spring framework 5.2 webclient

I could provide a PR, but that means the minimum required version of spring framework will be 5.2.
Or, we need to make rules in the template to add the following code on spring 5.2+ only, hence leaving it for you to decide.

Here is the fix to the ApiClient.mustache template required to fix this. webclient-ApiClient.patch.txt

diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache
index 98f4bfb2..080b6fbd 100644
--- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache
+++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache
@@ -564,11 +564,13 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
         updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
 
         final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
-        if (queryParams != null) {
-            builder.queryParams(queryParams);
-        }
 
-        final WebClient.RequestBodySpec requestBuilder = webClient.method(method).uri(builder.build(false).toUriString(), pathParams);
+        final WebClient.RequestBodySpec requestBuilder = webClient.method(method).uri(builder.build(false).toUriString(), uriBuilder -> {
+            if (queryParams == null) {
+                return uriBuilder.build(pathParams);
+            }
+            return uriBuilder.queryParams(queryParams).build(pathParams);
+        });
         if(accept != null) {
             requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
         }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions