From 41e97e6d2bd817c39e0ce6593acea8b9b290ddd6 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 12 May 2020 18:57:25 -0700 Subject: [PATCH 1/3] Mustache template should use invokerPackage tag to generate import --- .../Java/libraries/jersey2/AbstractOpenApiSchema.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/AbstractOpenApiSchema.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/AbstractOpenApiSchema.mustache index cdba3be84552..c924650e6c0a 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/AbstractOpenApiSchema.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/AbstractOpenApiSchema.mustache @@ -2,7 +2,7 @@ package {{invokerPackage}}.model; -import org.openapitools.client.ApiException; +import {{invokerPackage}}.ApiException; import java.lang.reflect.Type; import java.util.Map; import javax.ws.rs.core.GenericType; From b572c3729542e9f5b24cb804cc80d8af4a4eb740 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Thu, 21 May 2020 21:31:47 -0700 Subject: [PATCH 2/3] Add new constructor for Java ApiClient --- .../Java/libraries/jersey2/ApiClient.mustache | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 41db79fc8e56..a0872eec27aa 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -152,7 +152,19 @@ public class ApiClient { protected DateFormat dateFormat; + /** + * Constructs a new ApiClient with default parameters. + */ public ApiClient() { + this(null); + } + + /** + * Constructs a new ApiClient with the specified authentication parameters. + * + * @param auth A hash map containing authentication parameters. + */ + public ApiClient(Map authMap) { json = new JSON(); httpClient = buildHttpClient(debugging); @@ -164,22 +176,46 @@ public class ApiClient { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); {{#authMethods}} + Authentication auth; + if (authMap != null) { + auth = authMap.get("{{name}}"); + } {{#isBasic}} {{#isBasicBasic}} - authentications.put("{{name}}", new HttpBasicAuth()); + if (auth instanceof HttpBasicAuth) { + authentications.put("{{name}}", auth)); + } else { + authentications.put("{{name}}", new HttpBasicAuth()); + } {{/isBasicBasic}} {{#isBasicBearer}} - authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}")); + if (auth instanceof HttpBearerAuth) { + authentications.put("{{name}}", auth); + } else { + authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}")); + } {{/isBasicBearer}} {{#isHttpSignature}} - authentications.put("{{name}}", new HttpSignatureAuth("{{name}}", null, null)); + if (auth instanceof HttpSignatureAuth) { + authentications.put("{{name}}", auth); + } else { + authentications.put("{{name}}", null); + } {{/isHttpSignature}} {{/isBasic}} {{#isApiKey}} - authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}")); + if (auth instanceof ApiKeyAuth) { + authentications.put("{{name}}", auth)); + } else { + authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}")); + } {{/isApiKey}} {{#isOAuth}} - authentications.put("{{name}}", new OAuth(basePath, "{{tokenUrl}}")); + if (auth instanceof OAuth) { + authentications.put("{{name}}", auth); + } else { + authentications.put("{{name}}", new OAuth(basePath, "{{tokenUrl}}")); + } {{/isOAuth}} {{/authMethods}} // Prevent the authentications from being modified. From c325c111bdff87ccb829d683bda127223091b376 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Thu, 21 May 2020 21:56:01 -0700 Subject: [PATCH 3/3] Add constructor with auth map --- .../Java/libraries/jersey2/ApiClient.mustache | 8 +-- .../org/openapitools/client/ApiClient.java | 49 +++++++++++++++++-- .../org/openapitools/client/ApiClient.java | 49 +++++++++++++++++-- 3 files changed, 94 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index a0872eec27aa..66fe2735584a 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -162,7 +162,7 @@ public class ApiClient { /** * Constructs a new ApiClient with the specified authentication parameters. * - * @param auth A hash map containing authentication parameters. + * @param authMap A hash map containing authentication parameters. */ public ApiClient(Map authMap) { json = new JSON(); @@ -175,15 +175,15 @@ public class ApiClient { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); + Authentication auth = null; {{#authMethods}} - Authentication auth; if (authMap != null) { auth = authMap.get("{{name}}"); } {{#isBasic}} {{#isBasicBasic}} if (auth instanceof HttpBasicAuth) { - authentications.put("{{name}}", auth)); + authentications.put("{{name}}", auth); } else { authentications.put("{{name}}", new HttpBasicAuth()); } @@ -205,7 +205,7 @@ public class ApiClient { {{/isBasic}} {{#isApiKey}} if (auth instanceof ApiKeyAuth) { - authentications.put("{{name}}", auth)); + authentications.put("{{name}}", auth); } else { authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}")); } diff --git a/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/ApiClient.java index 36afb9066fb2..d2137b8d0882 100644 --- a/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/ApiClient.java @@ -87,7 +87,19 @@ public class ApiClient { protected DateFormat dateFormat; + /** + * Constructs a new ApiClient with default parameters. + */ public ApiClient() { + this(null); + } + + /** + * Constructs a new ApiClient with the specified authentication parameters. + * + * @param authMap A hash map containing authentication parameters. + */ + public ApiClient(Map authMap) { json = new JSON(); httpClient = buildHttpClient(debugging); @@ -98,10 +110,39 @@ public ApiClient() { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); - authentications.put("api_key", new ApiKeyAuth("header", "api_key")); - authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query")); - authentications.put("http_basic_test", new HttpBasicAuth()); - authentications.put("petstore_auth", new OAuth(basePath, "")); + Authentication auth = null; + if (authMap != null) { + auth = authMap.get("api_key"); + } + if (auth instanceof ApiKeyAuth) { + authentications.put("api_key", auth); + } else { + authentications.put("api_key", new ApiKeyAuth("header", "api_key")); + } + if (authMap != null) { + auth = authMap.get("api_key_query"); + } + if (auth instanceof ApiKeyAuth) { + authentications.put("api_key_query", auth); + } else { + authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query")); + } + if (authMap != null) { + auth = authMap.get("http_basic_test"); + } + if (auth instanceof HttpBasicAuth) { + authentications.put("http_basic_test", auth); + } else { + authentications.put("http_basic_test", new HttpBasicAuth()); + } + if (authMap != null) { + auth = authMap.get("petstore_auth"); + } + if (auth instanceof OAuth) { + authentications.put("petstore_auth", auth); + } else { + authentications.put("petstore_auth", new OAuth(basePath, "")); + } // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index 36afb9066fb2..d2137b8d0882 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -87,7 +87,19 @@ public class ApiClient { protected DateFormat dateFormat; + /** + * Constructs a new ApiClient with default parameters. + */ public ApiClient() { + this(null); + } + + /** + * Constructs a new ApiClient with the specified authentication parameters. + * + * @param authMap A hash map containing authentication parameters. + */ + public ApiClient(Map authMap) { json = new JSON(); httpClient = buildHttpClient(debugging); @@ -98,10 +110,39 @@ public ApiClient() { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); - authentications.put("api_key", new ApiKeyAuth("header", "api_key")); - authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query")); - authentications.put("http_basic_test", new HttpBasicAuth()); - authentications.put("petstore_auth", new OAuth(basePath, "")); + Authentication auth = null; + if (authMap != null) { + auth = authMap.get("api_key"); + } + if (auth instanceof ApiKeyAuth) { + authentications.put("api_key", auth); + } else { + authentications.put("api_key", new ApiKeyAuth("header", "api_key")); + } + if (authMap != null) { + auth = authMap.get("api_key_query"); + } + if (auth instanceof ApiKeyAuth) { + authentications.put("api_key_query", auth); + } else { + authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query")); + } + if (authMap != null) { + auth = authMap.get("http_basic_test"); + } + if (auth instanceof HttpBasicAuth) { + authentications.put("http_basic_test", auth); + } else { + authentications.put("http_basic_test", new HttpBasicAuth()); + } + if (authMap != null) { + auth = authMap.get("petstore_auth"); + } + if (auth instanceof OAuth) { + authentications.put("petstore_auth", auth); + } else { + authentications.put("petstore_auth", new OAuth(basePath, "")); + } // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications);