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
18 changes: 18 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Change Log

### 2022-xx-xx - 5.14.0

| Library | Min Version
| --------------- | -------
|`@autorest/core` | `3.6.2`
|`@autorest/modelerfour` | `4.19.1`
|`azure-core` dep of generated code | `1.20.1`
|`msrest` dep of generated code | `0.6.21`
|`azure-mgmt-core` dep of generated code (If generating mgmt plane code) | `1.3.0`

**Breaking Changes in Version Tolerant Generation**

- We now generate with optional constant parameters as None by defaulting `--default-optional-constants-to-none` to True #1171

**New Features**

- Add flag `--default-optional-constants-to-none` with which optional constant parameters is default to None #1171

### 2022-03-02 - 5.13.0

| Library | Min Version
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
15 changes: 14 additions & 1 deletion autorest/codegen/models/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def description(self):
if description:
description += " "
description += f"{self.schema.extra_description_information}"
if isinstance(self.schema, ConstantSchema) and not self.constant:
if description:
description += " "
description += f"Possible values are {self.schema.get_declaration(self.schema.value)} or {None}."
if self.has_default_value and not any(
l for l in ["default value is", "default is"] if l in description.lower()
):
Expand Down Expand Up @@ -237,7 +241,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 +328,10 @@ def is_hidden(self) -> bool:
self.yaml_data["implementation"] == "Client" and self.constant
)

@property
def is_content_type(self) -> bool:
return self.rest_api_name == "Content-Type" and self.location == ParameterLocation.Header

@property
def is_positional(self) -> bool:
return self.in_method_signature and not (self.is_keyword_only or self.is_kwarg)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@autorest/python",
"version": "5.13.0",
"version": "5.14.0",
"description": "The Python extension for generators in AutoRest.",
"scripts": {
"prepare": "node run-python3.js prepare.py",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async def put_no_model_as_string_no_required_one_value_no_default( # pylint: di

Puts constants to the testserver.

:param input: Default value is "value1".
:param input: Possible values are "value1" or None. Default value is "value1".
:type input: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -200,7 +200,7 @@ async def put_no_model_as_string_no_required_one_value_default( # pylint: disab

Puts constants to the testserver.

:param input: Default value is "value1".
:param input: Possible values are "value1" or None. Default value is "value1".
:type input: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def put_no_model_as_string_no_required_one_value_no_default( # pylint: disable=

Puts constants to the testserver.

:param input: Default value is "value1".
:param input: Possible values are "value1" or None. Default value is "value1".
:type input: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -575,7 +575,7 @@ def put_no_model_as_string_no_required_one_value_default( # pylint: disable=inc

Puts constants to the testserver.

:param input: Default value is "value1".
:param input: Possible values are "value1" or None. Default value is "value1".
:type input: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ async def put400( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 400 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -232,7 +233,8 @@ async def patch400( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 400 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -279,7 +281,8 @@ async def post400( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 400 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -326,7 +329,8 @@ async def delete400( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 400 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -509,7 +513,8 @@ async def put404( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 404 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -556,7 +561,8 @@ async def patch405( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 405 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -603,7 +609,8 @@ async def post406( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 406 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -650,7 +657,8 @@ async def delete407( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 407 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -697,7 +705,8 @@ async def put409( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 409 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -880,7 +889,8 @@ async def put413( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 413 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -927,7 +937,8 @@ async def patch414( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 414 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -974,7 +985,8 @@ async def post415( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 415 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -1055,7 +1067,8 @@ async def delete417( # pylint: disable=inconsistent-return-statements
) -> None:
"""Return 417 status code - should be represented in the client as an error.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ async def put301( # pylint: disable=inconsistent-return-statements
"""Put true Boolean value in request returns 301. This request should not be automatically
redirected, but should return the received 301 to the caller for evaluation.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -358,7 +359,8 @@ async def patch302( # pylint: disable=inconsistent-return-statements
"""Patch true Boolean value in request returns 302. This request should not be automatically
redirected, but should return the received 302 to the caller for evaluation.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -409,7 +411,8 @@ async def post303( # pylint: disable=inconsistent-return-statements
"""Post true Boolean value in request returns 303. This request should be automatically
redirected usign a get, ultimately returning a 200 status code.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -574,7 +577,8 @@ async def put307( # pylint: disable=inconsistent-return-statements
) -> None:
"""Put redirected with 307, resulting in a 200 after redirect.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -625,7 +629,8 @@ async def patch307( # pylint: disable=inconsistent-return-statements
) -> None:
"""Patch redirected with 307, resulting in a 200 after redirect.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -676,7 +681,8 @@ async def post307( # pylint: disable=inconsistent-return-statements
) -> None:
"""Post redirected with 307, resulting in a 200 after redirect.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down Expand Up @@ -727,7 +733,8 @@ async def delete307( # pylint: disable=inconsistent-return-statements
) -> None:
"""Delete redirected with 307, resulting in a 200 after redirect.

:param boolean_value: Simple boolean value true. Default value is True.
:param boolean_value: Simple boolean value true. Possible values are True or None. Default
value is True.
:type boolean_value: bool
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
Expand Down
Loading