From b609c0c44e9b3363a08361621ab3673b41573240 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Mon, 25 Nov 2019 07:42:26 -0800 Subject: [PATCH 01/20] Python: conditionally set auth attributes --- .../main/resources/python/api_client.mustache | 3 +- .../resources/python/configuration.mustache | 77 ++++++++++--------- .../python-experimental/api_client.mustache | 3 +- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/api_client.mustache b/modules/openapi-generator/src/main/resources/python/api_client.mustache index 3bf085e1904b..d57b51497746 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -516,7 +516,8 @@ class ApiClient(object): """ if not auth_settings: return - + if self.configuration.security_schemes: + auth_settings = [value for value in auth_settings if value in self.configuration.security_schemes] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 6e02b238da1a..8cd7379cc991 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -30,7 +30,7 @@ class Configuration(object): def __init__(self, host="{{{basePath}}}", api_key=None, api_key_prefix=None, - username="", password=""): + username=None, password=None): """Constructor """ self.host = host @@ -60,17 +60,22 @@ class Configuration(object): """Password for HTTP basic authentication """ {{#hasOAuthMethods}} - self.access_token = "" + self.access_token = None """access token for OAuth/Bearer """ {{/hasOAuthMethods}} {{^hasOAuthMethods}} {{#hasBearerMethods}} - self.access_token = "" + self.access_token = None """access token for OAuth/Bearer """ {{/hasBearerMethods}} {{/hasOAuthMethods}} + self.security_schemes = None + """The security schemes to use when authenticating and authorizing the client. + While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), + the client may want to explicitly use a specific security scheme. + """ self.logger = {} """Logging Settings """ @@ -256,51 +261,51 @@ class Configuration(object): :return: The Auth Settings information dict. """ - return { + auth = {} {{#authMethods}} {{#isApiKey}} - '{{name}}': - { - 'type': 'api_key', - 'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, - 'key': '{{keyParamName}}', - 'value': self.get_api_key_with_prefix('{{keyParamName}}') - }, + if self.api_key: + auth['{{name}}'] = { + 'type': 'api_key', + 'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, + 'key': '{{keyParamName}}', + 'value': self.get_api_key_with_prefix('{{keyParamName}}') + } {{/isApiKey}} {{#isBasic}} {{^isBasicBearer}} - '{{name}}': - { - 'type': 'basic', - 'in': 'header', - 'key': 'Authorization', - 'value': self.get_basic_auth_token() - }, + if self.username and self.password: + auth['{{name}}'] = { + 'type': 'basic', + 'in': 'header', + 'key': 'Authorization', + 'value': self.get_basic_auth_token() + } {{/isBasicBearer}} {{#isBasicBearer}} - '{{name}}': - { - 'type': 'bearer', - 'in': 'header', - {{#bearerFormat}} - 'format': '{{{.}}}', - {{/bearerFormat}} - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, + if self.access_token: + auth['{{name}}'] = { + 'type': 'bearer', + 'in': 'header', + {{#bearerFormat}} + 'format': '{{{.}}}', + {{/bearerFormat}} + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } {{/isBasicBearer}} {{/isBasic}} {{#isOAuth}} - '{{name}}': - { - 'type': 'oauth2', - 'in': 'header', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, + if self.access_token: + auth['{{name}}'] = { + 'type': 'oauth2', + 'in': 'header', + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } {{/isOAuth}} {{/authMethods}} - } + return auth def to_debug_report(self): """Gets the essential information for debugging. diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache index 25ca4b5043ff..d167304e36b9 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache @@ -526,7 +526,8 @@ class ApiClient(object): """ if not auth_settings: return - + if self.configuration.security_schemes: + auth_settings = [value for value in auth_settings if value in self.configuration.security_schemes] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: From 7d379db31544a5e5ea70fb2538ba9608fb2f2e8b Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Mon, 25 Nov 2019 10:18:01 -0800 Subject: [PATCH 02/20] Python: conditionally set auth attributes --- .../petstore_api/api_client.py | 3 +- .../petstore_api/configuration.py | 69 ++++++++++--------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/samples/client/petstore/python-experimental/petstore_api/api_client.py b/samples/client/petstore/python-experimental/petstore_api/api_client.py index 6673889b51f7..82c38fe7dfa1 100644 --- a/samples/client/petstore/python-experimental/petstore_api/api_client.py +++ b/samples/client/petstore/python-experimental/petstore_api/api_client.py @@ -519,7 +519,8 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - + if self.configuration.security_schemes: + auth_settings = [value for value in auth_settings if value in self.configuration.security_schemes] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 9a0c95c90458..3ee9887b8e2b 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -36,7 +36,7 @@ class Configuration(object): def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username="", password=""): + username=None, password=None): """Constructor """ self.host = host @@ -65,9 +65,14 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = None """access token for OAuth/Bearer """ + self.security_schemes = None + """The security schemes to use when authenticating and authorizing the client. + While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), + the client may want to explicitly use a specific security scheme. + """ self.logger = {} """Logging Settings """ @@ -245,36 +250,36 @@ def auth_settings(self): :return: The Auth Settings information dict. """ - return { - 'api_key': - { - 'type': 'api_key', - 'in': 'header', - 'key': 'api_key', - 'value': self.get_api_key_with_prefix('api_key') - }, - 'api_key_query': - { - 'type': 'api_key', - 'in': 'query', - 'key': 'api_key_query', - 'value': self.get_api_key_with_prefix('api_key_query') - }, - 'http_basic_test': - { - 'type': 'basic', - 'in': 'header', - 'key': 'Authorization', - 'value': self.get_basic_auth_token() - }, - 'petstore_auth': - { - 'type': 'oauth2', - 'in': 'header', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, - } + auth = {} + if self.api_key: + auth['api_key'] = { + 'type': 'api_key', + 'in': 'header', + 'key': 'api_key', + 'value': self.get_api_key_with_prefix('api_key') + } + if self.api_key: + auth['api_key_query'] = { + 'type': 'api_key', + 'in': 'query', + 'key': 'api_key_query', + 'value': self.get_api_key_with_prefix('api_key_query') + } + if self.username and self.password: + auth['http_basic_test'] = { + 'type': 'basic', + 'in': 'header', + 'key': 'Authorization', + 'value': self.get_basic_auth_token() + } + if self.access_token: + auth['petstore_auth'] = { + 'type': 'oauth2', + 'in': 'header', + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } + return auth def to_debug_report(self): """Gets the essential information for debugging. From 29ee6aff4cea3f8f7b1ad81287f43196e70b4b77 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Mon, 25 Nov 2019 16:54:16 -0800 Subject: [PATCH 03/20] [Python] conditionally set auth attributes. Update unit tests --- .../petstore/python-experimental/tests/test_pet_api.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/samples/client/petstore/python-experimental/tests/test_pet_api.py b/samples/client/petstore/python-experimental/tests/test_pet_api.py index 5e1dd8528c9c..031a17a3a8f9 100644 --- a/samples/client/petstore/python-experimental/tests/test_pet_api.py +++ b/samples/client/petstore/python-experimental/tests/test_pet_api.py @@ -73,6 +73,7 @@ class PetApiTests(unittest.TestCase): def setUp(self): config = Configuration() config.host = HOST + config.access_token = 'ACCESS_TOKEN' self.api_client = petstore_api.ApiClient(config) self.pet_api = petstore_api.PetApi(self.api_client) self.setUpModels() @@ -122,13 +123,13 @@ def test_timeout(self): mock_pool.expect_request('POST', 'http://localhost/v2/pet', body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), headers={'Content-Type': 'application/json', - 'Authorization': 'Bearer ', + 'Authorization': 'Bearer ACCESS_TOKEN', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}, preload_content=True, timeout=TimeoutWithEqual(total=5)) mock_pool.expect_request('POST', 'http://localhost/v2/pet', body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), headers={'Content-Type': 'application/json', - 'Authorization': 'Bearer ', + 'Authorization': 'Bearer ACCESS_TOKEN', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}, preload_content=True, timeout=TimeoutWithEqual(connect=1, read=2)) @@ -325,7 +326,7 @@ def get_header(name, default=None): 'Accept': 'application/json', 'Content-Type': 'multipart/form-data', 'User-Agent': 'OpenAPI-Generator/1.0.0/python', - 'Authorization': 'Bearer ' + 'Authorization': 'Bearer ACCESS_TOKEN' }, post_params=[ ('files', ('1px_pic1.png', b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x00\x00\x00\x00:~\x9bU\x00\x00\x00\nIDATx\x9cc\xfa\x0f\x00\x01\x05\x01\x02\xcf\xa0.\xcd\x00\x00\x00\x00IEND\xaeB`\x82', 'image/png')), From d68bfde9a854dd046e5a8f3b7cd7b2c9f968132a Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Mon, 25 Nov 2019 17:10:28 -0800 Subject: [PATCH 04/20] [Python] conditionally set auth attributes. Format python code --- .../src/main/resources/python/api_client.mustache | 5 ++++- .../resources/python/python-experimental/api_client.mustache | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/api_client.mustache b/modules/openapi-generator/src/main/resources/python/api_client.mustache index d57b51497746..846b80245ec7 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -517,7 +517,10 @@ class ApiClient(object): if not auth_settings: return if self.configuration.security_schemes: - auth_settings = [value for value in auth_settings if value in self.configuration.security_schemes] + auth_settings = [ + value for value in auth_settings + if value in self.configuration.security_schemes + ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache index d167304e36b9..cc90b389e78c 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache @@ -527,7 +527,10 @@ class ApiClient(object): if not auth_settings: return if self.configuration.security_schemes: - auth_settings = [value for value in auth_settings if value in self.configuration.security_schemes] + auth_settings = [ + value for value in auth_settings + if value in self.configuration.security_schemes + ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: From 10e9b6140a1ae69881e490045ff60cb4016522bf Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 01:15:30 +0000 Subject: [PATCH 05/20] [Python] regenerate Python samples --- .../petstore_api/api_client.py | 5 +- .../python/petstore_api/api_client.py | 6 +- .../python/petstore_api/configuration.py | 69 ++++++++++--------- 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/samples/client/petstore/python-experimental/petstore_api/api_client.py b/samples/client/petstore/python-experimental/petstore_api/api_client.py index 82c38fe7dfa1..84ae7b1f2d18 100644 --- a/samples/client/petstore/python-experimental/petstore_api/api_client.py +++ b/samples/client/petstore/python-experimental/petstore_api/api_client.py @@ -520,7 +520,10 @@ def update_params_for_auth(self, headers, querys, auth_settings): if not auth_settings: return if self.configuration.security_schemes: - auth_settings = [value for value in auth_settings if value in self.configuration.security_schemes] + auth_settings = [ + value for value in auth_settings + if value in self.configuration.security_schemes + ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index 46e568df26ab..e32fca19b1d9 100644 --- a/samples/client/petstore/python/petstore_api/api_client.py +++ b/samples/client/petstore/python/petstore_api/api_client.py @@ -509,7 +509,11 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - + if self.configuration.security_schemes: + auth_settings = [ + value for value in auth_settings + if value in self.configuration.security_schemes + ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 9a0c95c90458..3ee9887b8e2b 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -36,7 +36,7 @@ class Configuration(object): def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username="", password=""): + username=None, password=None): """Constructor """ self.host = host @@ -65,9 +65,14 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = None """access token for OAuth/Bearer """ + self.security_schemes = None + """The security schemes to use when authenticating and authorizing the client. + While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), + the client may want to explicitly use a specific security scheme. + """ self.logger = {} """Logging Settings """ @@ -245,36 +250,36 @@ def auth_settings(self): :return: The Auth Settings information dict. """ - return { - 'api_key': - { - 'type': 'api_key', - 'in': 'header', - 'key': 'api_key', - 'value': self.get_api_key_with_prefix('api_key') - }, - 'api_key_query': - { - 'type': 'api_key', - 'in': 'query', - 'key': 'api_key_query', - 'value': self.get_api_key_with_prefix('api_key_query') - }, - 'http_basic_test': - { - 'type': 'basic', - 'in': 'header', - 'key': 'Authorization', - 'value': self.get_basic_auth_token() - }, - 'petstore_auth': - { - 'type': 'oauth2', - 'in': 'header', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, - } + auth = {} + if self.api_key: + auth['api_key'] = { + 'type': 'api_key', + 'in': 'header', + 'key': 'api_key', + 'value': self.get_api_key_with_prefix('api_key') + } + if self.api_key: + auth['api_key_query'] = { + 'type': 'api_key', + 'in': 'query', + 'key': 'api_key_query', + 'value': self.get_api_key_with_prefix('api_key_query') + } + if self.username and self.password: + auth['http_basic_test'] = { + 'type': 'basic', + 'in': 'header', + 'key': 'Authorization', + 'value': self.get_basic_auth_token() + } + if self.access_token: + auth['petstore_auth'] = { + 'type': 'oauth2', + 'in': 'header', + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } + return auth def to_debug_report(self): """Gets the essential information for debugging. From 5a17a9d8da6aa1840a8ad5f3ee3efcda04b1fe68 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 01:50:03 +0000 Subject: [PATCH 06/20] [Python] update unit test --- samples/client/petstore/python/tests/test_pet_api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/client/petstore/python/tests/test_pet_api.py b/samples/client/petstore/python/tests/test_pet_api.py index 80d34f6b5f21..2e20059f22a6 100644 --- a/samples/client/petstore/python/tests/test_pet_api.py +++ b/samples/client/petstore/python/tests/test_pet_api.py @@ -57,6 +57,7 @@ class PetApiTests(unittest.TestCase): def setUp(self): config = Configuration() config.host = HOST + config.access_token = 'ACCESS_TOKEN' self.api_client = petstore_api.ApiClient(config) self.pet_api = petstore_api.PetApi(self.api_client) self.setUpModels() @@ -107,13 +108,13 @@ def test_timeout(self): mock_pool.expect_request('POST', HOST + '/pet', body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), headers={'Content-Type': 'application/json', - 'Authorization': 'Bearer ', + 'Authorization': 'Bearer ACCESS_TOKEN', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}, preload_content=True, timeout=TimeoutWithEqual(total=5)) mock_pool.expect_request('POST', HOST + '/pet', body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), headers={'Content-Type': 'application/json', - 'Authorization': 'Bearer ', + 'Authorization': 'Bearer ACCESS_TOKEN', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}, preload_content=True, timeout=TimeoutWithEqual(connect=1, read=2)) From 7a119d929e90eda962ed2587f07d760cec988058 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 02:22:30 +0000 Subject: [PATCH 07/20] [Python] run ./bin/python-petstore-all.sh --- .../python-asyncio/petstore_api/api_client.py | 6 +- .../petstore_api/configuration.py | 69 ++++++++++--------- .../python-tornado/petstore_api/api_client.py | 6 +- .../petstore_api/configuration.py | 69 ++++++++++--------- 4 files changed, 84 insertions(+), 66 deletions(-) diff --git a/samples/client/petstore/python-asyncio/petstore_api/api_client.py b/samples/client/petstore/python-asyncio/petstore_api/api_client.py index 24b99a2946f4..043df5c76087 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/api_client.py +++ b/samples/client/petstore/python-asyncio/petstore_api/api_client.py @@ -509,7 +509,11 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - + if self.configuration.security_schemes: + auth_settings = [ + value for value in auth_settings + if value in self.configuration.security_schemes + ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index fa90c926deef..916d75bab52b 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -35,7 +35,7 @@ class Configuration(object): def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username="", password=""): + username=None, password=None): """Constructor """ self.host = host @@ -64,9 +64,14 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = None """access token for OAuth/Bearer """ + self.security_schemes = None + """The security schemes to use when authenticating and authorizing the client. + While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), + the client may want to explicitly use a specific security scheme. + """ self.logger = {} """Logging Settings """ @@ -241,36 +246,36 @@ def auth_settings(self): :return: The Auth Settings information dict. """ - return { - 'api_key': - { - 'type': 'api_key', - 'in': 'header', - 'key': 'api_key', - 'value': self.get_api_key_with_prefix('api_key') - }, - 'api_key_query': - { - 'type': 'api_key', - 'in': 'query', - 'key': 'api_key_query', - 'value': self.get_api_key_with_prefix('api_key_query') - }, - 'http_basic_test': - { - 'type': 'basic', - 'in': 'header', - 'key': 'Authorization', - 'value': self.get_basic_auth_token() - }, - 'petstore_auth': - { - 'type': 'oauth2', - 'in': 'header', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, - } + auth = {} + if self.api_key: + auth['api_key'] = { + 'type': 'api_key', + 'in': 'header', + 'key': 'api_key', + 'value': self.get_api_key_with_prefix('api_key') + } + if self.api_key: + auth['api_key_query'] = { + 'type': 'api_key', + 'in': 'query', + 'key': 'api_key_query', + 'value': self.get_api_key_with_prefix('api_key_query') + } + if self.username and self.password: + auth['http_basic_test'] = { + 'type': 'basic', + 'in': 'header', + 'key': 'Authorization', + 'value': self.get_basic_auth_token() + } + if self.access_token: + auth['petstore_auth'] = { + 'type': 'oauth2', + 'in': 'header', + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } + return auth def to_debug_report(self): """Gets the essential information for debugging. diff --git a/samples/client/petstore/python-tornado/petstore_api/api_client.py b/samples/client/petstore/python-tornado/petstore_api/api_client.py index 0d1075ffe970..335ff6c1bc83 100644 --- a/samples/client/petstore/python-tornado/petstore_api/api_client.py +++ b/samples/client/petstore/python-tornado/petstore_api/api_client.py @@ -511,7 +511,11 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - + if self.configuration.security_schemes: + auth_settings = [ + value for value in auth_settings + if value in self.configuration.security_schemes + ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 9a0c95c90458..3ee9887b8e2b 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -36,7 +36,7 @@ class Configuration(object): def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username="", password=""): + username=None, password=None): """Constructor """ self.host = host @@ -65,9 +65,14 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = None """access token for OAuth/Bearer """ + self.security_schemes = None + """The security schemes to use when authenticating and authorizing the client. + While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), + the client may want to explicitly use a specific security scheme. + """ self.logger = {} """Logging Settings """ @@ -245,36 +250,36 @@ def auth_settings(self): :return: The Auth Settings information dict. """ - return { - 'api_key': - { - 'type': 'api_key', - 'in': 'header', - 'key': 'api_key', - 'value': self.get_api_key_with_prefix('api_key') - }, - 'api_key_query': - { - 'type': 'api_key', - 'in': 'query', - 'key': 'api_key_query', - 'value': self.get_api_key_with_prefix('api_key_query') - }, - 'http_basic_test': - { - 'type': 'basic', - 'in': 'header', - 'key': 'Authorization', - 'value': self.get_basic_auth_token() - }, - 'petstore_auth': - { - 'type': 'oauth2', - 'in': 'header', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, - } + auth = {} + if self.api_key: + auth['api_key'] = { + 'type': 'api_key', + 'in': 'header', + 'key': 'api_key', + 'value': self.get_api_key_with_prefix('api_key') + } + if self.api_key: + auth['api_key_query'] = { + 'type': 'api_key', + 'in': 'query', + 'key': 'api_key_query', + 'value': self.get_api_key_with_prefix('api_key_query') + } + if self.username and self.password: + auth['http_basic_test'] = { + 'type': 'basic', + 'in': 'header', + 'key': 'Authorization', + 'value': self.get_basic_auth_token() + } + if self.access_token: + auth['petstore_auth'] = { + 'type': 'oauth2', + 'in': 'header', + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } + return auth def to_debug_report(self): """Gets the essential information for debugging. From 670551a45ab65599af3950755491c924ea5a8a28 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 05:08:39 +0000 Subject: [PATCH 08/20] [Python] remove security scheme attribute --- .../src/main/resources/python/api_client.mustache | 5 ----- .../src/main/resources/python/configuration.mustache | 5 ----- .../resources/python/python-experimental/api_client.mustache | 5 ----- .../petstore/python-asyncio/petstore_api/api_client.py | 5 ----- .../petstore/python-asyncio/petstore_api/configuration.py | 5 ----- .../petstore/python-experimental/petstore_api/api_client.py | 5 ----- .../python-experimental/petstore_api/configuration.py | 5 ----- .../petstore/python-tornado/petstore_api/api_client.py | 5 ----- .../petstore/python-tornado/petstore_api/configuration.py | 5 ----- samples/client/petstore/python/petstore_api/api_client.py | 5 ----- samples/client/petstore/python/petstore_api/configuration.py | 5 ----- 11 files changed, 55 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/api_client.mustache b/modules/openapi-generator/src/main/resources/python/api_client.mustache index 846b80245ec7..e37128d224cc 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -516,11 +516,6 @@ class ApiClient(object): """ if not auth_settings: return - if self.configuration.security_schemes: - auth_settings = [ - value for value in auth_settings - if value in self.configuration.security_schemes - ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 8cd7379cc991..3cdfa864853b 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -71,11 +71,6 @@ class Configuration(object): """ {{/hasBearerMethods}} {{/hasOAuthMethods}} - self.security_schemes = None - """The security schemes to use when authenticating and authorizing the client. - While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), - the client may want to explicitly use a specific security scheme. - """ self.logger = {} """Logging Settings """ diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache index cc90b389e78c..de4aed9b3606 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache @@ -526,11 +526,6 @@ class ApiClient(object): """ if not auth_settings: return - if self.configuration.security_schemes: - auth_settings = [ - value for value in auth_settings - if value in self.configuration.security_schemes - ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python-asyncio/petstore_api/api_client.py b/samples/client/petstore/python-asyncio/petstore_api/api_client.py index 043df5c76087..f560488f08eb 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/api_client.py +++ b/samples/client/petstore/python-asyncio/petstore_api/api_client.py @@ -509,11 +509,6 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - if self.configuration.security_schemes: - auth_settings = [ - value for value in auth_settings - if value in self.configuration.security_schemes - ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index 916d75bab52b..7b9ee7ca32e4 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -67,11 +67,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.access_token = None """access token for OAuth/Bearer """ - self.security_schemes = None - """The security schemes to use when authenticating and authorizing the client. - While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), - the client may want to explicitly use a specific security scheme. - """ self.logger = {} """Logging Settings """ diff --git a/samples/client/petstore/python-experimental/petstore_api/api_client.py b/samples/client/petstore/python-experimental/petstore_api/api_client.py index 84ae7b1f2d18..eae2368f4d28 100644 --- a/samples/client/petstore/python-experimental/petstore_api/api_client.py +++ b/samples/client/petstore/python-experimental/petstore_api/api_client.py @@ -519,11 +519,6 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - if self.configuration.security_schemes: - auth_settings = [ - value for value in auth_settings - if value in self.configuration.security_schemes - ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 3ee9887b8e2b..5228a6c7e3ec 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -68,11 +68,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.access_token = None """access token for OAuth/Bearer """ - self.security_schemes = None - """The security schemes to use when authenticating and authorizing the client. - While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), - the client may want to explicitly use a specific security scheme. - """ self.logger = {} """Logging Settings """ diff --git a/samples/client/petstore/python-tornado/petstore_api/api_client.py b/samples/client/petstore/python-tornado/petstore_api/api_client.py index 335ff6c1bc83..fcc468257ffb 100644 --- a/samples/client/petstore/python-tornado/petstore_api/api_client.py +++ b/samples/client/petstore/python-tornado/petstore_api/api_client.py @@ -511,11 +511,6 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - if self.configuration.security_schemes: - auth_settings = [ - value for value in auth_settings - if value in self.configuration.security_schemes - ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 3ee9887b8e2b..5228a6c7e3ec 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -68,11 +68,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.access_token = None """access token for OAuth/Bearer """ - self.security_schemes = None - """The security schemes to use when authenticating and authorizing the client. - While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), - the client may want to explicitly use a specific security scheme. - """ self.logger = {} """Logging Settings """ diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index e32fca19b1d9..961aaeb0c5eb 100644 --- a/samples/client/petstore/python/petstore_api/api_client.py +++ b/samples/client/petstore/python/petstore_api/api_client.py @@ -509,11 +509,6 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - if self.configuration.security_schemes: - auth_settings = [ - value for value in auth_settings - if value in self.configuration.security_schemes - ] for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 3ee9887b8e2b..5228a6c7e3ec 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -68,11 +68,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.access_token = None """access token for OAuth/Bearer """ - self.security_schemes = None - """The security schemes to use when authenticating and authorizing the client. - While the OpenAPI specification may support multiple security schemes (e.g. OAuth2, basic), - the client may want to explicitly use a specific security scheme. - """ self.logger = {} """Logging Settings """ From 688e0ae7f390962720cb12789cb54e48f3a06ddc Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 05:11:35 +0000 Subject: [PATCH 09/20] [Python] Address spacether PR comments, remove unnecessary test --- .../src/main/resources/python/api_client.mustache | 4 +--- .../resources/python/python-experimental/api_client.mustache | 4 +--- .../client/petstore/python-asyncio/petstore_api/api_client.py | 4 +--- .../petstore/python-experimental/petstore_api/api_client.py | 4 +--- .../client/petstore/python-tornado/petstore_api/api_client.py | 4 +--- samples/client/petstore/python/petstore_api/api_client.py | 4 +--- 6 files changed, 6 insertions(+), 18 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/api_client.mustache b/modules/openapi-generator/src/main/resources/python/api_client.mustache index e37128d224cc..57bb78133c53 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -519,9 +519,7 @@ class ApiClient(object): for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: - if not auth_setting['value']: - continue - elif auth_setting['in'] == 'cookie': + if auth_setting['in'] == 'cookie': headers['Cookie'] = auth_setting['value'] elif auth_setting['in'] == 'header': headers[auth_setting['key']] = auth_setting['value'] diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache index de4aed9b3606..a553f05ec62d 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache @@ -529,9 +529,7 @@ class ApiClient(object): for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: - if not auth_setting['value']: - continue - elif auth_setting['in'] == 'cookie': + if auth_setting['in'] == 'cookie': headers['Cookie'] = auth_setting['value'] elif auth_setting['in'] == 'header': headers[auth_setting['key']] = auth_setting['value'] diff --git a/samples/client/petstore/python-asyncio/petstore_api/api_client.py b/samples/client/petstore/python-asyncio/petstore_api/api_client.py index f560488f08eb..b07166d76450 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/api_client.py +++ b/samples/client/petstore/python-asyncio/petstore_api/api_client.py @@ -512,9 +512,7 @@ def update_params_for_auth(self, headers, querys, auth_settings): for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: - if not auth_setting['value']: - continue - elif auth_setting['in'] == 'cookie': + if auth_setting['in'] == 'cookie': headers['Cookie'] = auth_setting['value'] elif auth_setting['in'] == 'header': headers[auth_setting['key']] = auth_setting['value'] diff --git a/samples/client/petstore/python-experimental/petstore_api/api_client.py b/samples/client/petstore/python-experimental/petstore_api/api_client.py index eae2368f4d28..37025b1314b3 100644 --- a/samples/client/petstore/python-experimental/petstore_api/api_client.py +++ b/samples/client/petstore/python-experimental/petstore_api/api_client.py @@ -522,9 +522,7 @@ def update_params_for_auth(self, headers, querys, auth_settings): for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: - if not auth_setting['value']: - continue - elif auth_setting['in'] == 'cookie': + if auth_setting['in'] == 'cookie': headers['Cookie'] = auth_setting['value'] elif auth_setting['in'] == 'header': headers[auth_setting['key']] = auth_setting['value'] diff --git a/samples/client/petstore/python-tornado/petstore_api/api_client.py b/samples/client/petstore/python-tornado/petstore_api/api_client.py index fcc468257ffb..d86d8817779b 100644 --- a/samples/client/petstore/python-tornado/petstore_api/api_client.py +++ b/samples/client/petstore/python-tornado/petstore_api/api_client.py @@ -514,9 +514,7 @@ def update_params_for_auth(self, headers, querys, auth_settings): for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: - if not auth_setting['value']: - continue - elif auth_setting['in'] == 'cookie': + if auth_setting['in'] == 'cookie': headers['Cookie'] = auth_setting['value'] elif auth_setting['in'] == 'header': headers[auth_setting['key']] = auth_setting['value'] diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index 961aaeb0c5eb..52910fd8d0d9 100644 --- a/samples/client/petstore/python/petstore_api/api_client.py +++ b/samples/client/petstore/python/petstore_api/api_client.py @@ -512,9 +512,7 @@ def update_params_for_auth(self, headers, querys, auth_settings): for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: - if not auth_setting['value']: - continue - elif auth_setting['in'] == 'cookie': + if auth_setting['in'] == 'cookie': headers['Cookie'] = auth_setting['value'] elif auth_setting['in'] == 'header': headers[auth_setting['key']] = auth_setting['value'] From 26258b687ea182e08eff8885a5a89f1d03a74409 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 05:24:15 +0000 Subject: [PATCH 10/20] [Python] Address spacether PR comments, add test --- .../src/main/resources/python/configuration.mustache | 2 +- .../petstore/python-asyncio/petstore_api/configuration.py | 4 ++-- .../python-experimental/petstore_api/configuration.py | 4 ++-- .../petstore/python-tornado/petstore_api/configuration.py | 4 ++-- samples/client/petstore/python/petstore_api/configuration.py | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 3cdfa864853b..b3ef7c4db7bf 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -259,7 +259,7 @@ class Configuration(object): auth = {} {{#authMethods}} {{#isApiKey}} - if self.api_key: + if self.api_key and '{{keyParamName}}' in self.api_key: auth['{{name}}'] = { 'type': 'api_key', 'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index 7b9ee7ca32e4..b65d83699568 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -242,14 +242,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key: + if self.api_key and 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key: + if self.api_key and 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 5228a6c7e3ec..d8a6020be390 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -246,14 +246,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key: + if self.api_key and 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key: + if self.api_key and 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 5228a6c7e3ec..d8a6020be390 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -246,14 +246,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key: + if self.api_key and 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key: + if self.api_key and 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 5228a6c7e3ec..d8a6020be390 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -246,14 +246,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key: + if self.api_key and 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key: + if self.api_key and 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', From 65f215233f01e26f01224bef9d9e3b7001941df5 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 06:29:47 +0000 Subject: [PATCH 11/20] [Python] Address spacether PR comments --- .../resources/python/configuration.mustache | 21 ++--- .../petstore_api/configuration.py | 10 ++- .../petstore_api/configuration.py | 10 ++- .../petstore_api/configuration.py | 10 ++- .../python/petstore_api/configuration.py | 10 ++- .../python/petstore_api/api_client.py | 5 +- .../python/petstore_api/configuration.py | 82 ++++++++++--------- 7 files changed, 74 insertions(+), 74 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index b3ef7c4db7bf..fbc4968ec68a 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -26,11 +26,13 @@ class Configuration(object): :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication + :param access_token: The OAuth2 access token """ def __init__(self, host="{{{basePath}}}", api_key=None, api_key_prefix=None, - username=None, password=None): + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -59,18 +61,9 @@ class Configuration(object): self.password = password """Password for HTTP basic authentication """ -{{#hasOAuthMethods}} - self.access_token = None + self.access_token = access_token """access token for OAuth/Bearer """ -{{/hasOAuthMethods}} -{{^hasOAuthMethods}} -{{#hasBearerMethods}} - self.access_token = None - """access token for OAuth/Bearer - """ -{{/hasBearerMethods}} -{{/hasOAuthMethods}} self.logger = {} """Logging Settings """ @@ -269,7 +262,7 @@ class Configuration(object): {{/isApiKey}} {{#isBasic}} {{^isBasicBearer}} - if self.username and self.password: + if self.username is not None and self.password is not None: auth['{{name}}'] = { 'type': 'basic', 'in': 'header', @@ -278,7 +271,7 @@ class Configuration(object): } {{/isBasicBearer}} {{#isBasicBearer}} - if self.access_token: + if self.access_token is not None: auth['{{name}}'] = { 'type': 'bearer', 'in': 'header', @@ -291,7 +284,7 @@ class Configuration(object): {{/isBasicBearer}} {{/isBasic}} {{#isOAuth}} - if self.access_token: + if self.access_token is not None: auth['{{name}}'] = { 'type': 'oauth2', 'in': 'header', diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index b65d83699568..e29f7c7339ba 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -31,11 +31,13 @@ class Configuration(object): :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication + :param access_token: The OAuth2 access token """ def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username=None, password=None): + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -64,7 +66,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = None + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -256,14 +258,14 @@ def auth_settings(self): 'key': 'api_key_query', 'value': self.get_api_key_with_prefix('api_key_query') } - if self.username and self.password: + if self.username is not None and self.password is not None: auth['http_basic_test'] = { 'type': 'basic', 'in': 'header', 'key': 'Authorization', 'value': self.get_basic_auth_token() } - if self.access_token: + if self.access_token is not None: auth['petstore_auth'] = { 'type': 'oauth2', 'in': 'header', diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index d8a6020be390..74c8639c3a87 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -32,11 +32,13 @@ class Configuration(object): :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication + :param access_token: The OAuth2 access token """ def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username=None, password=None): + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -65,7 +67,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = None + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -260,14 +262,14 @@ def auth_settings(self): 'key': 'api_key_query', 'value': self.get_api_key_with_prefix('api_key_query') } - if self.username and self.password: + if self.username is not None and self.password is not None: auth['http_basic_test'] = { 'type': 'basic', 'in': 'header', 'key': 'Authorization', 'value': self.get_basic_auth_token() } - if self.access_token: + if self.access_token is not None: auth['petstore_auth'] = { 'type': 'oauth2', 'in': 'header', diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index d8a6020be390..74c8639c3a87 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -32,11 +32,13 @@ class Configuration(object): :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication + :param access_token: The OAuth2 access token """ def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username=None, password=None): + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -65,7 +67,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = None + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -260,14 +262,14 @@ def auth_settings(self): 'key': 'api_key_query', 'value': self.get_api_key_with_prefix('api_key_query') } - if self.username and self.password: + if self.username is not None and self.password is not None: auth['http_basic_test'] = { 'type': 'basic', 'in': 'header', 'key': 'Authorization', 'value': self.get_basic_auth_token() } - if self.access_token: + if self.access_token is not None: auth['petstore_auth'] = { 'type': 'oauth2', 'in': 'header', diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index d8a6020be390..74c8639c3a87 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -32,11 +32,13 @@ class Configuration(object): :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication + :param access_token: The OAuth2 access token """ def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username=None, password=None): + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -65,7 +67,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = None + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -260,14 +262,14 @@ def auth_settings(self): 'key': 'api_key_query', 'value': self.get_api_key_with_prefix('api_key_query') } - if self.username and self.password: + if self.username is not None and self.password is not None: auth['http_basic_test'] = { 'type': 'basic', 'in': 'header', 'key': 'Authorization', 'value': self.get_basic_auth_token() } - if self.access_token: + if self.access_token is not None: auth['petstore_auth'] = { 'type': 'oauth2', 'in': 'header', diff --git a/samples/openapi3/client/petstore/python/petstore_api/api_client.py b/samples/openapi3/client/petstore/python/petstore_api/api_client.py index 46e568df26ab..52910fd8d0d9 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -509,13 +509,10 @@ def update_params_for_auth(self, headers, querys, auth_settings): """ if not auth_settings: return - for auth in auth_settings: auth_setting = self.configuration.auth_settings().get(auth) if auth_setting: - if not auth_setting['value']: - continue - elif auth_setting['in'] == 'cookie': + if auth_setting['in'] == 'cookie': headers['Cookie'] = auth_setting['value'] elif auth_setting['in'] == 'header': headers[auth_setting['key']] = auth_setting['value'] diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index 91109c7d78cb..0bbf73c3dc21 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -32,11 +32,13 @@ class Configuration(object): :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication + :param access_token: The OAuth2 access token """ def __init__(self, host="http://petstore.swagger.io:80/v2", api_key=None, api_key_prefix=None, - username="", password=""): + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -65,7 +67,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -245,44 +247,44 @@ def auth_settings(self): :return: The Auth Settings information dict. """ - return { - 'api_key': - { - 'type': 'api_key', - 'in': 'header', - 'key': 'api_key', - 'value': self.get_api_key_with_prefix('api_key') - }, - 'api_key_query': - { - 'type': 'api_key', - 'in': 'query', - 'key': 'api_key_query', - 'value': self.get_api_key_with_prefix('api_key_query') - }, - 'bearer_test': - { - 'type': 'bearer', - 'in': 'header', - 'format': 'JWT', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, - 'http_basic_test': - { - 'type': 'basic', - 'in': 'header', - 'key': 'Authorization', - 'value': self.get_basic_auth_token() - }, - 'petstore_auth': - { - 'type': 'oauth2', - 'in': 'header', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, - } + auth = {} + if self.api_key and 'api_key' in self.api_key: + auth['api_key'] = { + 'type': 'api_key', + 'in': 'header', + 'key': 'api_key', + 'value': self.get_api_key_with_prefix('api_key') + } + if self.api_key and 'api_key_query' in self.api_key: + auth['api_key_query'] = { + 'type': 'api_key', + 'in': 'query', + 'key': 'api_key_query', + 'value': self.get_api_key_with_prefix('api_key_query') + } + if self.access_token is not None: + auth['bearer_test'] = { + 'type': 'bearer', + 'in': 'header', + 'format': 'JWT', + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } + if self.username is not None and self.password is not None: + auth['http_basic_test'] = { + 'type': 'basic', + 'in': 'header', + 'key': 'Authorization', + 'value': self.get_basic_auth_token() + } + if self.access_token is not None: + auth['petstore_auth'] = { + 'type': 'oauth2', + 'in': 'header', + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } + return auth def to_debug_report(self): """Gets the essential information for debugging. From f1f2a7234981307c67890ac10595d3d3816e7298 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 07:17:51 +0000 Subject: [PATCH 12/20] [Python] Address spacether PR comments --- .../resources/python/configuration.mustache | 8 +++++- .../petstore_api/configuration.py | 8 +++++- .../petstore_api/configuration.py | 8 +++++- .../python-experimental/tests/test_pet_api.py | 27 ++++++++++++++++--- .../petstore_api/configuration.py | 8 +++++- .../python/petstore_api/configuration.py | 8 +++++- .../python/petstore_api/configuration.py | 8 +++++- 7 files changed, 65 insertions(+), 10 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index fbc4968ec68a..ae922c052524 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -240,8 +240,14 @@ class Configuration(object): :return: The token for basic HTTP authentication. """ + username = "" + if self.username is not None: + username = self.username + password = "" + if self.password is not None: + password = self.password return urllib3.util.make_headers( - basic_auth=self.username + ':' + self.password + basic_auth=username + ':' + password ).get('authorization') def auth_settings(self): diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index e29f7c7339ba..ad82341022a1 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -234,8 +234,14 @@ def get_basic_auth_token(self): :return: The token for basic HTTP authentication. """ + username = "" + if self.username is not None: + username = self.username + password = "" + if self.password is not None: + password = self.password return urllib3.util.make_headers( - basic_auth=self.username + ':' + self.password + basic_auth=username + ':' + password ).get('authorization') def auth_settings(self): diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 74c8639c3a87..54263928632e 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -238,8 +238,14 @@ def get_basic_auth_token(self): :return: The token for basic HTTP authentication. """ + username = "" + if self.username is not None: + username = self.username + password = "" + if self.password is not None: + password = self.password return urllib3.util.make_headers( - basic_auth=self.username + ':' + self.password + basic_auth=username + ':' + password ).get('authorization') def auth_settings(self): diff --git a/samples/client/petstore/python-experimental/tests/test_pet_api.py b/samples/client/petstore/python-experimental/tests/test_pet_api.py index 031a17a3a8f9..90af52052228 100644 --- a/samples/client/petstore/python-experimental/tests/test_pet_api.py +++ b/samples/client/petstore/python-experimental/tests/test_pet_api.py @@ -71,10 +71,10 @@ def request(self, *args, **kwargs): class PetApiTests(unittest.TestCase): def setUp(self): - config = Configuration() - config.host = HOST - config.access_token = 'ACCESS_TOKEN' - self.api_client = petstore_api.ApiClient(config) + self.config = Configuration() + self.config.host = HOST + self.config.access_token = 'ACCESS_TOKEN' + self.api_client = petstore_api.ApiClient(self.config) self.pet_api = petstore_api.PetApi(self.api_client) self.setUpModels() self.setUpFiles() @@ -116,6 +116,25 @@ def test_preload_content_flag(self): resp.close() resp.release_conn() + def test_config(self): + self.assertIsNotNone(self.config.get_host_settings()) + self.assertEquals(self.config.get_basic_auth_token(), + urllib3.util.make_headers(basic_auth=":").get('authorization')) + self.assertEquals(len(self.config.auth_settings()), 1) + self.assertIn("petstore_auth", self.config.auth_settings().keys()) + self.config.username = "user" + self.config.password = "password" + self.assertEquals( + self.config.get_basic_auth_token(), + urllib3.util.make_headers(basic_auth="user:password").get('authorization')) + self.assertEquals(len(self.config.auth_settings()), 2) + self.assertIn("petstore_auth", self.config.auth_settings().keys()) + self.assertIn("http_basic_test", self.config.auth_settings().keys()) + self.config.username = None + self.config.password = None + self.assertEquals(len(self.config.auth_settings()), 1) + self.assertIn("petstore_auth", self.config.auth_settings().keys()) + def test_timeout(self): mock_pool = MockPoolManager(self) self.api_client.rest_client.pool_manager = mock_pool diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 74c8639c3a87..54263928632e 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -238,8 +238,14 @@ def get_basic_auth_token(self): :return: The token for basic HTTP authentication. """ + username = "" + if self.username is not None: + username = self.username + password = "" + if self.password is not None: + password = self.password return urllib3.util.make_headers( - basic_auth=self.username + ':' + self.password + basic_auth=username + ':' + password ).get('authorization') def auth_settings(self): diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 74c8639c3a87..54263928632e 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -238,8 +238,14 @@ def get_basic_auth_token(self): :return: The token for basic HTTP authentication. """ + username = "" + if self.username is not None: + username = self.username + password = "" + if self.password is not None: + password = self.password return urllib3.util.make_headers( - basic_auth=self.username + ':' + self.password + basic_auth=username + ':' + password ).get('authorization') def auth_settings(self): diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index 0bbf73c3dc21..62259ca8b620 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -238,8 +238,14 @@ def get_basic_auth_token(self): :return: The token for basic HTTP authentication. """ + username = "" + if self.username is not None: + username = self.username + password = "" + if self.password is not None: + password = self.password return urllib3.util.make_headers( - basic_auth=self.username + ':' + self.password + basic_auth=username + ':' + password ).get('authorization') def auth_settings(self): From fb7bc4613b3fd3fd59beb866de33a77baafdecda Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 17:39:19 +0000 Subject: [PATCH 13/20] [Python] Add sample to configure API keys --- .../resources/python/configuration.mustache | 26 +++++++++++++++-- .../petstore_api/configuration.py | 28 ++++++++++++++++--- .../petstore_api/configuration.py | 28 ++++++++++++++++--- .../petstore_api/configuration.py | 28 ++++++++++++++++--- .../python/petstore_api/configuration.py | 28 ++++++++++++++++--- .../python/petstore_api/configuration.py | 28 ++++++++++++++++--- 6 files changed, 143 insertions(+), 23 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index ae922c052524..0d5212edc11f 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -22,11 +22,31 @@ class Configuration(object): Do not edit the class manually. :param host: Base url - :param api_key: Dict to store API key(s) + :param api_key: Dict to store API key(s). + The dict key should be the name of the api key, as specified in the OAS specification. + The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token + + :Example: + + Given the following security scheme in the OpenAPI specification: + components: + securitySchemes: + cookieAuth: # name for the security scheme + type: apiKey + in: cookie + name: JSESSIONID # cookie name + + You can programmatically set the cookie: + conf = {{{packageName}}}.Configuration( + api_key={'JSESSIONID': 'abc123'} + api_key_prefix={'JSESSIONID': 'JSESSIONID='} + ) + The following cookie will be added to the HTTP request: + Cookie: JSESSIONID=abc123 """ def __init__(self, host="{{{basePath}}}", @@ -231,7 +251,7 @@ class Configuration(object): if key: prefix = self.api_key_prefix.get(identifier) if prefix: - return "%s %s" % (prefix, key) + return "%s%s" % (prefix, key) else: return key @@ -258,7 +278,7 @@ class Configuration(object): auth = {} {{#authMethods}} {{#isApiKey}} - if self.api_key and '{{keyParamName}}' in self.api_key: + if '{{keyParamName}}' in self.api_key: auth['{{name}}'] = { 'type': 'api_key', 'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index ad82341022a1..a02784da1b8c 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -27,11 +27,31 @@ class Configuration(object): Do not edit the class manually. :param host: Base url - :param api_key: Dict to store API key(s) + :param api_key: Dict to store API key(s). + The dict key should be the name of the api key, as specified in the OAS specification. + The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token + + :Example: + + Given the following security scheme in the OpenAPI specification: + components: + securitySchemes: + cookieAuth: # name for the security scheme + type: apiKey + in: cookie + name: JSESSIONID # cookie name + + You can programmatically set the cookie: + conf = petstore_api.Configuration( + api_key={'JSESSIONID': 'abc123'} + api_key_prefix={'JSESSIONID': 'JSESSIONID='} + ) + The following cookie will be added to the HTTP request: + Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", @@ -225,7 +245,7 @@ def get_api_key_with_prefix(self, identifier): if key: prefix = self.api_key_prefix.get(identifier) if prefix: - return "%s %s" % (prefix, key) + return "%s%s" % (prefix, key) else: return key @@ -250,14 +270,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key and 'api_key' in self.api_key: + if 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key and 'api_key_query' in self.api_key: + if 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 54263928632e..490b4d73c260 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -28,11 +28,31 @@ class Configuration(object): Do not edit the class manually. :param host: Base url - :param api_key: Dict to store API key(s) + :param api_key: Dict to store API key(s). + The dict key should be the name of the api key, as specified in the OAS specification. + The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token + + :Example: + + Given the following security scheme in the OpenAPI specification: + components: + securitySchemes: + cookieAuth: # name for the security scheme + type: apiKey + in: cookie + name: JSESSIONID # cookie name + + You can programmatically set the cookie: + conf = petstore_api.Configuration( + api_key={'JSESSIONID': 'abc123'} + api_key_prefix={'JSESSIONID': 'JSESSIONID='} + ) + The following cookie will be added to the HTTP request: + Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", @@ -229,7 +249,7 @@ def get_api_key_with_prefix(self, identifier): if key: prefix = self.api_key_prefix.get(identifier) if prefix: - return "%s %s" % (prefix, key) + return "%s%s" % (prefix, key) else: return key @@ -254,14 +274,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key and 'api_key' in self.api_key: + if 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key and 'api_key_query' in self.api_key: + if 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 54263928632e..490b4d73c260 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -28,11 +28,31 @@ class Configuration(object): Do not edit the class manually. :param host: Base url - :param api_key: Dict to store API key(s) + :param api_key: Dict to store API key(s). + The dict key should be the name of the api key, as specified in the OAS specification. + The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token + + :Example: + + Given the following security scheme in the OpenAPI specification: + components: + securitySchemes: + cookieAuth: # name for the security scheme + type: apiKey + in: cookie + name: JSESSIONID # cookie name + + You can programmatically set the cookie: + conf = petstore_api.Configuration( + api_key={'JSESSIONID': 'abc123'} + api_key_prefix={'JSESSIONID': 'JSESSIONID='} + ) + The following cookie will be added to the HTTP request: + Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", @@ -229,7 +249,7 @@ def get_api_key_with_prefix(self, identifier): if key: prefix = self.api_key_prefix.get(identifier) if prefix: - return "%s %s" % (prefix, key) + return "%s%s" % (prefix, key) else: return key @@ -254,14 +274,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key and 'api_key' in self.api_key: + if 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key and 'api_key_query' in self.api_key: + if 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 54263928632e..490b4d73c260 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -28,11 +28,31 @@ class Configuration(object): Do not edit the class manually. :param host: Base url - :param api_key: Dict to store API key(s) + :param api_key: Dict to store API key(s). + The dict key should be the name of the api key, as specified in the OAS specification. + The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token + + :Example: + + Given the following security scheme in the OpenAPI specification: + components: + securitySchemes: + cookieAuth: # name for the security scheme + type: apiKey + in: cookie + name: JSESSIONID # cookie name + + You can programmatically set the cookie: + conf = petstore_api.Configuration( + api_key={'JSESSIONID': 'abc123'} + api_key_prefix={'JSESSIONID': 'JSESSIONID='} + ) + The following cookie will be added to the HTTP request: + Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", @@ -229,7 +249,7 @@ def get_api_key_with_prefix(self, identifier): if key: prefix = self.api_key_prefix.get(identifier) if prefix: - return "%s %s" % (prefix, key) + return "%s%s" % (prefix, key) else: return key @@ -254,14 +274,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key and 'api_key' in self.api_key: + if 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key and 'api_key_query' in self.api_key: + if 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index 62259ca8b620..b0192a85b144 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -28,11 +28,31 @@ class Configuration(object): Do not edit the class manually. :param host: Base url - :param api_key: Dict to store API key(s) + :param api_key: Dict to store API key(s). + The dict key should be the name of the api key, as specified in the OAS specification. + The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token + + :Example: + + Given the following security scheme in the OpenAPI specification: + components: + securitySchemes: + cookieAuth: # name for the security scheme + type: apiKey + in: cookie + name: JSESSIONID # cookie name + + You can programmatically set the cookie: + conf = petstore_api.Configuration( + api_key={'JSESSIONID': 'abc123'} + api_key_prefix={'JSESSIONID': 'JSESSIONID='} + ) + The following cookie will be added to the HTTP request: + Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", @@ -229,7 +249,7 @@ def get_api_key_with_prefix(self, identifier): if key: prefix = self.api_key_prefix.get(identifier) if prefix: - return "%s %s" % (prefix, key) + return "%s%s" % (prefix, key) else: return key @@ -254,14 +274,14 @@ def auth_settings(self): :return: The Auth Settings information dict. """ auth = {} - if self.api_key and 'api_key' in self.api_key: + if 'api_key' in self.api_key: auth['api_key'] = { 'type': 'api_key', 'in': 'header', 'key': 'api_key', 'value': self.get_api_key_with_prefix('api_key') } - if self.api_key and 'api_key_query' in self.api_key: + if 'api_key_query' in self.api_key: auth['api_key_query'] = { 'type': 'api_key', 'in': 'query', From e600852a56a1a549d4c984837973ea6958ebd1d3 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 10:04:41 -0800 Subject: [PATCH 14/20] [Python] fix unit test --- samples/client/petstore/python/tests/test_api_client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/client/petstore/python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py index c9b9edd86c44..ee90633f704f 100644 --- a/samples/client/petstore/python/tests/test_api_client.py +++ b/samples/client/petstore/python/tests/test_api_client.py @@ -29,7 +29,7 @@ def test_configuration(self): config = petstore_api.Configuration() config.api_key['api_key'] = '123456' - config.api_key_prefix['api_key'] = 'PREFIX' + config.api_key_prefix['api_key'] = 'PREFIX=' config.username = 'test_username' config.password = 'test_password' @@ -40,14 +40,14 @@ def test_configuration(self): client = petstore_api.ApiClient(config) # test prefix - self.assertEqual('PREFIX', client.configuration.api_key_prefix['api_key']) + self.assertEqual('PREFIX=', client.configuration.api_key_prefix['api_key']) # update parameters based on auth setting client.update_params_for_auth(header_params, query_params, auth_settings) # test api key auth self.assertEqual(header_params['test1'], 'value1') - self.assertEqual(header_params['api_key'], 'PREFIX 123456') + self.assertEqual(header_params['api_key'], 'PREFIX=123456') self.assertEqual(query_params['test2'], 'value2') # test basic auth From 221a87bd015bc1ba16f846e39d1e353620492d37 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 18:38:32 +0000 Subject: [PATCH 15/20] [Python] fix unit test for python experimental --- .../petstore/python-experimental/tests/test_api_client.py | 4 ++-- samples/client/petstore/python/tests/test_api_client.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/client/petstore/python-experimental/tests/test_api_client.py b/samples/client/petstore/python-experimental/tests/test_api_client.py index ad50f1862014..764afa6ae3c8 100644 --- a/samples/client/petstore/python-experimental/tests/test_api_client.py +++ b/samples/client/petstore/python-experimental/tests/test_api_client.py @@ -30,7 +30,7 @@ def test_configuration(self): config.host = 'http://localhost/' config.api_key['api_key'] = '123456' - config.api_key_prefix['api_key'] = 'PREFIX' + config.api_key_prefix['api_key'] = 'PREFIX ' config.username = 'test_username' config.password = 'test_password' @@ -41,7 +41,7 @@ def test_configuration(self): client = petstore_api.ApiClient(config) # test prefix - self.assertEqual('PREFIX', client.configuration.api_key_prefix['api_key']) + self.assertEqual('PREFIX ', client.configuration.api_key_prefix['api_key']) # update parameters based on auth setting client.update_params_for_auth(header_params, query_params, auth_settings) diff --git a/samples/client/petstore/python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py index ee90633f704f..11c4dec9de17 100644 --- a/samples/client/petstore/python/tests/test_api_client.py +++ b/samples/client/petstore/python/tests/test_api_client.py @@ -47,7 +47,7 @@ def test_configuration(self): # test api key auth self.assertEqual(header_params['test1'], 'value1') - self.assertEqual(header_params['api_key'], 'PREFIX=123456') + self.assertEqual(header_params['api_key'], 'PREFIX= 123456') self.assertEqual(query_params['test2'], 'value2') # test basic auth From f2d247b88f187c17f9d115006b14232a801d5207 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 18:56:10 +0000 Subject: [PATCH 16/20] [Python] fix unit test for python experimental --- samples/client/petstore/python/tests/test_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/client/petstore/python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py index 11c4dec9de17..ee90633f704f 100644 --- a/samples/client/petstore/python/tests/test_api_client.py +++ b/samples/client/petstore/python/tests/test_api_client.py @@ -47,7 +47,7 @@ def test_configuration(self): # test api key auth self.assertEqual(header_params['test1'], 'value1') - self.assertEqual(header_params['api_key'], 'PREFIX= 123456') + self.assertEqual(header_params['api_key'], 'PREFIX=123456') self.assertEqual(query_params['test2'], 'value2') # test basic auth From fd4a0e40fe262f278a5d2029f6f4fa8ecb4db708 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 26 Nov 2019 19:16:33 +0000 Subject: [PATCH 17/20] [Python] fix max length of line in python --- .../src/main/resources/python/configuration.mustache | 3 ++- .../petstore/python-asyncio/petstore_api/configuration.py | 3 ++- .../petstore/python-experimental/petstore_api/configuration.py | 3 ++- .../petstore/python-tornado/petstore_api/configuration.py | 3 ++- samples/client/petstore/python/petstore_api/configuration.py | 3 ++- .../client/petstore/python/petstore_api/configuration.py | 3 ++- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 0d5212edc11f..7a04767f7849 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -23,7 +23,8 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the OAS specification. + The dict key should be the name of the api key, as specified in the + OAS specification. The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index a02784da1b8c..ba1ccf1eb6f3 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -28,7 +28,8 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the OAS specification. + The dict key should be the name of the api key, as specified in the + OAS specification. The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 490b4d73c260..28a6a7e7339e 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -29,7 +29,8 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the OAS specification. + The dict key should be the name of the api key, as specified in the + OAS specification. The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 490b4d73c260..28a6a7e7339e 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -29,7 +29,8 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the OAS specification. + The dict key should be the name of the api key, as specified in the + OAS specification. The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 490b4d73c260..28a6a7e7339e 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -29,7 +29,8 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the OAS specification. + The dict key should be the name of the api key, as specified in the + OAS specification. The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index b0192a85b144..e0b751be65a7 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -29,7 +29,8 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the OAS specification. + The dict key should be the name of the api key, as specified in the + OAS specification. The dict value should be the value of the api key (i.e. the secret). :param api_key_prefix: Dict to store API prefix (e.g. Bearer) :param username: Username for HTTP basic authentication From eedcfeb46e75b2e23a5e32f612f3c80c5fbec4bf Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Mon, 2 Dec 2019 08:26:22 -0800 Subject: [PATCH 18/20] CSCvs20843: specify api_key as a dictionary argument --- .../resources/python/configuration.mustache | 32 ++++++++-------- .../petstore_api/configuration.py | 32 ++++++++-------- .../petstore_api/configuration.py | 32 ++++++++-------- .../tests/test_api_client.py | 22 +++++++++-- .../python-experimental/tests/test_pet_api.py | 37 ++++++++++--------- .../petstore_api/configuration.py | 32 ++++++++-------- .../python/petstore_api/configuration.py | 32 ++++++++-------- .../petstore/python/tests/test_api_client.py | 8 +--- .../python/tests/test_configuration.py | 1 - .../petstore/python/tests/test_pet_api.py | 6 ++- .../python/petstore_api/configuration.py | 32 ++++++++-------- 11 files changed, 134 insertions(+), 132 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 7a04767f7849..07d75647e80a 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -23,10 +23,12 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the - OAS specification. - The dict value should be the value of the api key (i.e. the secret). - :param api_key_prefix: Dict to store API prefix (e.g. Bearer) + Each entry in the dict specifies an API key. The dict key is the name + of the api key, as specified in the OAS specification. + The dict value is itself a dict that must have a 'value' entry. + The 'value' entry is required and specifies the api key secret. + The 'prefix' entry is optional and specifies an api key prefix when + generating the auth data. :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token @@ -43,15 +45,14 @@ class Configuration(object): You can programmatically set the cookie: conf = {{{packageName}}}.Configuration( - api_key={'JSESSIONID': 'abc123'} - api_key_prefix={'JSESSIONID': 'JSESSIONID='} + api_key={'JSESSIONID': {prefix='JSESSIONID=', value='abc123'}} ) The following cookie will be added to the HTTP request: Cookie: JSESSIONID=abc123 """ def __init__(self, host="{{{basePath}}}", - api_key=None, api_key_prefix=None, + api_key=None, username=None, password=None, access_token=None): """Constructor @@ -68,11 +69,6 @@ class Configuration(object): self.api_key = api_key """dict to store API key(s) """ - self.api_key_prefix = {} - if api_key_prefix: - self.api_key_prefix = api_key_prefix - """dict to store API prefix (e.g. Bearer) - """ self.refresh_api_key_hook = None """function hook to refresh API key if expired """ @@ -248,11 +244,13 @@ class Configuration(object): """ if self.refresh_api_key_hook is not None: self.refresh_api_key_hook(self) - key = self.api_key.get(identifier) - if key: - prefix = self.api_key_prefix.get(identifier) - if prefix: - return "%s%s" % (prefix, key) + key_entry = self.api_key.get(identifier) + if not isinstance(key_entry, dict): + raise Exception("The api_key attribute must be a dictionary") + if key_entry: + key = key_entry["value"] + if "prefix" in key_entry: + return "%s%s" % (key_entry["prefix"], key) else: return key diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index ba1ccf1eb6f3..e3acb62fd230 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -28,10 +28,12 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the - OAS specification. - The dict value should be the value of the api key (i.e. the secret). - :param api_key_prefix: Dict to store API prefix (e.g. Bearer) + Each entry in the dict specifies an API key. The dict key is the name + of the api key, as specified in the OAS specification. + The dict value is itself a dict that must have a 'value' entry. + The 'value' entry is required and specifies the api key secret. + The 'prefix' entry is optional and specifies an api key prefix when + generating the auth data. :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token @@ -48,15 +50,14 @@ class Configuration(object): You can programmatically set the cookie: conf = petstore_api.Configuration( - api_key={'JSESSIONID': 'abc123'} - api_key_prefix={'JSESSIONID': 'JSESSIONID='} + api_key={'JSESSIONID': {prefix='JSESSIONID=', value='abc123'}} ) The following cookie will be added to the HTTP request: Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, + api_key=None, username=None, password=None, access_token=None): """Constructor @@ -73,11 +74,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.api_key = api_key """dict to store API key(s) """ - self.api_key_prefix = {} - if api_key_prefix: - self.api_key_prefix = api_key_prefix - """dict to store API prefix (e.g. Bearer) - """ self.refresh_api_key_hook = None """function hook to refresh API key if expired """ @@ -242,11 +238,13 @@ def get_api_key_with_prefix(self, identifier): """ if self.refresh_api_key_hook is not None: self.refresh_api_key_hook(self) - key = self.api_key.get(identifier) - if key: - prefix = self.api_key_prefix.get(identifier) - if prefix: - return "%s%s" % (prefix, key) + key_entry = self.api_key.get(identifier) + if not isinstance(key_entry, dict): + raise Exception("The api_key attribute must be a dictionary") + if key_entry: + key = key_entry["value"] + if "prefix" in key_entry: + return "%s%s" % (key_entry["prefix"], key) else: return key diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 28a6a7e7339e..b8194838e3b3 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -29,10 +29,12 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the - OAS specification. - The dict value should be the value of the api key (i.e. the secret). - :param api_key_prefix: Dict to store API prefix (e.g. Bearer) + Each entry in the dict specifies an API key. The dict key is the name + of the api key, as specified in the OAS specification. + The dict value is itself a dict that must have a 'value' entry. + The 'value' entry is required and specifies the api key secret. + The 'prefix' entry is optional and specifies an api key prefix when + generating the auth data. :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token @@ -49,15 +51,14 @@ class Configuration(object): You can programmatically set the cookie: conf = petstore_api.Configuration( - api_key={'JSESSIONID': 'abc123'} - api_key_prefix={'JSESSIONID': 'JSESSIONID='} + api_key={'JSESSIONID': {prefix='JSESSIONID=', value='abc123'}} ) The following cookie will be added to the HTTP request: Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, + api_key=None, username=None, password=None, access_token=None): """Constructor @@ -74,11 +75,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.api_key = api_key """dict to store API key(s) """ - self.api_key_prefix = {} - if api_key_prefix: - self.api_key_prefix = api_key_prefix - """dict to store API prefix (e.g. Bearer) - """ self.refresh_api_key_hook = None """function hook to refresh API key if expired """ @@ -246,11 +242,13 @@ def get_api_key_with_prefix(self, identifier): """ if self.refresh_api_key_hook is not None: self.refresh_api_key_hook(self) - key = self.api_key.get(identifier) - if key: - prefix = self.api_key_prefix.get(identifier) - if prefix: - return "%s%s" % (prefix, key) + key_entry = self.api_key.get(identifier) + if not isinstance(key_entry, dict): + raise Exception("The api_key attribute must be a dictionary") + if key_entry: + key = key_entry["value"] + if "prefix" in key_entry: + return "%s%s" % (key_entry["prefix"], key) else: return key diff --git a/samples/client/petstore/python-experimental/tests/test_api_client.py b/samples/client/petstore/python-experimental/tests/test_api_client.py index 764afa6ae3c8..fd43d661a31c 100644 --- a/samples/client/petstore/python-experimental/tests/test_api_client.py +++ b/samples/client/petstore/python-experimental/tests/test_api_client.py @@ -29,8 +29,10 @@ def test_configuration(self): config = petstore_api.Configuration() config.host = 'http://localhost/' - config.api_key['api_key'] = '123456' - config.api_key_prefix['api_key'] = 'PREFIX ' + config.api_key['api_key'] = {'value': '123456', 'prefix': 'PREFIX'} + # api key prefix used to be set with the api_key_prefix attribute. + # Now the key prefix is set in the 'api_key' dictionary. + #config.api_key_prefix['api_key'] = 'PREFIX' config.username = 'test_username' config.password = 'test_password' @@ -41,20 +43,32 @@ def test_configuration(self): client = petstore_api.ApiClient(config) # test prefix - self.assertEqual('PREFIX ', client.configuration.api_key_prefix['api_key']) + self.assertEqual('PREFIX', client.configuration.api_key['api_key']['prefix']) # update parameters based on auth setting client.update_params_for_auth(header_params, query_params, auth_settings) # test api key auth self.assertEqual(header_params['test1'], 'value1') - self.assertEqual(header_params['api_key'], 'PREFIX 123456') + self.assertEqual(header_params['api_key'], 'PREFIX123456') self.assertEqual(query_params['test2'], 'value2') # test basic auth self.assertEqual('test_username', client.configuration.username) self.assertEqual('test_password', client.configuration.password) + # test api key without prefix + config.api_key['api_key'] = {'value': '123456'} + # update parameters based on auth setting + client.update_params_for_auth(header_params, query_params, auth_settings) + self.assertEqual(header_params['api_key'], '123456') + + # test api key with wrong type + config.api_key['api_key'] = '123456' + with self.assertRaises(Exception): + # update parameters based on auth setting + client.update_params_for_auth(header_params, query_params, auth_settings) + def test_select_header_accept(self): accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] accept = self.api_client.select_header_accept(accepts) diff --git a/samples/client/petstore/python-experimental/tests/test_pet_api.py b/samples/client/petstore/python-experimental/tests/test_pet_api.py index 90af52052228..a86d07e0e627 100644 --- a/samples/client/petstore/python-experimental/tests/test_pet_api.py +++ b/samples/client/petstore/python-experimental/tests/test_pet_api.py @@ -71,10 +71,10 @@ def request(self, *args, **kwargs): class PetApiTests(unittest.TestCase): def setUp(self): - self.config = Configuration() - self.config.host = HOST - self.config.access_token = 'ACCESS_TOKEN' - self.api_client = petstore_api.ApiClient(self.config) + config = Configuration() + config.host = HOST + config.access_token = 'ACCESS_TOKEN' + self.api_client = petstore_api.ApiClient(config) self.pet_api = petstore_api.PetApi(self.api_client) self.setUpModels() self.setUpFiles() @@ -117,23 +117,24 @@ def test_preload_content_flag(self): resp.release_conn() def test_config(self): - self.assertIsNotNone(self.config.get_host_settings()) - self.assertEquals(self.config.get_basic_auth_token(), + config = Configuration(host=HOST, access_token='ACCESS_TOKEN') + self.assertIsNotNone(config.get_host_settings()) + self.assertEquals(config.get_basic_auth_token(), urllib3.util.make_headers(basic_auth=":").get('authorization')) - self.assertEquals(len(self.config.auth_settings()), 1) - self.assertIn("petstore_auth", self.config.auth_settings().keys()) - self.config.username = "user" - self.config.password = "password" + self.assertEquals(len(config.auth_settings()), 1) + self.assertIn("petstore_auth", config.auth_settings().keys()) + config.username = "user" + config.password = "password" self.assertEquals( - self.config.get_basic_auth_token(), + config.get_basic_auth_token(), urllib3.util.make_headers(basic_auth="user:password").get('authorization')) - self.assertEquals(len(self.config.auth_settings()), 2) - self.assertIn("petstore_auth", self.config.auth_settings().keys()) - self.assertIn("http_basic_test", self.config.auth_settings().keys()) - self.config.username = None - self.config.password = None - self.assertEquals(len(self.config.auth_settings()), 1) - self.assertIn("petstore_auth", self.config.auth_settings().keys()) + self.assertEquals(len(config.auth_settings()), 2) + self.assertIn("petstore_auth", config.auth_settings().keys()) + self.assertIn("http_basic_test", config.auth_settings().keys()) + config.username = None + config.password = None + self.assertEquals(len(config.auth_settings()), 1) + self.assertIn("petstore_auth", config.auth_settings().keys()) def test_timeout(self): mock_pool = MockPoolManager(self) diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 28a6a7e7339e..b8194838e3b3 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -29,10 +29,12 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the - OAS specification. - The dict value should be the value of the api key (i.e. the secret). - :param api_key_prefix: Dict to store API prefix (e.g. Bearer) + Each entry in the dict specifies an API key. The dict key is the name + of the api key, as specified in the OAS specification. + The dict value is itself a dict that must have a 'value' entry. + The 'value' entry is required and specifies the api key secret. + The 'prefix' entry is optional and specifies an api key prefix when + generating the auth data. :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token @@ -49,15 +51,14 @@ class Configuration(object): You can programmatically set the cookie: conf = petstore_api.Configuration( - api_key={'JSESSIONID': 'abc123'} - api_key_prefix={'JSESSIONID': 'JSESSIONID='} + api_key={'JSESSIONID': {prefix='JSESSIONID=', value='abc123'}} ) The following cookie will be added to the HTTP request: Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, + api_key=None, username=None, password=None, access_token=None): """Constructor @@ -74,11 +75,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.api_key = api_key """dict to store API key(s) """ - self.api_key_prefix = {} - if api_key_prefix: - self.api_key_prefix = api_key_prefix - """dict to store API prefix (e.g. Bearer) - """ self.refresh_api_key_hook = None """function hook to refresh API key if expired """ @@ -246,11 +242,13 @@ def get_api_key_with_prefix(self, identifier): """ if self.refresh_api_key_hook is not None: self.refresh_api_key_hook(self) - key = self.api_key.get(identifier) - if key: - prefix = self.api_key_prefix.get(identifier) - if prefix: - return "%s%s" % (prefix, key) + key_entry = self.api_key.get(identifier) + if not isinstance(key_entry, dict): + raise Exception("The api_key attribute must be a dictionary") + if key_entry: + key = key_entry["value"] + if "prefix" in key_entry: + return "%s%s" % (key_entry["prefix"], key) else: return key diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 28a6a7e7339e..b8194838e3b3 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -29,10 +29,12 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the - OAS specification. - The dict value should be the value of the api key (i.e. the secret). - :param api_key_prefix: Dict to store API prefix (e.g. Bearer) + Each entry in the dict specifies an API key. The dict key is the name + of the api key, as specified in the OAS specification. + The dict value is itself a dict that must have a 'value' entry. + The 'value' entry is required and specifies the api key secret. + The 'prefix' entry is optional and specifies an api key prefix when + generating the auth data. :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token @@ -49,15 +51,14 @@ class Configuration(object): You can programmatically set the cookie: conf = petstore_api.Configuration( - api_key={'JSESSIONID': 'abc123'} - api_key_prefix={'JSESSIONID': 'JSESSIONID='} + api_key={'JSESSIONID': {prefix='JSESSIONID=', value='abc123'}} ) The following cookie will be added to the HTTP request: Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, + api_key=None, username=None, password=None, access_token=None): """Constructor @@ -74,11 +75,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.api_key = api_key """dict to store API key(s) """ - self.api_key_prefix = {} - if api_key_prefix: - self.api_key_prefix = api_key_prefix - """dict to store API prefix (e.g. Bearer) - """ self.refresh_api_key_hook = None """function hook to refresh API key if expired """ @@ -246,11 +242,13 @@ def get_api_key_with_prefix(self, identifier): """ if self.refresh_api_key_hook is not None: self.refresh_api_key_hook(self) - key = self.api_key.get(identifier) - if key: - prefix = self.api_key_prefix.get(identifier) - if prefix: - return "%s%s" % (prefix, key) + key_entry = self.api_key.get(identifier) + if not isinstance(key_entry, dict): + raise Exception("The api_key attribute must be a dictionary") + if key_entry: + key = key_entry["value"] + if "prefix" in key_entry: + return "%s%s" % (key_entry["prefix"], key) else: return key diff --git a/samples/client/petstore/python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py index ee90633f704f..5d2cea35a128 100644 --- a/samples/client/petstore/python/tests/test_api_client.py +++ b/samples/client/petstore/python/tests/test_api_client.py @@ -28,8 +28,7 @@ def setUp(self): def test_configuration(self): config = petstore_api.Configuration() - config.api_key['api_key'] = '123456' - config.api_key_prefix['api_key'] = 'PREFIX=' + config.api_key['api_key'] = { 'value': '123456', 'prefix': 'PREFIX' } config.username = 'test_username' config.password = 'test_password' @@ -39,15 +38,12 @@ def test_configuration(self): client = petstore_api.ApiClient(config) - # test prefix - self.assertEqual('PREFIX=', client.configuration.api_key_prefix['api_key']) - # update parameters based on auth setting client.update_params_for_auth(header_params, query_params, auth_settings) # test api key auth self.assertEqual(header_params['test1'], 'value1') - self.assertEqual(header_params['api_key'], 'PREFIX=123456') + self.assertEqual(header_params['api_key'], 'PREFIX123456') self.assertEqual(query_params['test2'], 'value2') # test basic auth diff --git a/samples/client/petstore/python/tests/test_configuration.py b/samples/client/petstore/python/tests/test_configuration.py index f1074b17e70a..7ec03789a887 100644 --- a/samples/client/petstore/python/tests/test_configuration.py +++ b/samples/client/petstore/python/tests/test_configuration.py @@ -29,7 +29,6 @@ def testConfiguration(self): c1 = petstore_api.Configuration() c2 = petstore_api.Configuration() assert id(c1.api_key) != id(c2.api_key) - assert id(c1.api_key_prefix) != id(c2.api_key_prefix) if __name__ == '__main__': diff --git a/samples/client/petstore/python/tests/test_pet_api.py b/samples/client/petstore/python/tests/test_pet_api.py index 2e20059f22a6..f50980afae7f 100644 --- a/samples/client/petstore/python/tests/test_pet_api.py +++ b/samples/client/petstore/python/tests/test_pet_api.py @@ -13,6 +13,7 @@ import os import unittest +import pprint import petstore_api from petstore_api import Configuration @@ -223,7 +224,10 @@ def test_find_pets_by_status(self): def test_find_pets_by_tags(self): self.pet_api.add_pet(self.pet) - + with open("file_out.txt", "w") as fout: + pp = pprint.PrettyPrinter(indent=4, stream=fout) + pp.pprint(self.pet) + pp.pprint(self.pet_api.find_pets_by_tags(tags=[self.tag.name])) self.assertIn( self.pet.id, list(map(lambda x: getattr(x, 'id'), self.pet_api.find_pets_by_tags(tags=[self.tag.name]))) diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index e0b751be65a7..ac7b3c6ce9ca 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -29,10 +29,12 @@ class Configuration(object): :param host: Base url :param api_key: Dict to store API key(s). - The dict key should be the name of the api key, as specified in the - OAS specification. - The dict value should be the value of the api key (i.e. the secret). - :param api_key_prefix: Dict to store API prefix (e.g. Bearer) + Each entry in the dict specifies an API key. The dict key is the name + of the api key, as specified in the OAS specification. + The dict value is itself a dict that must have a 'value' entry. + The 'value' entry is required and specifies the api key secret. + The 'prefix' entry is optional and specifies an api key prefix when + generating the auth data. :param username: Username for HTTP basic authentication :param password: Password for HTTP basic authentication :param access_token: The OAuth2 access token @@ -49,15 +51,14 @@ class Configuration(object): You can programmatically set the cookie: conf = petstore_api.Configuration( - api_key={'JSESSIONID': 'abc123'} - api_key_prefix={'JSESSIONID': 'JSESSIONID='} + api_key={'JSESSIONID': {prefix='JSESSIONID=', value='abc123'}} ) The following cookie will be added to the HTTP request: Cookie: JSESSIONID=abc123 """ def __init__(self, host="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, + api_key=None, username=None, password=None, access_token=None): """Constructor @@ -74,11 +75,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.api_key = api_key """dict to store API key(s) """ - self.api_key_prefix = {} - if api_key_prefix: - self.api_key_prefix = api_key_prefix - """dict to store API prefix (e.g. Bearer) - """ self.refresh_api_key_hook = None """function hook to refresh API key if expired """ @@ -246,11 +242,13 @@ def get_api_key_with_prefix(self, identifier): """ if self.refresh_api_key_hook is not None: self.refresh_api_key_hook(self) - key = self.api_key.get(identifier) - if key: - prefix = self.api_key_prefix.get(identifier) - if prefix: - return "%s%s" % (prefix, key) + key_entry = self.api_key.get(identifier) + if not isinstance(key_entry, dict): + raise Exception("The api_key attribute must be a dictionary") + if key_entry: + key = key_entry["value"] + if "prefix" in key_entry: + return "%s%s" % (key_entry["prefix"], key) else: return key From 74b2393d10c3aec9c2eee5a02f289254c83755f6 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 3 Dec 2019 09:54:04 -0800 Subject: [PATCH 19/20] Remove pprint that was used for debug purpose --- .../petstore/python-experimental/tests/test_api_client.py | 6 +++--- samples/client/petstore/python/tests/test_pet_api.py | 5 ----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/samples/client/petstore/python-experimental/tests/test_api_client.py b/samples/client/petstore/python-experimental/tests/test_api_client.py index fd43d661a31c..5e3cdbd174ea 100644 --- a/samples/client/petstore/python-experimental/tests/test_api_client.py +++ b/samples/client/petstore/python-experimental/tests/test_api_client.py @@ -29,10 +29,10 @@ def test_configuration(self): config = petstore_api.Configuration() config.host = 'http://localhost/' - config.api_key['api_key'] = {'value': '123456', 'prefix': 'PREFIX'} + config.api_key['api_key'] = {'value': '123456', 'prefix': 'PREFIX='} # api key prefix used to be set with the api_key_prefix attribute. # Now the key prefix is set in the 'api_key' dictionary. - #config.api_key_prefix['api_key'] = 'PREFIX' + #config.api_key_prefix['api_key'] = 'PREFIX=' config.username = 'test_username' config.password = 'test_password' @@ -43,7 +43,7 @@ def test_configuration(self): client = petstore_api.ApiClient(config) # test prefix - self.assertEqual('PREFIX', client.configuration.api_key['api_key']['prefix']) + self.assertEqual('PREFIX=', client.configuration.api_key['api_key']['prefix']) # update parameters based on auth setting client.update_params_for_auth(header_params, query_params, auth_settings) diff --git a/samples/client/petstore/python/tests/test_pet_api.py b/samples/client/petstore/python/tests/test_pet_api.py index f50980afae7f..6ec416885dce 100644 --- a/samples/client/petstore/python/tests/test_pet_api.py +++ b/samples/client/petstore/python/tests/test_pet_api.py @@ -13,7 +13,6 @@ import os import unittest -import pprint import petstore_api from petstore_api import Configuration @@ -224,10 +223,6 @@ def test_find_pets_by_status(self): def test_find_pets_by_tags(self): self.pet_api.add_pet(self.pet) - with open("file_out.txt", "w") as fout: - pp = pprint.PrettyPrinter(indent=4, stream=fout) - pp.pprint(self.pet) - pp.pprint(self.pet_api.find_pets_by_tags(tags=[self.tag.name])) self.assertIn( self.pet.id, list(map(lambda x: getattr(x, 'id'), self.pet_api.find_pets_by_tags(tags=[self.tag.name]))) From 8bdf9f372df5a44f29ed32110949e3cb061500f9 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Tue, 3 Dec 2019 10:13:17 -0800 Subject: [PATCH 20/20] Fix unit test --- .../petstore/python-experimental/tests/test_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/client/petstore/python-experimental/tests/test_api_client.py b/samples/client/petstore/python-experimental/tests/test_api_client.py index 5e3cdbd174ea..cd993eb760a1 100644 --- a/samples/client/petstore/python-experimental/tests/test_api_client.py +++ b/samples/client/petstore/python-experimental/tests/test_api_client.py @@ -50,7 +50,7 @@ def test_configuration(self): # test api key auth self.assertEqual(header_params['test1'], 'value1') - self.assertEqual(header_params['api_key'], 'PREFIX123456') + self.assertEqual(header_params['api_key'], 'PREFIX=123456') self.assertEqual(query_params['test2'], 'value2') # test basic auth