Skip to content

Commit

Permalink
[Python][Client] Allow all content-types with text/ prefix (OpenAPI…
Browse files Browse the repository at this point in the history
…Tools#19802)

* ODM-12108: allow all content-types with text/ prefix

* ODM-12108: Update deserialization tests

---------

Co-authored-by: Oleg Kunitsyn <[email protected]>
  • Loading branch information
genestack-okunitsyn and Oleg Kunitsyn authored Oct 8, 2024
1 parent 7d1e999 commit 4ff8c3a
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class ApiClient:
data = ""
else:
data = json.loads(response_text)
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class RESTClientObject:
headers=headers,
preload_content=False
)
elif headers['Content-Type'] == 'text/plain' and isinstance(body, bool):
elif headers['Content-Type'].startswith('text/') and isinstance(body, bool):
request_body = "true" if body else "false"
r = self.pool_manager.request(
method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
data = ""
else:
data = json.loads(response_text)
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def request(
headers=headers,
preload_content=False
)
elif headers['Content-Type'] == 'text/plain' and isinstance(body, bool):
elif headers['Content-Type'].startswith('text/') and isinstance(body, bool):
request_body = "true" if body else "false"
r = self.pool_manager.request(
method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
data = ""
else:
data = json.loads(response_text)
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
2 changes: 1 addition & 1 deletion samples/client/echo_api/python/openapi_client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def request(
headers=headers,
preload_content=False
)
elif headers['Content-Type'] == 'text/plain' and isinstance(body, bool):
elif headers['Content-Type'].startswith('text/') and isinstance(body, bool):
request_body = "true" if body else "false"
r = self.pool_manager.request(
method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
data = ""
else:
data = json.loads(response_text)
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
data = ""
else:
data = json.loads(response_text)
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def request(
headers=headers,
preload_content=False
)
elif headers['Content-Type'] == 'text/plain' and isinstance(body, bool):
elif headers['Content-Type'].startswith('text/') and isinstance(body, bool):
request_body = "true" if body else "false"
r = self.pool_manager.request(
method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,18 @@ def test_deserialize_content_type(self):

deserialized = self.deserialize(response, "str", 'text/plain')
self.assertTrue(isinstance(deserialized, str))


deserialized = self.deserialize(response, "str", 'text/csv')
self.assertTrue(isinstance(deserialized, str))

deserialized = self.deserialize(response, "Dict[str, str]", 'APPLICATION/JSON')
self.assertTrue(isinstance(deserialized, dict))


with self.assertRaises(petstore_api.ApiException) as cm:
deserialized = self.deserialize(response, "str", 'text')

with self.assertRaises(petstore_api.ApiException) as cm:
deserialized = self.deserialize(response, "str", 'text/html')
deserialized = self.deserialize(response, "str", 'text/n0t-exist!ng')

with self.assertRaises(petstore_api.ApiException) as cm:
deserialized = self.deserialize(response, "Dict[str, str]", 'application/jsonnnnn')
Expand Down

0 comments on commit 4ff8c3a

Please sign in to comment.