[python-experimental] adds and uses regex patterns for json + filename detection (#13357)

* Unit test sample regenerated

* Reverts version files
This commit is contained in:
Justin Black 2022-09-05 11:03:57 -07:00 committed by GitHub
parent cb8d9d5bfe
commit f139c090e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 51 deletions

View File

@ -21,7 +21,7 @@ Python {{generatorLanguageVersion}}
v3.9 is needed so one can combine classmethod and property decorators to define v3.9 is needed so one can combine classmethod and property decorators to define
object schema properties as classes object schema properties as classes
## Migration from other generators like python and python-experimental ## Migration from other generators like python and python-legacy
### Changes ### Changes
1. This generator uses spec case for all (object) property names and parameter names. 1. This generator uses spec case for all (object) property names and parameter names.

View File

@ -796,24 +796,25 @@ class ApiResponseWithoutDeserialization(ApiResponse):
class JSONDetector: class JSONDetector:
@staticmethod
def _content_type_is_json(content_type: str) -> bool:
content_type_piece = content_type
if ';' in content_type:
# application/json; charset=UTF-8
content_type_piece = content_type.split(';')[0]
elif '-' in content_type:
""" """
Works for:
application/json
application/json; charset=UTF-8
application/json-patch+json application/json-patch+json
application/json-seq application/geo+json
""" """
content_type_piece = content_type.split('-')[0] __json_content_type_pattern = re.compile("application/[^+]*[+]?(json);?.*")
if content_type_piece == 'application/json':
@classmethod
def _content_type_is_json(cls, content_type: str) -> bool:
if cls.__json_content_type_pattern.match(content_type):
return True return True
return False return False
class OpenApiResponse(JSONDetector): class OpenApiResponse(JSONDetector):
__filename_content_disposition_pattern = re.compile('filename="(.+?)"')
def __init__( def __init__(
self, self,
response_cls: typing.Type[ApiResponse] = ApiResponse, response_cls: typing.Type[ApiResponse] = ApiResponse,
@ -831,11 +832,11 @@ class OpenApiResponse(JSONDetector):
# python must be >= 3.9 so we can pass in bytes into json.loads # python must be >= 3.9 so we can pass in bytes into json.loads
return json.loads(response.data) return json.loads(response.data)
@staticmethod @classmethod
def __file_name_from_content_disposition(content_disposition: typing.Optional[str]) -> typing.Optional[str]: def __file_name_from_content_disposition(cls, content_disposition: typing.Optional[str]) -> typing.Optional[str]:
if content_disposition is None: if content_disposition is None:
return None return None
match = re.search('filename="(.+?)"', content_disposition) match = cls.__filename_content_disposition_pattern.search(content_disposition)
if not match: if not match:
return None return None
return match.group(1) return match.group(1)

View File

@ -13,7 +13,7 @@ Python >=3.9
v3.9 is needed so one can combine classmethod and property decorators to define v3.9 is needed so one can combine classmethod and property decorators to define
object schema properties as classes object schema properties as classes
## Migration from other generators like python and python-experimental ## Migration from other generators like python and python-legacy
### Changes ### Changes
1. This generator uses spec case for all (object) property names and parameter names. 1. This generator uses spec case for all (object) property names and parameter names.

View File

@ -800,24 +800,25 @@ class ApiResponseWithoutDeserialization(ApiResponse):
class JSONDetector: class JSONDetector:
@staticmethod
def _content_type_is_json(content_type: str) -> bool:
content_type_piece = content_type
if ';' in content_type:
# application/json; charset=UTF-8
content_type_piece = content_type.split(';')[0]
elif '-' in content_type:
""" """
Works for:
application/json
application/json; charset=UTF-8
application/json-patch+json application/json-patch+json
application/json-seq application/geo+json
""" """
content_type_piece = content_type.split('-')[0] __json_content_type_pattern = re.compile("application/[^+]*[+]?(json);?.*")
if content_type_piece == 'application/json':
@classmethod
def _content_type_is_json(cls, content_type: str) -> bool:
if cls.__json_content_type_pattern.match(content_type):
return True return True
return False return False
class OpenApiResponse(JSONDetector): class OpenApiResponse(JSONDetector):
__filename_content_disposition_pattern = re.compile('filename="(.+?)"')
def __init__( def __init__(
self, self,
response_cls: typing.Type[ApiResponse] = ApiResponse, response_cls: typing.Type[ApiResponse] = ApiResponse,
@ -835,11 +836,11 @@ class OpenApiResponse(JSONDetector):
# python must be >= 3.9 so we can pass in bytes into json.loads # python must be >= 3.9 so we can pass in bytes into json.loads
return json.loads(response.data) return json.loads(response.data)
@staticmethod @classmethod
def __file_name_from_content_disposition(content_disposition: typing.Optional[str]) -> typing.Optional[str]: def __file_name_from_content_disposition(cls, content_disposition: typing.Optional[str]) -> typing.Optional[str]:
if content_disposition is None: if content_disposition is None:
return None return None
match = re.search('filename="(.+?)"', content_disposition) match = cls.__filename_content_disposition_pattern.search(content_disposition)
if not match: if not match:
return None return None
return match.group(1) return match.group(1)

View File

@ -13,7 +13,7 @@ Python >=3.9
v3.9 is needed so one can combine classmethod and property decorators to define v3.9 is needed so one can combine classmethod and property decorators to define
object schema properties as classes object schema properties as classes
## Migration from other generators like python and python-experimental ## Migration from other generators like python and python-legacy
### Changes ### Changes
1. This generator uses spec case for all (object) property names and parameter names. 1. This generator uses spec case for all (object) property names and parameter names.

View File

@ -800,24 +800,25 @@ class ApiResponseWithoutDeserialization(ApiResponse):
class JSONDetector: class JSONDetector:
@staticmethod
def _content_type_is_json(content_type: str) -> bool:
content_type_piece = content_type
if ';' in content_type:
# application/json; charset=UTF-8
content_type_piece = content_type.split(';')[0]
elif '-' in content_type:
""" """
Works for:
application/json
application/json; charset=UTF-8
application/json-patch+json application/json-patch+json
application/json-seq application/geo+json
""" """
content_type_piece = content_type.split('-')[0] __json_content_type_pattern = re.compile("application/[^+]*[+]?(json);?.*")
if content_type_piece == 'application/json':
@classmethod
def _content_type_is_json(cls, content_type: str) -> bool:
if cls.__json_content_type_pattern.match(content_type):
return True return True
return False return False
class OpenApiResponse(JSONDetector): class OpenApiResponse(JSONDetector):
__filename_content_disposition_pattern = re.compile('filename="(.+?)"')
def __init__( def __init__(
self, self,
response_cls: typing.Type[ApiResponse] = ApiResponse, response_cls: typing.Type[ApiResponse] = ApiResponse,
@ -835,11 +836,11 @@ class OpenApiResponse(JSONDetector):
# python must be >= 3.9 so we can pass in bytes into json.loads # python must be >= 3.9 so we can pass in bytes into json.loads
return json.loads(response.data) return json.loads(response.data)
@staticmethod @classmethod
def __file_name_from_content_disposition(content_disposition: typing.Optional[str]) -> typing.Optional[str]: def __file_name_from_content_disposition(cls, content_disposition: typing.Optional[str]) -> typing.Optional[str]:
if content_disposition is None: if content_disposition is None:
return None return None
match = re.search('filename="(.+?)"', content_disposition) match = cls.__filename_content_disposition_pattern.search(content_disposition)
if not match: if not match:
return None return None
return match.group(1) return match.group(1)