Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions autorest/namer/name_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@
from typing import cast, Any, Dict, List, Match, Optional
from .python_mappings import basic_latin_chars, reserved_words, PadType

def _get_all_values(all_headers: List[Dict[str, Any]]) -> List[str]:
content_types: List[str] = []
for h in all_headers:
if h['schema']['type'] == 'constant':
content_types.append(h['schema']['value']['value'])
elif any(
choice_type for choice_type in ['sealed-choice', 'choice']
if h['schema']['type'] == choice_type
):
content_types.extend([
choice['value']
for choice in h['schema']['choices']
])
return content_types

def _get_default_value(all_values: List[str]) -> str:
json_values = [v for v in all_values if "json" in v]
if json_values:
if "application/json" in json_values:
return "application/json"
return json_values[0]

xml_values = [v for v in all_values if "xml" in v]
if xml_values:
if "application/xml" in xml_values:
return "application/xml"
return xml_values[0]
return all_values[0]

_M4_HEADER_PARAMETERS = ["content_type", "accept"]
class NameConverter:
@staticmethod
Expand Down Expand Up @@ -105,6 +134,7 @@ def _handle_m4_header_parameters(requests):
id(p) for p in params_of_header[1:]
])
else:
all_values = _get_all_values(params_of_header)
# if one of them is an enum schema, set the default value to constant
param_with_constant_schema = next(p for p in params_of_header if p['schema']['type'] == 'constant')
try:
Expand All @@ -116,9 +146,23 @@ def _handle_m4_header_parameters(requests):
# this means there's no enum schema
pass
else:
param_with_enum_schema['clientDefaultValue'] = (
param_with_constant_schema['schema']['value']['value']
)
param_with_enum_schema['clientDefaultValue'] = _get_default_value(all_values)
# add constant enum schema value into list of possible schema values
constant_schema = param_with_constant_schema['schema']
constant_choice = {
"language": {
"default": {
"description": constant_schema['language']['default']['description'],
"name": constant_schema['language']['default']['name'],
},
"python": {
"description": constant_schema['language']['python']['description'],
"name": constant_schema['language']['python']['name'].upper(),
}
},
"value": constant_schema['value']['value']
}
param_with_enum_schema['schema']['choices'].append(constant_choice)
m4_header_params_to_remove.append(id(param_with_constant_schema))

for request in requests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ class ContentType(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
IMAGE_PNG = "image/png"
#: Content Type 'image/tiff'.
IMAGE_TIFF = "image/tiff"
#: Content Type 'application/json'.
APPLICATION_JSON = "application/json"
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ class ContentType(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
IMAGE_PNG = "image/png"
#: Content Type 'image/tiff'.
IMAGE_TIFF = "image/tiff"
#: Content Type 'application/json'.
APPLICATION_JSON = "application/json"
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ class ContentType(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
IMAGE_PNG = "image/png"
#: Content Type 'image/tiff'.
IMAGE_TIFF = "image/tiff"
#: Content Type 'application/json'.
APPLICATION_JSON = "application/json"
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ class ContentType(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
IMAGE_PNG = "image/png"
#: Content Type 'image/tiff'.
IMAGE_TIFF = "image/tiff"
#: Content Type 'application/json'.
APPLICATION_JSON = "application/json"
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ class ContentType(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
IMAGE_PNG = "image/png"
#: Content Type 'image/tiff'.
IMAGE_TIFF = "image/tiff"
#: Content Type 'application/json'.
APPLICATION_JSON = "application/json"
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ class ContentType(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
IMAGE_PNG = "image/png"
#: Content Type 'image/tiff'.
IMAGE_TIFF = "image/tiff"
#: Content Type 'application/json'.
APPLICATION_JSON = "application/json"
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ async def binary_body_with_three_content_types(self, message: Union[IO, str], **
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))

content_type = kwargs.pop("content_type", "text/plain") # type: Optional[Union[str, "_models.ContentType1"]]
content_type = kwargs.pop(
"content_type", "application/json"
) # type: Optional[Union[str, "_models.ContentType1"]]

content = message

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ContentType(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
IMAGE_PNG = "image/png"
#: Content Type 'image/tiff'.
IMAGE_TIFF = "image/tiff"
#: Content Type 'application/json'.
APPLICATION_JSON = "application/json"


class ContentType1(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
Expand All @@ -31,3 +33,5 @@ class ContentType1(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
APPLICATION_JSON = "application/json"
#: Content Type 'application/octet-stream'.
APPLICATION_OCTET_STREAM = "application/octet-stream"
#: Content Type 'text/plain'.
TEXT_PLAIN = "text/plain"
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ def binary_body_with_three_content_types(
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))

content_type = kwargs.pop("content_type", "text/plain") # type: Optional[Union[str, "_models.ContentType1"]]
content_type = kwargs.pop(
"content_type", "application/json"
) # type: Optional[Union[str, "_models.ContentType1"]]

content = message

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ async def binary_body_with_three_content_types(self, message: Union[IO, str], **
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))

content_type = kwargs.pop("content_type", "text/plain") # type: Optional[str]
content_type = kwargs.pop("content_type", "application/json") # type: Optional[str]

json = None
content = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def binary_body_with_three_content_types(
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))

content_type = kwargs.pop("content_type", "text/plain") # type: Optional[str]
content_type = kwargs.pop("content_type", "application/json") # type: Optional[str]

json = None
content = None
Expand Down