Skip to content

Commit

Permalink
don't reformat query params for dpg next link calls (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Mar 3, 2022
1 parent f737949 commit f3d33d6
Show file tree
Hide file tree
Showing 21 changed files with 356 additions and 393 deletions.
270 changes: 138 additions & 132 deletions ChangeLog.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions autorest/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ def _validate_code_model_options(options: Dict[str, Any]) -> None:
"If you want operation files, pass in flag --show-operations"
)

if options["reformat_next_link"] and options["version_tolerant"]:
raise ValueError(
"--reformat-next-link can not be true for version tolerant generations. "
"Please remove --reformat-next-link from your call for version tolerant generations."
)

_LOGGER = logging.getLogger(__name__)
class CodeGenerator(Plugin):
@staticmethod
Expand Down Expand Up @@ -292,6 +298,7 @@ def _build_code_model_options(self) -> Dict[str, Any]:
"default_optional_constants_to_none": self._autorestapi.get_boolean_value(
"default-optional-constants-to-none", low_level_client or version_tolerant
),
"reformat_next_link": self._autorestapi.get_boolean_value("reformat-next-link", not version_tolerant)
}

if options["builders_visibility"] is None:
Expand Down
3 changes: 2 additions & 1 deletion autorest/codegen/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .imports import FileImport, ImportType, TypingSection
from .lro_operation import LROOperation
from .paging_operation import PagingOperation
from .parameter import Parameter, ParameterStyle
from .parameter import Parameter, ParameterStyle, ParameterLocation
from .operation import Operation
from .property import Property
from .operation_group import OperationGroup
Expand Down Expand Up @@ -50,6 +50,7 @@
"PagingOperation",
"Parameter",
"ParameterList",
"ParameterLocation",
"OperationGroup",
"Property",
"RequestBuilder",
Expand Down
14 changes: 14 additions & 0 deletions autorest/codegen/serializers/builder_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SchemaResponse,
IOSchema,
ParameterStyle,
ParameterLocation
)
from . import utils

Expand Down Expand Up @@ -798,6 +799,7 @@ def _call_request_builder_helper(
builder,
request_builder: RequestBuilder,
template_url: Optional[str] = None,
is_next_request: bool = False,
) -> List[str]:
retval = []
if len(builder.body_kwargs_to_pass_to_request_builder) > 1:
Expand Down Expand Up @@ -837,6 +839,16 @@ def _call_request_builder_helper(
parameter.serialized_name not in builder.body_kwargs_to_pass_to_request_builder
):
continue
if (
is_next_request and
not bool(builder.next_request_builder) and
not self.code_model.options["reformat_next_link"] and
parameter.location == ParameterLocation.Query
):
# if we don't want to reformat query parameters for next link calls
# in paging operations with a single swagger operation defintion,
# we skip passing query params when building the next request
continue
high_level_name = cast(RequestBuilderParameter, parameter).name_in_high_level_operation
retval.append(f" {parameter.serialized_name}={high_level_name},")
if not self.code_model.options["version_tolerant"]:
Expand Down Expand Up @@ -1083,11 +1095,13 @@ def call_next_link_request_builder(self, builder) -> List[str]:
else:
request_builder = builder.request_builder
template_url = "next_link"

request_builder = builder.next_request_builder or builder.request_builder
return self._call_request_builder_helper(
builder,
request_builder,
template_url=template_url,
is_next_request=True
)

