Skip to content

Commit

Permalink
fix case of grouped parameter + reserved property name (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Jul 13, 2021
1 parent e294bb0 commit 3cf0599
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 6 deletions.
5 changes: 3 additions & 2 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Change Log

### 2021-07-06 - 5.8.4
### 2021-07-13 - 5.8.4

min Autorest core version: 3.3.0
min Autorest core version: 3.4.5

min Modelerfour version: 4.19.1

**Bug Fixes**

- Fix case where we have a grouped parameter whose name is a reserved property name #970
- Remove all hosts from global parameters, regardless of how many m4 sends us #972

### 2021-07-06 - 5.8.3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ modelerfour:
pass-thru:
- model-deduplicator
- subset-reducer
version: ~3.3.0
version: ~3.4.5
use-extension:
"@autorest/modelerfour": ~4.19.1

Expand Down
16 changes: 15 additions & 1 deletion autorest/codegen/models/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
# --------------------------------------------------------------------------
from enum import Enum
import logging
from typing import Dict, Optional, List, Any, Union, Tuple
from typing import Dict, Optional, List, Any, Union, Tuple, cast

from .imports import FileImport, ImportType, TypingSection
from .base_model import BaseModel
from .base_schema import BaseSchema
from .list_schema import ListSchema
from .constant_schema import ConstantSchema
from .object_schema import ObjectSchema
from .property import Property


_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -171,6 +173,18 @@ def in_method_signature(self) -> bool:
or self.is_kwarg
)

@property
def corresponding_grouped_property(self) -> Property:
if not self.grouped_by:
raise ValueError("Should only be calling if your parameter is grouped")
try:
return next(
p for p in cast(ObjectSchema, self.grouped_by.schema).properties
if any(op for op in p.yaml_data['originalParameter'] if id(op) == self.id)
)
except StopIteration:
raise ValueError("There is not a corresponding grouped property for your parameter.")

