Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid name clashes between url, header, and query params #1143

Merged
merged 7 commits into from
Feb 8, 2022
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
24 changes: 17 additions & 7 deletions autorest/codegen/serializers/builder_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
T = TypeVar("T")
OrderedSet = Dict[T, None]

def _escape_str(input_str: str) -> str:
replace = input_str.replace("'", "\\'")
return f'"{replace}"'

def _improve_json_string(template_representation: str) -> Any:
origin = template_representation.split('\n')
Expand Down Expand Up @@ -110,7 +113,7 @@ def _pop_parameters_kwarg(
function_name: str,
kwarg_name: str,
) -> str:
return f'{function_name}_parameters = kwargs.pop("{kwarg_name}", {{}}) # type: Dict[str, Any]'
return f'_{function_name}_parameters = kwargs.pop("{kwarg_name}", {{}}) # type: Dict[str, Any]'

def _serialize_grouped_body(builder) -> List[str]:
retval: List[str] = []
Expand Down Expand Up @@ -399,7 +402,7 @@ def serializer_name(self) -> str:
def _serialize_parameter(
self, param: Parameter, function_name: str
) -> List[str]:
set_parameter = "{}_parameters['{}'] = {}".format(
set_parameter = "_{}_parameters['{}'] = {}".format(
function_name,
param.rest_api_name,
utils.build_serialize_data_call(param, function_name, self.serializer_name)
Expand Down Expand Up @@ -496,11 +499,11 @@ def _body_params_to_pass_to_request_creation(self, builder) -> List[str]:
def create_http_request(self, builder) -> List[str]:
retval = ["return HttpRequest("]
retval.append(f' method="{builder.method}",')
retval.append(" url=url,")
retval.append(" url=_url,")
if builder.parameters.query:
retval.append(" params=query_parameters,")
retval.append(" params=_query_parameters,")
if builder.parameters.headers:
retval.append(" headers=header_parameters,")
retval.append(" headers=_header_parameters,")
if builder.parameters.has_body:
retval.extend([
f" {body_kwarg}={body_kwarg},"
Expand Down Expand Up @@ -530,6 +533,13 @@ def serialize_query(self, builder) -> List[str]:
))
return retval

def construct_url(self, builder) -> str:
if any(o for o in ["low_level_client", "version_tolerant"] if self.code_model.options.get(o)):
url_value = _escape_str(builder.url)
else:
url_value = f'kwargs.pop("template_url", {_escape_str(builder.url)})'
return f"_url = {url_value}{' # pylint: disable=line-too-long' if len(url_value) > 114 else ''}"

class RequestBuilderGenericSerializer(_RequestBuilderBaseSerializer):
@property
def _want_inline_type_hints(self) -> bool:
Expand Down Expand Up @@ -983,8 +993,8 @@ def error_map(self, builder) -> List[str]:

@staticmethod
def get_metadata_url(builder) -> str:
url = builder.request_builder.url.replace("'", "\\'")
return f"{builder.python_name}.metadata = {{'url': '{ url }'}} # type: ignore"
url = _escape_str(builder.request_builder.url)
return f"{builder.python_name}.metadata = {{'url': { url }}} # type: ignore"

class _SyncOperationBaseSerializer(_OperationBaseSerializer): # pylint: disable=abstract-method
@property
Expand Down
9 changes: 2 additions & 7 deletions autorest/codegen/templates/request_builder.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@
{% endfor %}
{% endif %}
# Construct URL
{% if code_model.options["version_tolerant"] or code_model.options["low_level_client"] %}
{% set url_value = keywords.escape_str(request_builder.url) %}
{% else %}
{% set url_value = 'kwargs.pop("template_url", ' + keywords.escape_str(request_builder.url) + ')' %}
{% endif %}
url = {{ url_value }}{{ " # pylint: disable=line-too-long" if (url_value | length) > 114 else "" }}
{{ request_builder_serializer.construct_url(request_builder) }}
{% if request_builder.parameters.path %}
{{ op_tools.serialize(request_builder_serializer.serialize_path(request_builder)) | indent }}
url = _format_url_section(url, **path_format_arguments)
_url = _format_url_section(_url, **path_format_arguments)
{% endif %}

{% if request_builder.parameters.query %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def head200( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head200.metadata = {'url': '/http/success/200'} # type: ignore
head200.metadata = {'url': "/http/success/200"} # type: ignore


@distributed_trace_async
Expand Down Expand Up @@ -118,7 +118,7 @@ async def head204( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head204.metadata = {'url': '/http/success/204'} # type: ignore
head204.metadata = {'url': "/http/success/204"} # type: ignore


@distributed_trace_async
Expand Down Expand Up @@ -160,5 +160,5 @@ async def head404( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head404.metadata = {'url': '/http/success/404'} # type: ignore
head404.metadata = {'url': "/http/success/404"} # type: ignore

Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def build_head200_request(
):
# type: (...) -> HttpRequest
# Construct URL
url = kwargs.pop("template_url", '/http/success/200')
_url = kwargs.pop("template_url", "/http/success/200")

return HttpRequest(
method="HEAD",
url=url,
url=_url,
**kwargs
)

Expand All @@ -46,11 +46,11 @@ def build_head204_request(
):
# type: (...) -> HttpRequest
# Construct URL
url = kwargs.pop("template_url", '/http/success/204')
_url = kwargs.pop("template_url", "/http/success/204")

return HttpRequest(
method="HEAD",
url=url,
url=_url,
**kwargs
)

Expand All @@ -60,11 +60,11 @@ def build_head404_request(
):
# type: (...) -> HttpRequest
# Construct URL
url = kwargs.pop("template_url", '/http/success/404')
_url = kwargs.pop("template_url", "/http/success/404")

return HttpRequest(
method="HEAD",
url=url,
url=_url,
**kwargs
)

Expand Down Expand Up @@ -127,7 +127,7 @@ def head200( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head200.metadata = {'url': '/http/success/200'} # type: ignore
head200.metadata = {'url': "/http/success/200"} # type: ignore


@distributed_trace
Expand Down Expand Up @@ -170,7 +170,7 @@ def head204( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head204.metadata = {'url': '/http/success/204'} # type: ignore
head204.metadata = {'url': "/http/success/204"} # type: ignore


@distributed_trace
Expand Down Expand Up @@ -213,5 +213,5 @@ def head404( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head404.metadata = {'url': '/http/success/404'} # type: ignore
head404.metadata = {'url': "/http/success/404"} # type: ignore

Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def head200( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head200.metadata = {'url': '/http/success/200'} # type: ignore
head200.metadata = {'url': "/http/success/200"} # type: ignore


@distributed_trace_async
Expand Down Expand Up @@ -118,7 +118,7 @@ async def head204( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head204.metadata = {'url': '/http/success/204'} # type: ignore
head204.metadata = {'url': "/http/success/204"} # type: ignore


@distributed_trace_async
Expand Down Expand Up @@ -160,5 +160,5 @@ async def head404( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head404.metadata = {'url': '/http/success/404'} # type: ignore
head404.metadata = {'url': "/http/success/404"} # type: ignore

Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def build_head200_request(
):
# type: (...) -> HttpRequest
# Construct URL
url = kwargs.pop("template_url", '/http/success/200')
_url = kwargs.pop("template_url", "/http/success/200")

return HttpRequest(
method="HEAD",
url=url,
url=_url,
**kwargs
)

Expand All @@ -46,11 +46,11 @@ def build_head204_request(
):
# type: (...) -> HttpRequest
# Construct URL
url = kwargs.pop("template_url", '/http/success/204')
_url = kwargs.pop("template_url", "/http/success/204")

return HttpRequest(
method="HEAD",
url=url,
url=_url,
**kwargs
)

Expand All @@ -60,11 +60,11 @@ def build_head404_request(
):
# type: (...) -> HttpRequest
# Construct URL
url = kwargs.pop("template_url", '/http/success/404')
_url = kwargs.pop("template_url", "/http/success/404")

return HttpRequest(
method="HEAD",
url=url,
url=_url,
**kwargs
)

Expand Down Expand Up @@ -127,7 +127,7 @@ def head200( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head200.metadata = {'url': '/http/success/200'} # type: ignore
head200.metadata = {'url': "/http/success/200"} # type: ignore


@distributed_trace
Expand Down Expand Up @@ -170,7 +170,7 @@ def head204( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head204.metadata = {'url': '/http/success/204'} # type: ignore
head204.metadata = {'url': "/http/success/204"} # type: ignore


@distributed_trace
Expand Down Expand Up @@ -213,5 +213,5 @@ def head404( # pylint: disable=inconsistent-return-statements
if cls:
return cls(pipeline_response, None, {})

head404.metadata = {'url': '/http/success/404'} # type: ignore
head404.metadata = {'url': "/http/success/404"} # type: ignore

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def _basic_polling_initial(

return deserialized

_basic_polling_initial.metadata = {'url': '/basic/polling'} # type: ignore
_basic_polling_initial.metadata = {'url': "/basic/polling"} # type: ignore


@distributed_trace_async
Expand Down Expand Up @@ -135,7 +135,7 @@ def get_long_running_output(pipeline_response):
)
return AsyncCustomPoller(self._client, raw_result, get_long_running_output, polling_method)

begin_basic_polling.metadata = {'url': '/basic/polling'} # type: ignore
begin_basic_polling.metadata = {'url': "/basic/polling"} # type: ignore

@distributed_trace
def basic_paging(
Expand Down Expand Up @@ -200,4 +200,4 @@ async def get_next(next_link=None):
return AsyncCustomPager(
get_next, extract_data
)
basic_paging.metadata = {'url': '/basic/paging'} # type: ignore
basic_paging.metadata = {'url': "/basic/paging"} # type: ignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ def build_basic_polling_request_initial(

accept = "application/json"
# Construct URL
url = kwargs.pop("template_url", '/basic/polling')
_url = kwargs.pop("template_url", "/basic/polling")

# Construct headers
header_parameters = kwargs.pop("headers", {}) # type: Dict[str, Any]
_header_parameters = kwargs.pop("headers", {}) # type: Dict[str, Any]
if content_type is not None:
header_parameters['Content-Type'] = _SERIALIZER.header("content_type", content_type, 'str')
header_parameters['Accept'] = _SERIALIZER.header("accept", accept, 'str')
_header_parameters['Content-Type'] = _SERIALIZER.header("content_type", content_type, 'str')
_header_parameters['Accept'] = _SERIALIZER.header("accept", accept, 'str')

return HttpRequest(
method="PUT",
url=url,
headers=header_parameters,
url=_url,
headers=_header_parameters,
**kwargs
)

Expand All @@ -60,16 +60,16 @@ def build_basic_paging_request(
# type: (...) -> HttpRequest
accept = "application/json"
# Construct URL
url = kwargs.pop("template_url", '/basic/paging')
_url = kwargs.pop("template_url", "/basic/paging")

# Construct headers
header_parameters = kwargs.pop("headers", {}) # type: Dict[str, Any]
header_parameters['Accept'] = _SERIALIZER.header("accept", accept, 'str')
_header_parameters = kwargs.pop("headers", {}) # type: Dict[str, Any]
_header_parameters['Accept'] = _SERIALIZER.header("accept", accept, 'str')

return HttpRequest(
method="GET",
url=url,
headers=header_parameters,
url=_url,
headers=_header_parameters,
**kwargs
)

Expand Down Expand Up @@ -123,7 +123,7 @@ def _basic_polling_initial(

return deserialized

_basic_polling_initial.metadata = {'url': '/basic/polling'} # type: ignore
_basic_polling_initial.metadata = {'url': "/basic/polling"} # type: ignore


@distributed_trace
Expand Down Expand Up @@ -186,7 +186,7 @@ def get_long_running_output(pipeline_response):
)
return CustomPoller(self._client, raw_result, get_long_running_output, polling_method)

begin_basic_polling.metadata = {'url': '/basic/polling'} # type: ignore
begin_basic_polling.metadata = {'url': "/basic/polling"} # type: ignore

@distributed_trace
def basic_paging(
Expand Down Expand Up @@ -252,4 +252,4 @@ def get_next(next_link=None):
return CustomPager(
get_next, extract_data
)
basic_paging.metadata = {'url': '/basic/paging'} # type: ignore
basic_paging.metadata = {'url': "/basic/paging"} # type: ignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def head200(
return cls(pipeline_response, None, {})
return 200 <= response.status_code <= 299

head200.metadata = {'url': '/http/success/200'} # type: ignore
head200.metadata = {'url': "/http/success/200"} # type: ignore


@distributed_trace_async
Expand Down Expand Up @@ -121,7 +121,7 @@ async def head204(
return cls(pipeline_response, None, {})
return 200 <= response.status_code <= 299

head204.metadata = {'url': '/http/success/204'} # type: ignore
head204.metadata = {'url': "/http/success/204"} # type: ignore


@distributed_trace_async
Expand Down Expand Up @@ -164,5 +164,5 @@ async def head404(
return cls(pipeline_response, None, {})
return 200 <= response.status_code <= 299

head404.metadata = {'url': '/http/success/404'} # type: ignore
head404.metadata = {'url': "/http/success/404"} # type: ignore

Loading