def _prepare_request_callback(self, builder) -> List[str]:
Expand Down
1 change: 1 addition & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def _build_flags(
generation_section += "/legacy"
override_flags = override_flags or {}
override_flags["payload-flattening-threshold"] = 1
override_flags["reformat-next-link"] = False

flags = {
"use": autorest_dir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,8 @@
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
from typing import List
import importlib
from ._auto_rest_paging_test_service import AutoRestPagingTestService as AutoRestPagingTestServiceGenerated
from azure.core.pipeline.policies import SansIOHTTPPolicy

try:
binary_type = str
import urlparse # type: ignore
from urllib import urlencode
except ImportError:
binary_type = bytes # type: ignore
from urllib import parse as urlparse
from urllib.parse import urlencode


class RemoveDuplicateParamsPolicy(SansIOHTTPPolicy):
def __init__(self, duplicate_param_names):
# type: (List[str]) -> None
self.duplicate_param_names = duplicate_param_names

def on_request(self, request):
parsed_url = urlparse.urlparse(request.http_request.url)
query_params = urlparse.parse_qs(parsed_url.query)
# service returned will be later in the url because of how we format
filtered_query_params = {k: v[-1:] if k in self.duplicate_param_names else v for k, v in query_params.items()}
request.http_request.url = request.http_request.url.replace(parsed_url.query, "") + urlencode(
filtered_query_params, doseq=True
)
return super(RemoveDuplicateParamsPolicy, self).on_request(request)


class AutoRestPagingTestService(AutoRestPagingTestServiceGenerated):
def __init__(self, *args, **kwargs):
per_call_policies = kwargs.pop("per_call_policies", [])
params_policy = RemoveDuplicateParamsPolicy(duplicate_param_names=["$filter", "$skiptoken"])
try:
per_call_policies.append(params_policy)
except AttributeError:
per_call_policies = [per_call_policies, params_policy]
super(AutoRestPagingTestService, self).__init__(*args, per_call_policies=per_call_policies, **kwargs)


# This file is used for handwritten extensions to the generated code. Example:
# https://github.com/Azure/azure-sdk-for-python/blob/main/doc/dev/customize_code/how-to-patch-sdk-code.md
def patch_sdk():
curr_package = importlib.import_module("paging")
curr_package.AutoRestPagingTestService = AutoRestPagingTestService
pass
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,8 @@
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
import importlib
from .._patch import RemoveDuplicateParamsPolicy
from ._auto_rest_paging_test_service import AutoRestPagingTestService as AutoRestPagingTestServiceGenerated


class AutoRestPagingTestService(AutoRestPagingTestServiceGenerated):
def __init__(self, *args, **kwargs):
per_call_policies = kwargs.pop("per_call_policies", [])
params_policy = RemoveDuplicateParamsPolicy(duplicate_param_names=["$filter", "$skiptoken"])
try:
per_call_policies.append(params_policy)
except AttributeError:
per_call_policies = [per_call_policies, params_policy]
super().__init__(*args, per_call_policies=per_call_policies, **kwargs)


# This file is used for handwritten extensions to the generated code. Example:
# https://github.com/Azure/azure-sdk-for-python/blob/main/doc/dev/customize_code/how-to-patch-sdk-code.md
def patch_sdk():
curr_package = importlib.import_module("paging.aio")
curr_package.AutoRestPagingTestService = AutoRestPagingTestService
pass
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ def prepare_request(next_link=None):
else:

request = build_duplicate_params_request(
filter=filter,
template_url=next_link,
)
request = _convert_request(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,6 @@ def prepare_request(next_link=None):
else:

request = build_duplicate_params_request(
filter=filter,
template_url=next_link,
)
request = _convert_request(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ def prepare_request(next_link=None):

request = build_list_request(
subscription_id=self._config.subscription_id,
api_version=api_version,
template_url=next_link,
)
request = _convert_request(request)
Expand Down Expand Up @@ -577,7 +576,6 @@ def prepare_request(next_link=None):
request = build_list_by_resource_group_request(
resource_group_name=resource_group_name,
subscription_id=self._config.subscription_id,
api_version=api_version,
template_url=next_link,
)
request = _convert_request(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,6 @@ def prepare_request(next_link=None):

request = build_list_request(
subscription_id=self._config.subscription_id,
api_version=api_version,
template_url=next_link,
)
request = _convert_request(request)
Expand Down Expand Up @@ -921,7 +920,6 @@ def prepare_request(next_link=None):
request = build_list_by_resource_group_request(
resource_group_name=resource_group_name,
subscription_id=self._config.subscription_id,
api_version=api_version,
template_url=next_link,
)
request = _convert_request(request)
Expand Down
Loading

0 comments on commit f3d33d6

Please sign in to comment.