Skip to content

[REQ] Use global Configuration.getDefaultApiClient() in no-arg ctor for native Api classes and make Configuration.defaultApiClient volatile #20633

@ronreynolds

Description

@ronreynolds

Is your feature request related to a problem? Please describe.

currently *Api classes for native library code creates a new ApiClient every time; this is inefficient and makes the ctor useless if one is using the global ApiClient object to hold auth headers and other customizations. there is no reason (i can think of) to not reuse the Configuration.getDefaultApiClient() as is used in most other java libraries' generated code.

also the ApiClient ref in Configuration is not volatile and thus changes in one thread may never be reflected to others.

e.g.,

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.11.0")
public class ServerInfoApi {
  ...
  public ServerInfoApi() {
    this(new ApiClient());
  }

and

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.11.0")
public class Configuration {
...
    private static ApiClient defaultApiClient = new ApiClient();

Describe the solution you'd like

reuse of the global Configuration.defaultApiClient in no-arg Api ctors and make the ref volatile so if/when it's changed the change is reflected instantly to other threads using that same ref. the cost of reading a volatile (especially one that changes very very rarely; i.e., cache-miss odds are nearly 0) is roughly equal to reading any other reference.
e.g.,

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.11.0")
public class ServerInfoApi {
  ...
  public ServerInfoApi() {
    this(Configuration.getDefaultApiClient());
  }

and

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.11.0")
public class Configuration {
...
    private static volatile ApiClient defaultApiClient = new ApiClient();

Describe alternatives you've considered

the alternative would be to leave it as-is which means that when writes to Configuration.defaultApiClient are visible to other threads is indeterminate (never is even possible) and every time someone creates a new Api class they either create a new vanilla ApiClient object OR they don't use the default ctor and instead call new ServerInfoApi(Configuration.getDefaultApiClient()) every single time.

Additional context

i've already implemented the changes on my own fork - https://github.com/ronreynolds/openapi-generator/tree/master

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