swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.04k stars 6.03k forks source link

[JAVA] ApiClient.setDebugging(boolean debug) fails to add LoggingInterceptors to HTTP client when its debugging field is already true #6497

Open jcasey23 opened 7 years ago

jcasey23 commented 7 years ago
Description

Here is the code for the ApiClient.setDebugging method:

public ApiClient setDebugging(boolean debugging) { if (debugging != this.debugging) { if (debugging) { loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(Level.BODY); httpClient.interceptors().add(loggingInterceptor); } else { httpClient.interceptors().remove(loggingInterceptor); loggingInterceptor = null; } } this.debugging = debugging; return this; }

Configuration.getDefaultClient() returns an ApiClient instance in which "debugging" is already set to true. When the "setDebugging" method is called, passing the boolean true, the method fails to add the LoggingInterceptors to the HTTP client and HTTP debugging is disabled.

Swagger-codegen version

I generated the Java client from the master branch, which I believe was 2.3.0 at the time. The same code for the setDebugging method is present in version 2.2.1, so this is not a regression (from that point at least).

Command line used for generation

java -jar swagger-codegen-cli-2.3.0.jar generate -i api.yaml -l java -o SwarmApi

YAML file

For copyright reasons I cannot include the api.yaml file I was using.

Steps to reproduce
  1. Generate the JAVA client jar for any api.yaml file.
  2. Using the generated code, get an instance of the default API client using Configuration.getDefaultClient().
  3. Call the "setDebugging" method on the client instance passing the boolean argument true.
  4. Using the client instance, exercise any test code that involves HTTP traffic between the client and a server.
  5. Note that the send-receive traffic is not included in the test output.
Related issues/PRs

Not that I could find.

Suggest a fix/enhancement

When the boolean "true" is passed to the setDebugging method, interrogate the HTTP client to determine whether LogginInterceptors have already been added or not. If not, then add them.

wing328 commented 7 years ago

@jcasey23 thanks for reporting the issue. Would you please submit a PR with the suggested fix so that we can review more easily?