@property
def in_method_code(self) -> bool:
return not (self.constant and self.location == ParameterLocation.Other)
Expand Down
2 changes: 1 addition & 1 deletion autorest/codegen/templates/operation_tools.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ deserialized = self._deserialize('{{ response.serialization_type }}', pipeline_r
{% for grouper_name, grouped_parameters in operation.parameters.grouped|groupby("grouped_by.serialized_name") %}
if {{ grouper_name }} is not None:
{% for grouped_parameter in grouped_parameters %}
{{ grouped_parameter.serialized_name }} = {{ grouper_name }}.{{ grouped_parameter.serialized_name.lstrip('_') }}
{{ grouped_parameter.serialized_name }} = {{ grouper_name }}.{{ grouped_parameter.corresponding_grouped_property.name }}
{% endfor %}
{% endfor %}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"devDependencies": {
"@autorest/autorest": "^3.0.0",
"@microsoft.azure/autorest.testserver": "^3.0.26"
"@microsoft.azure/autorest.testserver": "^3.0.27"
},
"files": [
"autorest/**/*.py",
Expand Down
9 changes: 9 additions & 0 deletions test/azure/AcceptanceTests/asynctests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ async def test_post_shared_parameter_group_object(self, client, header_parameter
first_group = FirstParameterGroup(header_one = header_parameter)
await client.parameter_grouping.post_shared_parameter_group_object(first_group)

@pytest.mark.asyncio
async def test_post_reserved_words(self, client):
from azureparametergrouping.models import ParameterGroupingPostReservedWordsParameters
group = ParameterGroupingPostReservedWordsParameters(
from_property="bob",
accept="yes"
)
await client.parameter_grouping.post_reserved_words(group)

@pytest.mark.asyncio
async def test_subscription_in_credentials(self, azure_client):
# valid_api_version = '2.0'
Expand Down
8 changes: 8 additions & 0 deletions test/azure/AcceptanceTests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ def test_post_shared_parameter_group_object(self, client, header_parameter):
first_group = FirstParameterGroup(header_one = header_parameter)
client.parameter_grouping.post_shared_parameter_group_object(first_group)

def test_post_reserved_words(self, client):
from azureparametergrouping.models import ParameterGroupingPostReservedWordsParameters
group = ParameterGroupingPostReservedWordsParameters(
from_property="bob",
accept="yes"
)
client.parameter_grouping.post_reserved_words(group)

def test_subscription_in_credentials(self, azure_client):
# valid_api_version = '2.0'
azure_client.subscription_in_credentials.post_method_global_not_provided_valid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,62 @@ async def post_optional(

post_optional.metadata = {"url": "/parameterGrouping/postOptional"} # type: ignore

@distributed_trace_async
async def post_reserved_words(
self,
parameter_grouping_post_reserved_words_parameters: Optional[
"_models.ParameterGroupingPostReservedWordsParameters"
] = None,
**kwargs: Any
) -> None:
"""Post a grouped parameters with reserved words.
:param parameter_grouping_post_reserved_words_parameters: Parameter group.
:type parameter_grouping_post_reserved_words_parameters: ~azureparametergrouping.models.ParameterGroupingPostReservedWordsParameters
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop("cls", None) # type: ClsType[None]
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))

_from = None
_accept = None
if parameter_grouping_post_reserved_words_parameters is not None:
_from = parameter_grouping_post_reserved_words_parameters.from_property
_accept = parameter_grouping_post_reserved_words_parameters.accept
accept = "application/json"

# Construct URL
url = self.post_reserved_words.metadata["url"] # type: ignore

# Construct parameters
query_parameters = {} # type: Dict[str, Any]
if _from is not None:
query_parameters["from"] = self._serialize.query("from", _from, "str")
if _accept is not None:
query_parameters["accept"] = self._serialize.query("accept", _accept, "str")

# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters["Accept"] = self._serialize.header("accept", accept, "str")

request = self._client.post(url, query_parameters, header_parameters)
pipeline_response = await self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response

if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize(_models.Error, response)
raise HttpResponseError(response=response, model=error)

if cls:
return cls(pipeline_response, None, {})

post_reserved_words.metadata = {"url": "/parameterGrouping/postReservedWords"} # type: ignore

@distributed_trace_async
async def post_multi_param_groups(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
from ._models_py3 import ParameterGroupingPostMultiParamGroupsSecondParamGroup
from ._models_py3 import ParameterGroupingPostOptionalParameters
from ._models_py3 import ParameterGroupingPostRequiredParameters
from ._models_py3 import ParameterGroupingPostReservedWordsParameters
except (SyntaxError, ImportError):
from ._models import Error # type: ignore
from ._models import FirstParameterGroup # type: ignore
from ._models import ParameterGroupingPostMultiParamGroupsSecondParamGroup # type: ignore
from ._models import ParameterGroupingPostOptionalParameters # type: ignore
from ._models import ParameterGroupingPostRequiredParameters # type: ignore
from ._models import ParameterGroupingPostReservedWordsParameters # type: ignore

__all__ = [
"Error",
"FirstParameterGroup",
"ParameterGroupingPostMultiParamGroupsSecondParamGroup",
"ParameterGroupingPostOptionalParameters",
"ParameterGroupingPostRequiredParameters",
"ParameterGroupingPostReservedWordsParameters",
]
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,23 @@ def __init__(self, **kwargs):
self.query = kwargs.get("query", 30)
self.path = kwargs["path"]
self.body = kwargs["body"]


class ParameterGroupingPostReservedWordsParameters(msrest.serialization.Model):
"""Parameter group.
:param from_property: 'from' is a reserved word. Pass in 'bob' to pass.
:type from_property: str
:param accept: 'accept' is a reserved word. Pass in 'yes' to pass.
:type accept: str
"""

_attribute_map = {
"from_property": {"key": "from", "type": "str"},
"accept": {"key": "accept", "type": "str"},
}

def __init__(self, **kwargs):
super(ParameterGroupingPostReservedWordsParameters, self).__init__(**kwargs)
self.from_property = kwargs.get("from_property", None)
self.accept = kwargs.get("accept", None)
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,23 @@ def __init__(
self.query = query
self.path = path
self.body = body


class ParameterGroupingPostReservedWordsParameters(msrest.serialization.Model):
"""Parameter group.
:param from_property: 'from' is a reserved word. Pass in 'bob' to pass.
:type from_property: str
:param accept: 'accept' is a reserved word. Pass in 'yes' to pass.
:type accept: str
"""

_attribute_map = {
"from_property": {"key": "from", "type": "str"},
"accept": {"key": "accept", "type": "str"},
}

def __init__(self, *, from_property: Optional[str] = None, accept: Optional[str] = None, **kwargs):
super(ParameterGroupingPostReservedWordsParameters, self).__init__(**kwargs)
self.from_property = from_property
self.accept = accept
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,61 @@ def post_optional(

post_optional.metadata = {"url": "/parameterGrouping/postOptional"} # type: ignore

@distributed_trace
def post_reserved_words(
self,
parameter_grouping_post_reserved_words_parameters=None, # type: Optional["_models.ParameterGroupingPostReservedWordsParameters"]
**kwargs # type: Any
):
# type: (...) -> None
"""Post a grouped parameters with reserved words.
:param parameter_grouping_post_reserved_words_parameters: Parameter group.
:type parameter_grouping_post_reserved_words_parameters: ~azureparametergrouping.models.ParameterGroupingPostReservedWordsParameters
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop("cls", None) # type: ClsType[None]
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
error_map.update(kwargs.pop("error_map", {}))

_from = None
_accept = None
if parameter_grouping_post_reserved_words_parameters is not None:
_from = parameter_grouping_post_reserved_words_parameters.from_property
_accept = parameter_grouping_post_reserved_words_parameters.accept
accept = "application/json"

# Construct URL
url = self.post_reserved_words.metadata["url"] # type: ignore

# Construct parameters
query_parameters = {} # type: Dict[str, Any]
if _from is not None:
query_parameters["from"] = self._serialize.query("from", _from, "str")
if _accept is not None:
query_parameters["accept"] = self._serialize.query("accept", _accept, "str")

# Construct headers
header_parameters = {} # type: Dict[str, Any]
header_parameters["Accept"] = self._serialize.header("accept", accept, "str")

request = self._client.post(url, query_parameters, header_parameters)
pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)
response = pipeline_response.http_response

if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize(_models.Error, response)
raise HttpResponseError(response=response, model=error)

if cls:
return cls(pipeline_response, None, {})

post_reserved_words.metadata = {"url": "/parameterGrouping/postReservedWords"} # type: ignore

@distributed_trace
def post_multi_param_groups(
self,
Expand Down

0 comments on commit 3cf0599

Please sign in to comment.