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

default optional const parameter to none #1171

Merged
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

- Add default value consistently for parameters #1164
- Make `content_type` param keyword-only if there are multiple content types #1167
- Add default_optional_constants_to_none option for optional constant parameters #1171

### 2022-02-09 - 5.12.6

Expand Down
3 changes: 3 additions & 0 deletions autorest/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ def _build_code_model_options(self) -> Dict[str, Any]:
"low_level_client": low_level_client,
"combine_operation_files": self._autorestapi.get_boolean_value("combine-operation-files", version_tolerant),
"python3_only": python3_only,
"default_optional_constants_to_none": self._autorestapi.get_boolean_value(
"default-optional-constants-to-none", low_level_client or version_tolerant
),
}

if options["builders_visibility"] is None:
Expand Down
11 changes: 10 additions & 1 deletion autorest/codegen/models/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ def _default_value(self) -> Tuple[Optional[Any], str, str]:
default_value_declaration = "None"
else:
if isinstance(self.schema, ConstantSchema):
default_value = self.schema.get_declaration(self.schema.value)
if (self.required or
self.is_content_type or
not self.code_model.options["default_optional_constants_to_none"]):
default_value = self.schema.get_declaration(self.schema.value)
else:
default_value = None
default_value_declaration = default_value
else:
default_value = self.schema.default_value
Expand Down Expand Up @@ -319,6 +324,10 @@ def is_hidden(self) -> bool:
self.yaml_data["implementation"] == "Client" and self.constant
)

@property
def is_content_type(self) -> bool:
return self.serialized_name == "content_type" and self.location.value == "header"

@property
def is_positional(self) -> bool:
return self.in_method_signature and not (self.is_keyword_only or self.is_kwarg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ async def patch_single(self, body: Any, **kwargs: Any) -> None:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize("object", pipeline_response)
raise HttpResponseError(response=response, model=error)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know why there's different white space for mergepatchjson? Doesn't look like there were any code changes that would have led to it, maybe did you accidentally format it my opening the generated code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be strange that this happen when I generate the test project in windows, and don't happen in Linux. So I committed the Linux result to undo the changes.

But I guess it's not related with this PR because this also happen in branch autorestv3 in my local machine. I will investigate further when have time.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@

T = TypeVar("T")
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

_SERIALIZER = Serializer()
_SERIALIZER.client_side_validation = False
# fmt: off



def build_patch_single_request(
**kwargs # type: Any
):
Expand Down Expand Up @@ -99,7 +100,6 @@ def patch_single(
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize("object", pipeline_response)
raise HttpResponseError(response=response, model=error)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ def build_put_no_model_as_string_no_required_one_value_no_default_request(
See https://aka.ms/azsdk/python/protocol/quickstart for how to incorporate this request builder
into your code flow.

:keyword input: Default value is "value1".
:keyword input: Default value is None.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also update the documentation to say the other value is "value1"? I think playing with code here is good

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

:paramtype input: str
:return: Returns an :class:`~azure.core.rest.HttpRequest` that you will pass to the client's
`send_request` method. See https://aka.ms/azsdk/python/protocol/quickstart for how to
incorporate this response into your code flow.
:rtype: ~azure.core.rest.HttpRequest
"""

input = kwargs.pop('input', "value1") # type: Optional[str]
input = kwargs.pop('input', None) # type: Optional[str]

# Construct URL
_url = "/constants/putNoModelAsStringNoRequiredOneValueNoDefault"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def build_put_no_model_as_string_no_required_two_value_default_request(


def build_put_no_model_as_string_no_required_one_value_no_default_request(
*, input: Optional[str] = "value1", **kwargs: Any
*, input: Optional[str] = None, **kwargs: Any
) -> HttpRequest:
"""Puts constants to the testserver.

Expand All @@ -85,7 +85,7 @@ def build_put_no_model_as_string_no_required_one_value_no_default_request(
See https://aka.ms/azsdk/python/protocol/quickstart for how to incorporate this request builder
into your code flow.

:keyword input: Default value is "value1".
:keyword input: Default value is None.
:paramtype input: str
:return: Returns an :class:`~azure.core.rest.HttpRequest` that you will pass to the client's
`send_request` method. See https://aka.ms/azsdk/python/protocol/quickstart for how to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ async def put_no_model_as_string_no_required_two_value_default( # pylint: disab

@distributed_trace_async
async def put_no_model_as_string_no_required_one_value_no_default( # pylint: disable=inconsistent-return-statements
self, *, input: Optional[str] = "value1", **kwargs: Any
self, *, input: Optional[str] = None, **kwargs: Any
) -> None:
"""Puts constants to the testserver.

Puts constants to the testserver.

:keyword input: Default value is "value1".
:keyword input: Default value is None.
:paramtype input: str
:return: None
:rtype: None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def build_contants_put_no_model_as_string_no_required_two_value_default_request(


def build_contants_put_no_model_as_string_no_required_one_value_no_default_request(
*, input: Optional[str] = "value1", **kwargs: Any
*, input: Optional[str] = None, **kwargs: Any
) -> HttpRequest:
# Construct URL
_url = "/constants/putNoModelAsStringNoRequiredOneValueNoDefault"
Expand Down Expand Up @@ -361,13 +361,13 @@ def put_no_model_as_string_no_required_two_value_default( # pylint: disable=inc

@distributed_trace
def put_no_model_as_string_no_required_one_value_no_default( # pylint: disable=inconsistent-return-statements
self, *, input: Optional[str] = "value1", **kwargs: Any
self, *, input: Optional[str] = None, **kwargs: Any
) -> None:
"""Puts constants to the testserver.

Puts constants to the testserver.

:keyword input: Default value is "value1".
:keyword input: Default value is None.
:paramtype input: str
:return: None
:rtype: None
Expand Down
Loading