Skip to content

Commit 69cce24

Browse files
authored
[python] fix content_type deserialize (#19317)
* [Python] fix: #19285 * [python] update sample * [python] add test * [python] remove test
1 parent 7a7c8c1 commit 69cce24

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,12 @@ class ApiClient:
412412
data = json.loads(response_text)
413413
except ValueError:
414414
data = response_text
415-
elif content_type.startswith("application/json"):
415+
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
416416
if response_text == "":
417417
data = ""
418418
else:
419419
data = json.loads(response_text)
420-
elif content_type.startswith("text/plain"):
420+
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
421421
data = response_text
422422
else:
423423
raise ApiException(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
405405
data = json.loads(response_text)
406406
except ValueError:
407407
data = response_text
408-
elif content_type.startswith("application/json"):
408+
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
409409
if response_text == "":
410410
data = ""
411411
else:
412412
data = json.loads(response_text)
413-
elif content_type.startswith("text/plain"):
413+
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
414414
data = response_text
415415
else:
416416
raise ApiException(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
405405
data = json.loads(response_text)
406406
except ValueError:
407407
data = response_text
408-
elif content_type.startswith("application/json"):
408+
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
409409
if response_text == "":
410410
data = ""
411411
else:
412412
data = json.loads(response_text)
413-
elif content_type.startswith("text/plain"):
413+
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
414414
data = response_text
415415
else:
416416
raise ApiException(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
407407
data = json.loads(response_text)
408408
except ValueError:
409409
data = response_text
410-
elif content_type.startswith("application/json"):
410+
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
411411
if response_text == "":
412412
data = ""
413413
else:
414414
data = json.loads(response_text)
415-
elif content_type.startswith("text/plain"):
415+
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
416416
data = response_text
417417
else:
418418
raise ApiException(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
404404
data = json.loads(response_text)
405405
except ValueError:
406406
data = response_text
407-
elif content_type.startswith("application/json"):
407+
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
408408
if response_text == "":
409409
data = ""
410410
else:
411411
data = json.loads(response_text)
412-
elif content_type.startswith("text/plain"):
412+
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
413413
data = response_text
414414
else:
415415
raise ApiException(

samples/openapi3/client/petstore/python/tests/test_deserialization.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,35 @@ def test_deserialize_animal(self):
301301
self.assertEqual(deserialized.class_name, "Cat")
302302
self.assertEqual(deserialized.declawed, True)
303303
self.assertEqual(deserialized.to_json(), '{"className": "Cat", "color": "red", "declawed": true}')
304+
305+
def test_deserialize_content_type(self):
306+
307+
response = json.dumps({"a": "a"})
308+
309+
deserialized = self.deserialize(response, "Dict[str, str]", 'application/json')
310+
self.assertTrue(isinstance(deserialized, dict))
311+
312+
313+
deserialized = self.deserialize(response, "Dict[str, str]", 'application/vnd.api+json')
314+
self.assertTrue(isinstance(deserialized, dict))
315+
316+
317+
deserialized = self.deserialize(response, "Dict[str, str]", 'application/json; charset=utf-8')
318+
self.assertTrue(isinstance(deserialized, dict))
319+
320+
deserialized = self.deserialize(response, "Dict[str, str]", 'application/vnd.api+json; charset=utf-8')
321+
self.assertTrue(isinstance(deserialized, dict))
322+
323+
deserialized = self.deserialize(response, "str", 'text/plain')
324+
self.assertTrue(isinstance(deserialized, str))
325+
326+
deserialized = self.deserialize(response, "Dict[str, str]", 'APPLICATION/JSON')
327+
self.assertTrue(isinstance(deserialized, dict))
328+
329+
with self.assertRaises(petstore_api.ApiException) as cm:
330+
deserialized = self.deserialize(response, "str", 'text/html')
331+
332+
with self.assertRaises(petstore_api.ApiException) as cm:
333+
deserialized = self.deserialize(response, "Dict[str, str]", 'application/jsonnnnn')
334+
335+

0 commit comments

Comments
 (0)