From 2540db8adf8dc6769abc8e4acd69dd312af0eec0 Mon Sep 17 00:00:00 2001 From: Eric O'Connell Date: Mon, 24 Jul 2017 23:56:34 -0700 Subject: [PATCH] More flexible subclassing of ApiClient possible by s/private/protected/g (#6159) * More flexible subclassing of ApiClient possible by s/private/protected/g I found myself in a situation where I needed to change the configured `Feature`s, but because of all the private variables I was not able to do so. Perhaps this is a bit too broad of a stroke, but I changed all fields and methods to `protected` instead of `private`. In this way, future extensibility should be improved. Also, to solve my particular problem, I added a new empty method called `performAdditionalClientConfiguration` which will allow subclasses to add specific features, or do anything else possible with a `ClientConfig`. * Updated samples --- .../Java/libraries/jersey2/ApiClient.mustache | 33 +++++++++++-------- .../java/io/swagger/client/ApiClient.java | 33 +++++++++++-------- .../java/io/swagger/client/ApiClient.java | 33 +++++++++++-------- 3 files changed, 57 insertions(+), 42 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 9399095a4a0a..0d707c5b398e 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -56,21 +56,21 @@ import {{invokerPackage}}.auth.OAuth; {{>generatedAnnotation}} public class ApiClient { - private Map defaultHeaderMap = new HashMap(); - private String basePath = "{{{basePath}}}"; - private boolean debugging = false; - private int connectionTimeout = 0; + protected Map defaultHeaderMap = new HashMap(); + protected String basePath = "{{{basePath}}}"; + protected boolean debugging = false; + protected int connectionTimeout = 0; - private Client httpClient; - private JSON json; - private String tempFolderPath = null; + protected Client httpClient; + protected JSON json; + protected String tempFolderPath = null; - private Map authentications; + protected Map authentications; - private int statusCode; - private Map> responseHeaders; + protected int statusCode; + protected Map> responseHeaders; - private DateFormat dateFormat; + protected DateFormat dateFormat; public ApiClient() { json = new JSON(); @@ -740,7 +740,7 @@ public class ApiClient { * @param debugging Debug setting * @return Client */ - private Client buildHttpClient(boolean debugging) { + protected Client buildHttpClient(boolean debugging) { final ClientConfig clientConfig = new ClientConfig(); clientConfig.register(MultiPartFeature.class); clientConfig.register(json); @@ -751,10 +751,15 @@ public class ApiClient { // Set logger to ALL java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL); } + performAdditionalClientConfiguration(clientConfig); return ClientBuilder.newClient(clientConfig); } - private Map> buildResponseHeaders(Response response) { + protected void performAdditionalClientConfiguration(ClientConfig clientConfig) { + // No-op extension point + } + + protected Map> buildResponseHeaders(Response response) { Map> responseHeaders = new HashMap>(); for (Entry> entry: response.getHeaders().entrySet()) { List values = entry.getValue(); @@ -772,7 +777,7 @@ public class ApiClient { * * @param authNames The authentications to apply */ - private void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { + protected void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); diff --git a/samples/client/petstore/java/jersey2-java6/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/jersey2-java6/src/main/java/io/swagger/client/ApiClient.java index d763b7ab6b37..42f3a9c70294 100644 --- a/samples/client/petstore/java/jersey2-java6/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java6/src/main/java/io/swagger/client/ApiClient.java @@ -50,21 +50,21 @@ import io.swagger.client.auth.OAuth; public class ApiClient { - private Map defaultHeaderMap = new HashMap(); - private String basePath = "http://petstore.swagger.io:80/v2"; - private boolean debugging = false; - private int connectionTimeout = 0; + protected Map defaultHeaderMap = new HashMap(); + protected String basePath = "http://petstore.swagger.io:80/v2"; + protected boolean debugging = false; + protected int connectionTimeout = 0; - private Client httpClient; - private JSON json; - private String tempFolderPath = null; + protected Client httpClient; + protected JSON json; + protected String tempFolderPath = null; - private Map authentications; + protected Map authentications; - private int statusCode; - private Map> responseHeaders; + protected int statusCode; + protected Map> responseHeaders; - private DateFormat dateFormat; + protected DateFormat dateFormat; public ApiClient() { json = new JSON(); @@ -729,7 +729,7 @@ public class ApiClient { * @param debugging Debug setting * @return Client */ - private Client buildHttpClient(boolean debugging) { + protected Client buildHttpClient(boolean debugging) { final ClientConfig clientConfig = new ClientConfig(); clientConfig.register(MultiPartFeature.class); clientConfig.register(json); @@ -740,10 +740,15 @@ public class ApiClient { // Set logger to ALL java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL); } + performAdditionalClientConfiguration(clientConfig); return ClientBuilder.newClient(clientConfig); } - private Map> buildResponseHeaders(Response response) { + protected void performAdditionalClientConfiguration(ClientConfig clientConfig) { + // No-op extension point + } + + protected Map> buildResponseHeaders(Response response) { Map> responseHeaders = new HashMap>(); for (Entry> entry: response.getHeaders().entrySet()) { List values = entry.getValue(); @@ -761,7 +766,7 @@ public class ApiClient { * * @param authNames The authentications to apply */ - private void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { + protected void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); diff --git a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/ApiClient.java index 4a2b2c4647fd..63d5f5c226d8 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/ApiClient.java @@ -51,21 +51,21 @@ import io.swagger.client.auth.OAuth; public class ApiClient { - private Map defaultHeaderMap = new HashMap(); - private String basePath = "http://petstore.swagger.io:80/v2"; - private boolean debugging = false; - private int connectionTimeout = 0; + protected Map defaultHeaderMap = new HashMap(); + protected String basePath = "http://petstore.swagger.io:80/v2"; + protected boolean debugging = false; + protected int connectionTimeout = 0; - private Client httpClient; - private JSON json; - private String tempFolderPath = null; + protected Client httpClient; + protected JSON json; + protected String tempFolderPath = null; - private Map authentications; + protected Map authentications; - private int statusCode; - private Map> responseHeaders; + protected int statusCode; + protected Map> responseHeaders; - private DateFormat dateFormat; + protected DateFormat dateFormat; public ApiClient() { json = new JSON(); @@ -729,7 +729,7 @@ public class ApiClient { * @param debugging Debug setting * @return Client */ - private Client buildHttpClient(boolean debugging) { + protected Client buildHttpClient(boolean debugging) { final ClientConfig clientConfig = new ClientConfig(); clientConfig.register(MultiPartFeature.class); clientConfig.register(json); @@ -740,10 +740,15 @@ public class ApiClient { // Set logger to ALL java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL); } + performAdditionalClientConfiguration(clientConfig); return ClientBuilder.newClient(clientConfig); } - private Map> buildResponseHeaders(Response response) { + protected void performAdditionalClientConfiguration(ClientConfig clientConfig) { + // No-op extension point + } + + protected Map> buildResponseHeaders(Response response) { Map> responseHeaders = new HashMap>(); for (Entry> entry: response.getHeaders().entrySet()) { List values = entry.getValue(); @@ -761,7 +766,7 @@ public class ApiClient { * * @param authNames The authentications to apply */ - private void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { + protected void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);