Skip to content

Commit 36bc084

Browse files
[python] Set API key cookie as key-value pair (#24149)
* [python] Set API key cookie as key-value pair * [python] Account for setting multiple cookies * Quote API cookie value to avoid possible parsing issues Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * [python] Stringify and quote API key cookie value to avoid possible parsing errors * [python] Limit cookie value URL-encoding to spaces and semicolons * [python] Quote special cookie characters other than special base64 characters * [python] Quote special cookie characters using double quotes --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
1 parent 7eb3dd0 commit 36bc084

8 files changed

Lines changed: 80 additions & 8 deletions

File tree

modules/openapi-generator/src/main/resources/python/api_client.mustache

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,16 @@ class ApiClient:
680680
:param auth_setting: auth settings for the endpoint
681681
"""
682682
if auth_setting['in'] == 'cookie':
683-
headers['Cookie'] = auth_setting['value']
683+
if not 'Cookie' in headers:
684+
headers['Cookie'] = ""
685+
else:
686+
headers['Cookie'] += "; "
687+
# Account for cookie value containing spaces and special characters
688+
cookie_value = str(auth_setting['value'])
689+
if not re.match("^\".*\"$", cookie_value):
690+
cookie_value = cookie_value.replace("\"", "\\\"")
691+
cookie_value = f"\"{cookie_value}\""
692+
headers['Cookie'] += f"{auth_setting['key']}={cookie_value}"
684693
elif auth_setting['in'] == 'header':
685694
if auth_setting['type'] != 'http-signature':
686695
headers[auth_setting['key']] = auth_setting['value']

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,16 @@ def _apply_auth_params(
673673
:param auth_setting: auth settings for the endpoint
674674
"""
675675
if auth_setting['in'] == 'cookie':
676-
headers['Cookie'] = auth_setting['value']
676+
if not 'Cookie' in headers:
677+
headers['Cookie'] = ""
678+
else:
679+
headers['Cookie'] += "; "
680+
# Account for cookie value containing spaces and special characters
681+
cookie_value = str(auth_setting['value'])
682+
if not re.match("^\".*\"$", cookie_value):
683+
cookie_value = cookie_value.replace("\"", "\\\"")
684+
cookie_value = f"\"{cookie_value}\""
685+
headers['Cookie'] += f"{auth_setting['key']}={cookie_value}"
677686
elif auth_setting['in'] == 'header':
678687
if auth_setting['type'] != 'http-signature':
679688
headers[auth_setting['key']] = auth_setting['value']

samples/client/echo_api/python/openapi_client/api_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,16 @@ def _apply_auth_params(
673673
:param auth_setting: auth settings for the endpoint
674674
"""
675675
if auth_setting['in'] == 'cookie':
676-
headers['Cookie'] = auth_setting['value']
676+
if not 'Cookie' in headers:
677+
headers['Cookie'] = ""
678+
else:
679+
headers['Cookie'] += "; "
680+
# Account for cookie value containing spaces and special characters
681+
cookie_value = str(auth_setting['value'])
682+
if not re.match("^\".*\"$", cookie_value):
683+
cookie_value = cookie_value.replace("\"", "\\\"")
684+
cookie_value = f"\"{cookie_value}\""
685+
headers['Cookie'] += f"{auth_setting['key']}={cookie_value}"
677686
elif auth_setting['in'] == 'header':
678687
if auth_setting['type'] != 'http-signature':
679688
headers[auth_setting['key']] = auth_setting['value']

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,16 @@ def _apply_auth_params(
675675
:param auth_setting: auth settings for the endpoint
676676
"""
677677
if auth_setting['in'] == 'cookie':
678-
headers['Cookie'] = auth_setting['value']
678+
if not 'Cookie' in headers:
679+
headers['Cookie'] = ""
680+
else:
681+
headers['Cookie'] += "; "
682+
# Account for cookie value containing spaces and special characters
683+
cookie_value = str(auth_setting['value'])
684+
if not re.match("^\".*\"$", cookie_value):
685+
cookie_value = cookie_value.replace("\"", "\\\"")
686+
cookie_value = f"\"{cookie_value}\""
687+
headers['Cookie'] += f"{auth_setting['key']}={cookie_value}"
679688
elif auth_setting['in'] == 'header':
680689
if auth_setting['type'] != 'http-signature':
681690
headers[auth_setting['key']] = auth_setting['value']

samples/openapi3/client/petstore/python-httpx-sync/petstore_api/api_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,16 @@ def _apply_auth_params(
675675
:param auth_setting: auth settings for the endpoint
676676
"""
677677
if auth_setting['in'] == 'cookie':
678-
headers['Cookie'] = auth_setting['value']
678+
if not 'Cookie' in headers:
679+
headers['Cookie'] = ""
680+
else:
681+
headers['Cookie'] += "; "
682+
# Account for cookie value containing spaces and special characters
683+
cookie_value = str(auth_setting['value'])
684+
if not re.match("^\".*\"$", cookie_value):
685+
cookie_value = cookie_value.replace("\"", "\\\"")
686+
cookie_value = f"\"{cookie_value}\""
687+
headers['Cookie'] += f"{auth_setting['key']}={cookie_value}"
679688
elif auth_setting['in'] == 'header':
680689
if auth_setting['type'] != 'http-signature':
681690
headers[auth_setting['key']] = auth_setting['value']

samples/openapi3/client/petstore/python-httpx/petstore_api/api_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,16 @@ def _apply_auth_params(
675675
:param auth_setting: auth settings for the endpoint
676676
"""
677677
if auth_setting['in'] == 'cookie':
678-
headers['Cookie'] = auth_setting['value']
678+
if not 'Cookie' in headers:
679+
headers['Cookie'] = ""
680+
else:
681+
headers['Cookie'] += "; "
682+
# Account for cookie value containing spaces and special characters
683+
cookie_value = str(auth_setting['value'])
684+
if not re.match("^\".*\"$", cookie_value):
685+
cookie_value = cookie_value.replace("\"", "\\\"")
686+
cookie_value = f"\"{cookie_value}\""
687+
headers['Cookie'] += f"{auth_setting['key']}={cookie_value}"
679688
elif auth_setting['in'] == 'header':
680689
if auth_setting['type'] != 'http-signature':
681690
headers[auth_setting['key']] = auth_setting['value']

samples/openapi3/client/petstore/python-lazyImports/petstore_api/api_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,16 @@ def _apply_auth_params(
672672
:param auth_setting: auth settings for the endpoint
673673
"""
674674
if auth_setting['in'] == 'cookie':
675-
headers['Cookie'] = auth_setting['value']
675+
if not 'Cookie' in headers:
676+
headers['Cookie'] = ""
677+
else:
678+
headers['Cookie'] += "; "
679+
# Account for cookie value containing spaces and special characters
680+
cookie_value = str(auth_setting['value'])
681+
if not re.match("^\".*\"$", cookie_value):
682+
cookie_value = cookie_value.replace("\"", "\\\"")
683+
cookie_value = f"\"{cookie_value}\""
684+
headers['Cookie'] += f"{auth_setting['key']}={cookie_value}"
676685
elif auth_setting['in'] == 'header':
677686
if auth_setting['type'] != 'http-signature':
678687
headers[auth_setting['key']] = auth_setting['value']

samples/openapi3/client/petstore/python/petstore_api/api_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,16 @@ def _apply_auth_params(
672672
:param auth_setting: auth settings for the endpoint
673673
"""
674674
if auth_setting['in'] == 'cookie':
675-
headers['Cookie'] = auth_setting['value']
675+
if not 'Cookie' in headers:
676+
headers['Cookie'] = ""
677+
else:
678+
headers['Cookie'] += "; "
679+
# Account for cookie value containing spaces and special characters
680+
cookie_value = str(auth_setting['value'])
681+
if not re.match("^\".*\"$", cookie_value):
682+
cookie_value = cookie_value.replace("\"", "\\\"")
683+
cookie_value = f"\"{cookie_value}\""
684+
headers['Cookie'] += f"{auth_setting['key']}={cookie_value}"
676685
elif auth_setting['in'] == 'header':
677686
if auth_setting['type'] != 'http-signature':
678687
headers[auth_setting['key']] = auth_setting['value']

0 commit comments

Comments
 (0)