From 9bce846fea008f4597c3a5754318fe17fd1a2b61 Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Fri, 10 Jan 2020 13:51:34 +0100 Subject: [PATCH 1/4] [java] Support aliasing of API keys --- .../Java/libraries/jersey2/ApiClient.mustache | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 b12740b0f649..c56dc4fbce06 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 @@ -73,6 +73,7 @@ public class ApiClient { protected String tempFolderPath = null; protected Map authentications; + protected Map authenticationLookup; protected DateFormat dateFormat; @@ -93,6 +94,10 @@ public class ApiClient { authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); + + // Setup authentication lookup (key: authentication alias, value: authentication name) + authenticationLookup = new HashMap();{{#authMethods}}{{#vendorExtensions.x-lookup}} + authenticationLookup.put("{{name}}", "{{.}}");{{/vendorExtensions.x-lookup}}{{/authMethods}} } /** @@ -181,6 +186,28 @@ public class ApiClient { throw new RuntimeException("No API key authentication configured!"); } + /** + * Helper method to configure authentications. + * + * NOTE: This method respects API key aliases using "x-lookup" property + * from OpenAPI specification. + * + * @param secrets Hash map from authentication name to its secret. + */ + public void configureApiKeys(HashMap secrets) { + for (Map.Entry authEntry : authentications.entrySet()) { + Authentication auth = authEntry.getValue(); + if (auth instanceof ApiKeyAuth) { + String name = authEntry.getKey(); + // respect x-lookup property + name = authenticationLookup.getOrDefault(name, name); + if (secrets.containsKey(name)) { + ((ApiKeyAuth) auth).setApiKey(secrets.get(name)); + } + } + } + } + /** * Helper method to set API key prefix for the first API key authentication. * @param apiKeyPrefix API key prefix From 01b7d898bd7158cc2ab829d42c18fa2d579a426d Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Fri, 10 Jan 2020 14:00:35 +0100 Subject: [PATCH 2/4] Rebuild Java Jersey2 sample client --- .../org/openapitools/client/ApiClient.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java index bd0facace110..b7b14b3b41c3 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java @@ -65,6 +65,7 @@ public class ApiClient { protected String tempFolderPath = null; protected Map authentications; + protected Map authenticationLookup; protected DateFormat dateFormat; @@ -85,6 +86,9 @@ public ApiClient() { authentications.put("petstore_auth", new OAuth()); // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); + + // Setup authentication lookup (key: authentication alias, value: authentication name) + authenticationLookup = new HashMap(); } /** @@ -173,6 +177,28 @@ public void setApiKey(String apiKey) { throw new RuntimeException("No API key authentication configured!"); } + /** + * Helper method to configure authentications. + * + * NOTE: This method respects API key aliases using "x-lookup" property + * from OpenAPI specification. + * + * @param secrets Hash map from authentication name to its secret. + */ + public void configureApiKeys(HashMap secrets) { + for (Map.Entry authEntry : authentications.entrySet()) { + Authentication auth = authEntry.getValue(); + if (auth instanceof ApiKeyAuth) { + String name = authEntry.getKey(); + // respect x-lookup property + name = authenticationLookup.getOrDefault(name, name); + if (secrets.containsKey(name)) { + ((ApiKeyAuth) auth).setApiKey(secrets.get(name)); + } + } + } + } + /** * Helper method to set API key prefix for the first API key authentication. * @param apiKeyPrefix API key prefix From 7dcd4f67f1a1fd6004cbaafd0cf24ed7a64743c9 Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Wed, 19 Feb 2020 10:16:21 +0100 Subject: [PATCH 3/4] x-lookup to x-auth-id-alias --- .../Java/libraries/jersey2/ApiClient.mustache | 11 ++++------- .../main/java/org/openapitools/client/ApiClient.java | 4 ++-- 2 files changed, 6 insertions(+), 9 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 c56dc4fbce06..83f9655e22f0 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 @@ -96,8 +96,8 @@ public class ApiClient { authentications = Collections.unmodifiableMap(authentications); // Setup authentication lookup (key: authentication alias, value: authentication name) - authenticationLookup = new HashMap();{{#authMethods}}{{#vendorExtensions.x-lookup}} - authenticationLookup.put("{{name}}", "{{.}}");{{/vendorExtensions.x-lookup}}{{/authMethods}} + authenticationLookup = new HashMap();{{#authMethods}}{{#vendorExtensions.x-auth-id-alias}} + authenticationLookup.put("{{name}}", "{{.}}");{{/vendorExtensions.x-auth-id-alias}}{{/authMethods}} } /** @@ -187,10 +187,7 @@ public class ApiClient { } /** - * Helper method to configure authentications. - * - * NOTE: This method respects API key aliases using "x-lookup" property - * from OpenAPI specification. + * Helper method to configure authentications which respects aliases of API keys. * * @param secrets Hash map from authentication name to its secret. */ @@ -199,7 +196,7 @@ public class ApiClient { Authentication auth = authEntry.getValue(); if (auth instanceof ApiKeyAuth) { String name = authEntry.getKey(); - // respect x-lookup property + // respect x-auth-id-alias property name = authenticationLookup.getOrDefault(name, name); if (secrets.containsKey(name)) { ((ApiKeyAuth) auth).setApiKey(secrets.get(name)); diff --git a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java index b7b14b3b41c3..51781b94a728 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java @@ -180,7 +180,7 @@ public void setApiKey(String apiKey) { /** * Helper method to configure authentications. * - * NOTE: This method respects API key aliases using "x-lookup" property + * NOTE: This method respects API key aliases using "x-auth-id-alias" property * from OpenAPI specification. * * @param secrets Hash map from authentication name to its secret. @@ -190,7 +190,7 @@ public void configureApiKeys(HashMap secrets) { Authentication auth = authEntry.getValue(); if (auth instanceof ApiKeyAuth) { String name = authEntry.getKey(); - // respect x-lookup property + // respect x-auth-id-alias property name = authenticationLookup.getOrDefault(name, name); if (secrets.containsKey(name)) { ((ApiKeyAuth) auth).setApiKey(secrets.get(name)); From 1f31423b941d2a6f807ac56753b0d83daa94ca10 Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Wed, 19 Feb 2020 10:22:44 +0100 Subject: [PATCH 4/4] Regenerated --- .../org/openapitools/client/ApiClient.java | 23 +++++++++++++++++++ .../org/openapitools/client/ApiClient.java | 23 +++++++++++++++++++ .../org/openapitools/client/ApiClient.java | 5 +--- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/ApiClient.java index 35d6ae5c9af9..b3bd35faa449 100644 --- a/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java6/src/main/java/org/openapitools/client/ApiClient.java @@ -64,6 +64,7 @@ public class ApiClient { protected String tempFolderPath = null; protected Map authentications; + protected Map authenticationLookup; protected DateFormat dateFormat; @@ -84,6 +85,9 @@ public ApiClient() { authentications.put("petstore_auth", new OAuth()); // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); + + // Setup authentication lookup (key: authentication alias, value: authentication name) + authenticationLookup = new HashMap(); } /** @@ -172,6 +176,25 @@ public void setApiKey(String apiKey) { throw new RuntimeException("No API key authentication configured!"); } + /** + * Helper method to configure authentications which respects aliases of API keys. + * + * @param secrets Hash map from authentication name to its secret. + */ + public void configureApiKeys(HashMap secrets) { + for (Map.Entry authEntry : authentications.entrySet()) { + Authentication auth = authEntry.getValue(); + if (auth instanceof ApiKeyAuth) { + String name = authEntry.getKey(); + // respect x-auth-id-alias property + name = authenticationLookup.getOrDefault(name, name); + if (secrets.containsKey(name)) { + ((ApiKeyAuth) auth).setApiKey(secrets.get(name)); + } + } + } + } + /** * Helper method to set API key prefix for the first API key authentication. * @param apiKeyPrefix API key prefix 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 dcc41e43f5d0..e75d20c92635 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 @@ -65,6 +65,7 @@ public class ApiClient { protected String tempFolderPath = null; protected Map authentications; + protected Map authenticationLookup; protected DateFormat dateFormat; @@ -85,6 +86,9 @@ public ApiClient() { authentications.put("petstore_auth", new OAuth()); // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); + + // Setup authentication lookup (key: authentication alias, value: authentication name) + authenticationLookup = new HashMap(); } /** @@ -173,6 +177,25 @@ public void setApiKey(String apiKey) { throw new RuntimeException("No API key authentication configured!"); } + /** + * Helper method to configure authentications which respects aliases of API keys. + * + * @param secrets Hash map from authentication name to its secret. + */ + public void configureApiKeys(HashMap secrets) { + for (Map.Entry authEntry : authentications.entrySet()) { + Authentication auth = authEntry.getValue(); + if (auth instanceof ApiKeyAuth) { + String name = authEntry.getKey(); + // respect x-auth-id-alias property + name = authenticationLookup.getOrDefault(name, name); + if (secrets.containsKey(name)) { + ((ApiKeyAuth) auth).setApiKey(secrets.get(name)); + } + } + } + } + /** * Helper method to set API key prefix for the first API key authentication. * @param apiKeyPrefix API key prefix diff --git a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java index 04d014af32c7..e75d20c92635 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2/src/main/java/org/openapitools/client/ApiClient.java @@ -178,10 +178,7 @@ public void setApiKey(String apiKey) { } /** - * Helper method to configure authentications. - * - * NOTE: This method respects API key aliases using "x-auth-id-alias" property - * from OpenAPI specification. + * Helper method to configure authentications which respects aliases of API keys. * * @param secrets Hash map from authentication name to its secret. */