Skip to content

Commit 84be144

Browse files
Merge pull request #20 from Mastercard/fix/openapi-6-does-not-work
Add openapi generator 6 support
2 parents 5f8ecef + df80e16 commit 84be144

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

client_encryption/api_encryption.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ def field_encryption_call_api(self, func):
3434

3535
@wraps(func)
3636
def call_api_function(*args, **kwargs):
37-
check_type = inspect.signature(func.__self__.call_api).parameters.get("_check_type") is None
38-
if check_type:
37+
original_parameters = inspect.signature(func.__self__.call_api).parameters
38+
check_type_is_none = original_parameters.get("_check_type") is None
39+
preload_content_is_not_none = original_parameters.get("_preload_content") is not None
40+
if check_type_is_none and preload_content_is_not_none:
3941
kwargs["_preload_content"] = False # version 4.3.1
4042
return func(*args, **kwargs)
4143

@@ -75,10 +77,17 @@ def _encrypt_payload(self, headers, body):
7577

7678
conf = self._encryption_conf
7779

78-
if type(conf) is FieldLevelEncryptionConfig:
79-
return self.encrypt_field_level_payload(headers, conf, body)
80-
else:
81-
return self.encrypt_jwe_payload(conf, body)
80+
encrypted_payload = self.encrypt_field_level_payload(headers, conf, body) if type(
81+
conf) is FieldLevelEncryptionConfig else self.encrypt_jwe_payload(conf, body)
82+
83+
# convert the encrypted_payload to the same data type as the input body
84+
if isinstance(body, str):
85+
return json.dumps(encrypted_payload)
86+
87+
if isinstance(body, bytes):
88+
return json.dumps(encrypted_payload).encode("utf-8")
89+
90+
return encrypted_payload
8291

8392
def _decrypt_payload(self, headers, body):
8493
"""Encryption enforcement based on configuration - decrypt using session key params from header or body"""

client_encryption/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
__version__ = "1.7.0"
3+
__version__ = "1.8.0"

tests/test_api_encryption.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ def test_ApiEncryption_with_config_as_dict(self, FieldLevelEncryptionConfig):
3939
def test_ApiEncryption_fail_with_config_as_string(self):
4040
self.assertRaises(FileNotFoundError, to_test.ApiEncryption, "this is not accepted")
4141

42+
def test_encrypt_payload_returns_same_data_type_as_input(self):
43+
api_encryption = to_test.ApiEncryption(self._json_config)
44+
45+
test_headers = {"Content-Type": "application/json"}
46+
47+
body = {
48+
"data": {
49+
"secret1": "test",
50+
"secret2": "secret"
51+
},
52+
"encryptedData": {}
53+
}
54+
55+
encrypted = api_encryption._encrypt_payload(body=body, headers=test_headers)
56+
self.assertIsInstance(encrypted, dict)
57+
58+
encrypted = api_encryption._encrypt_payload(body=json.dumps(body), headers=test_headers)
59+
self.assertIsInstance(encrypted, str)
60+
61+
encrypted = api_encryption._encrypt_payload(body=json.dumps(body).encode("utf-8"), headers=test_headers)
62+
self.assertIsInstance(encrypted, bytes)
63+
4264
def test_encrypt_payload_with_params_in_body(self):
4365
api_encryption = to_test.ApiEncryption(self._json_config)
4466

0 commit comments

Comments
 (0)